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 | 7f815dba267fa7e5153b3312dc77a1db27c7622a (patch) | |
tree | cea8e25b7bded0833b3e4c6a5c1c018785cd3c30 /BackEnd/Timeline/Controllers | |
parent | 31268d3cb8870452a0d6804b61a94ef1faafa09e (diff) | |
parent | 9494c9717f0d9983ec1c8db092387fef7199b00d (diff) | |
download | timeline-7f815dba267fa7e5153b3312dc77a1db27c7622a.tar.gz timeline-7f815dba267fa7e5153b3312dc77a1db27c7622a.tar.bz2 timeline-7f815dba267fa7e5153b3312dc77a1db27c7622a.zip |
Merge pull request #211 from crupest/search
Back search feature.
Diffstat (limited to 'BackEnd/Timeline/Controllers')
-rw-r--r-- | BackEnd/Timeline/Controllers/SearchController.cs | 61 | ||||
-rw-r--r-- | BackEnd/Timeline/Controllers/TimelineController.cs | 2 |
2 files changed, 62 insertions, 1 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);
+ }
+ }
+}
diff --git a/BackEnd/Timeline/Controllers/TimelineController.cs b/BackEnd/Timeline/Controllers/TimelineController.cs index b2e37b15..5d484388 100644 --- a/BackEnd/Timeline/Controllers/TimelineController.cs +++ b/BackEnd/Timeline/Controllers/TimelineController.cs @@ -441,7 +441,7 @@ namespace Timeline.Controllers [ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
- public async Task<ActionResult<HttpTimeline>> TimelineCreate([FromBody] TimelineCreateRequest body)
+ public async Task<ActionResult<HttpTimeline>> TimelineCreate([FromBody] HttpTimelineCreateRequest body)
{
var userId = this.GetUserId();
|