blob: dec876b6951a6b6f9abab045c8390eba45fbbd20 (
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
|
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);
}
}
}
|