summaryrefslogtreecommitdiffstats
path: root/server/src/Endpoints/V1/Labels/GetLabelRoute.cs
blob: c9ccef312828f8460ffc22fd2aaf4086986f9034 (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
32
33
34
namespace IOL.GreatOffice.Api.Endpoints.V1.Labels;

/// <inheritdoc />
public class GetEndpoint : RouteBaseSync.WithoutRequest.WithActionResult<List<TimeLabel.TimeLabelDto>>
{
	private readonly AppDbContext _context;

	/// <inheritdoc />
	public GetEndpoint(AppDbContext context) {
		_context = context;
	}

	/// <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 = _context.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);
	}
}