summaryrefslogtreecommitdiffstats
path: root/src/server/Api/V1/Entries/UpdateEntryRoute.cs
blob: 96c60fe1611c8a4224a925f6a7b5bff16181132e (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
39
40
41
42
43
44
45
using System.Security.Cryptography;
using IOL.BookmarkThing.Server.Api.V1.Entries.Dtos;

namespace IOL.BookmarkThing.Server.Api.V1.Entries;

public class UpdateEntryRoute : RouteBaseV1Sync.WithRequest<UpdateEntryRequest>.WithActionResult<EntryDto>
{
	private readonly AppDbContext _context;

	public UpdateEntryRoute(AppDbContext context) {
		_context = context;
	}


	/// <summary>
	/// Update an entry
	/// </summary>
	/// <param name="entryToUpdate">The entry to update</param>
	/// <response code="200">Entry updated successfully</response>
	/// <response code="400">Invalid entry</response>
	[ProducesResponseType(typeof(EntryDto), 200)]
	[ProducesResponseType(typeof(List<ErrorResult>), 400)]
	[ProducesResponseType(typeof(ErrorResult), 404)]
	[ApiVersion(ApiSpecV1.VERSION_STRING)]
	[HttpPost("~/v{version:apiVersion}/entries/update")]
	public override ActionResult<EntryDto> Handle(UpdateEntryRequest entryToUpdate) {
		var entry = _context.Entries.SingleOrDefault(c => c.Id == entryToUpdate.Id && c.UserId == LoggedInUser.Id);
		if (entry == default) {
			return NotFound(new ErrorResult("Entry does not exist"));
		}

		var errors = entryToUpdate.GetErrors();
		if (errors.Count != 0) {
			return BadRequest(errors);
		}

		entry.Description = entry.Description;
		entry.Tags = entry.Tags;
		entry.Url = entry.Url;

		_context.Update(entry);
		_context.SaveChanges();
		return Ok(new EntryDto(entry));
	}
}