summaryrefslogtreecommitdiffstats
path: root/server/src/Endpoints/V1/Labels/UpdateLabelRoute.cs
blob: 30d72ecb69da099aa671fa6b9457deb44b5711af (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
37
38
namespace IOL.GreatOffice.Api.Endpoints.V1.Labels;

/// <inheritdoc />
public class UpdateLabelEndpoint : RouteBaseSync.WithRequest<TimeLabel.TimeLabelDto>.WithActionResult
{
	private readonly AppDbContext _context;

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

	/// <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 = _context.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;
		_context.SaveChanges();
		return Ok();
	}
}