using IOL.BookmarkThing.Server.Api.V1.Entries.Dtos; namespace IOL.BookmarkThing.Server.Api.V1.Entries; public class UpdateEntryRoute : RouteBaseV1Sync.WithRequest.WithActionResult { private readonly AppDbContext _context; public UpdateEntryRoute(AppDbContext context) { _context = context; } /// /// Update an entry /// /// The entry to update /// Entry updated successfully /// Invalid entry [ProducesResponseType(typeof(EntryDto), 200)] [ProducesResponseType(typeof(List), 400)] [ProducesResponseType(typeof(ErrorResult), 404)] [ApiVersion(ApiSpecV1.VERSION_STRING)] [HttpPost("~/v{version:apiVersion}/entries/update")] public override ActionResult Handle(UpdateEntryRequest entryToUpdate) { if (IsApiCall() && !HasApiPermission(AppConstants.TOKEN_ALLOW_UPDATE)) { return StatusCode(403, "Your token does not permit access to this resource"); } 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)); } }