blob: 358c3739449dd80da7a2fa34a55325aaf312e549 (
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 : MyControllerBase
{
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.SearchTimelineAsync(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.SearchUserAsync(query);
var users = searchResult.Items.Select(i => i.Item).ToList();
return await _mapper.MapListAsync<HttpUser>(users, Url, User);
}
}
}
|