diff options
author | crupest <crupest@outlook.com> | 2021-01-19 15:27:01 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-19 15:27:01 +0800 |
commit | 23433673f91c1b07c43c4b2280e29db45e8896b5 (patch) | |
tree | 455fbd07678b1b21a1bfa3ad7e3778b1957fd12f /BackEnd/Timeline/Controllers/SearchController.cs | |
parent | a23b8af0b06be2ab58d1831a0a25a30d934ec1e2 (diff) | |
parent | fb6442f1716406c7a2da79e4a1cc42e23908d218 (diff) | |
download | timeline-23433673f91c1b07c43c4b2280e29db45e8896b5.tar.gz timeline-23433673f91c1b07c43c4b2280e29db45e8896b5.tar.bz2 timeline-23433673f91c1b07c43c4b2280e29db45e8896b5.zip |
Merge pull request #211 from crupest/search
Back search feature.
Diffstat (limited to 'BackEnd/Timeline/Controllers/SearchController.cs')
-rw-r--r-- | BackEnd/Timeline/Controllers/SearchController.cs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/BackEnd/Timeline/Controllers/SearchController.cs b/BackEnd/Timeline/Controllers/SearchController.cs new file mode 100644 index 00000000..dec876b6 --- /dev/null +++ b/BackEnd/Timeline/Controllers/SearchController.cs @@ -0,0 +1,61 @@ +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;
+ }
+
+ /// <summary>
+ /// Search timelines whose name or title contains query string case-insensitively.
+ /// </summary>
+ /// <param name="query">The string to contain.</param>
+ /// <returns>Timelines with most related at first.</returns>
+ [HttpGet("timelines")]
+ [ProducesResponseType(200)]
+ [ProducesResponseType(400)]
+ 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());
+ }
+
+ /// <summary>
+ /// Search users whose username or nick contains query string case-insensitively.
+ /// </summary>
+ /// <param name="query">The string to contain.</param>
+ /// <returns>Users with most related at first.</returns>
+ [HttpGet("users")]
+ [ProducesResponseType(200)]
+ [ProducesResponseType(400)]
+ 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);
+ }
+ }
+}
|