aboutsummaryrefslogtreecommitdiffstats
path: root/code/api/src/Endpoints/V1/Labels/GetLabelRoute.cs
blob: 09d453b606ff2b507e42aae6d3c863d437875333 (plain) (blame)
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
namespace IOL.GreatOffice.Api.Endpoints.V1.Labels;

public class GetEndpoint : RouteBaseSync.WithoutRequest.WithActionResult<List<TimeLabel.TimeLabelDto>>
{
    private readonly MainAppDatabase _database;

    public GetEndpoint(MainAppDatabase database) {
        _database = database;
    }

    /// <summary>
    /// Get a minimal list of time entry labels.
    /// </summary>
    /// <returns></returns>
    [ApiVersion(ApiSpecV1.VERSION_STRING)]
    [BasicAuthentication(AppConstants.TOKEN_ALLOW_READ)]
    [HttpGet("~/v{version:apiVersion}/labels")]
    public override ActionResult<List<TimeLabel.TimeLabelDto>> Handle() {
        var labels = _database.TimeLabels
            .Where(c => c.UserId == LoggedInUser.Id)
            .OrderByDescending(c => c.CreatedAt)
            .Select(c => c.AsDto)
            .ToList();

        if (labels.Count == 0) {
            return NoContent();
        }

        return Ok(labels);
    }
}