diff options
Diffstat (limited to 'BackEnd/Timeline/Controllers')
-rw-r--r-- | BackEnd/Timeline/Controllers/SearchController.cs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/BackEnd/Timeline/Controllers/SearchController.cs b/BackEnd/Timeline/Controllers/SearchController.cs new file mode 100644 index 00000000..915938de --- /dev/null +++ b/BackEnd/Timeline/Controllers/SearchController.cs @@ -0,0 +1,47 @@ +using Microsoft.AspNetCore.Mvc;
+using System.Collections.Generic;
+using System.ComponentModel.DataAnnotations;
+using System.Linq;
+using System.Threading.Tasks;
+using Timeline.Models.Http;
+using Timeline.Models.Mapper;
+using Timeline.Services;
+
+namespace Timeline.Controllers
+{
+ /// <summary>
+ /// Api related to search timelines or users.
+ /// </summary>
+ [ApiController]
+ [ProducesErrorResponseType(typeof(CommonResponse))]
+ [Route("search")]
+ public class SearchController : Controller
+ {
+ private readonly ISearchService _service;
+ private readonly TimelineMapper _timelineMapper;
+ private readonly UserMapper _userMapper;
+
+ public SearchController(ISearchService service, TimelineMapper timelineMapper, UserMapper userMapper)
+ {
+ _service = service;
+ _timelineMapper = timelineMapper;
+ _userMapper = userMapper;
+ }
+
+ [HttpGet("timelines")]
+ public async Task<ActionResult<List<HttpTimeline>>> TimelineSearch([FromQuery(Name = "q"), Required(AllowEmptyStrings = false)] string query)
+ {
+ var searchResult = await _service.SearchTimeline(query);
+ var timelines = searchResult.Items.Select(i => i.Item).ToList();
+ return await _timelineMapper.MapToHttp(timelines, Url, this.GetOptionalUserId());
+ }
+
+ [HttpGet("users")]
+ public async Task<ActionResult<List<HttpUser>>> UserSearch([FromQuery(Name = "q"), Required(AllowEmptyStrings = false)] string query)
+ {
+ var searchResult = await _service.SearchUser(query);
+ var users = searchResult.Items.Select(i => i.Item).ToList();
+ return await _userMapper.MapToHttp(users, Url);
+ }
+ }
+}
|