using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Threading.Tasks; using Timeline.Entities; using Timeline.Models.Http; using Timeline.Services.Api; using Timeline.Services.Mapper; namespace Timeline.Controllers { /// /// Api related to search timelines or users. /// [ApiController] [ProducesErrorResponseType(typeof(CommonResponse))] [Route("search")] public class SearchController : MyControllerBase { private readonly ISearchService _service; private readonly IGenericMapper _mapper; public SearchController(ISearchService service, IGenericMapper mapper) { _service = service; _mapper = mapper; } private Task> Map(List timelines) { return _mapper.MapListAsync(timelines, Url, User); } /// /// Search timelines whose name or title contains query string case-insensitively. /// /// The string to contain. /// Timelines with most related at first. [HttpGet("timelines")] [ProducesResponseType(200)] [ProducesResponseType(400)] public async Task>> TimelineSearch([FromQuery(Name = "q"), Required(AllowEmptyStrings = false)] string query) { var searchResult = await _service.SearchTimelineAsync(query); var timelines = searchResult.Items.Select(i => i.Item).ToList(); return await Map(timelines); } /// /// Search users whose username or nick contains query string case-insensitively. /// /// The string to contain. /// Users with most related at first. [HttpGet("users")] [ProducesResponseType(200)] [ProducesResponseType(400)] public async Task>> UserSearch([FromQuery(Name = "q"), Required(AllowEmptyStrings = false)] string query) { var searchResult = await _service.SearchUserAsync(query); var users = searchResult.Items.Select(i => i.Item).ToList(); return await _mapper.MapListAsync(users, Url, User); } } }