aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/Controllers/SearchController.cs
blob: 76f3d8f2f6340b2e3c9f11ee10ef1ef3a8a1c145 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
{
    /// <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 IGenericMapper _mapper;

        public SearchController(ISearchService service, IGenericMapper mapper)
        {
            _service = service;
            _mapper = mapper;
        }

        private Task<List<HttpTimeline>> Map(List<TimelineEntity> timelines)
        {
            return _mapper.MapListAsync<HttpTimeline>(timelines, Url, User);
        }

        /// <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 Map(timelines);
        }

        /// <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 _mapper.MapListAsync<HttpUser>(users, Url, User);
        }
    }
}