aboutsummaryrefslogtreecommitdiffstats
path: root/code/api/src/Endpoints/V1/Labels/UpdateLabelRoute.cs
blob: 9857b7d23013be7a92ac8db0a197195bea3da1fb (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
35
36
namespace IOL.GreatOffice.Api.Endpoints.V1.Labels;

public class UpdateLabelEndpoint : RouteBaseSync.WithRequest<TimeLabel.TimeLabelDto>.WithActionResult
{
    private readonly MainAppDatabase _database;

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

    /// <summary>
    /// Update a time entry label.
    /// </summary>
    /// <param name="labelTimeLabelDto"></param>
    /// <returns></returns>
    [ApiVersion(ApiSpecV1.VERSION_STRING)]
    [BasicAuthentication(AppConstants.TOKEN_ALLOW_UPDATE)]
    [HttpPost("~/v{version:apiVersion}/labels/update")]
    public override ActionResult Handle(TimeLabel.TimeLabelDto labelTimeLabelDto) {
        var label = _database.TimeLabels
            .Where(c => c.UserId == LoggedInUser.Id)
            .SingleOrDefault(c => c.Id == labelTimeLabelDto.Id);
        if (label == default) {
            return NotFound();
        }

        if (LoggedInUser.Id != label.UserId) {
            return Forbid();
        }

        label.Name = labelTimeLabelDto.Name;
        label.Color = labelTimeLabelDto.Color;
        _database.SaveChanges();
        return Ok();
    }
}