summaryrefslogtreecommitdiffstats
path: root/src/server/Api/V1/Entries/DeleteEntryRoute.cs
blob: 8b8b75c0c352ba15a6718efc46f9d6f22bac3ffc (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.BookmarkThing.Server.Api.V1.Entries;

public class DeleteEntryRoute : RouteBaseV1Sync.WithRequest<Guid>.WithActionResult
{
	private readonly AppDbContext _context;

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

	/// <summary>
	/// Delete a entry
	/// </summary>
	/// <param name="entryId">The guid id of the entry to delete</param>
	/// <response code="200">Entry deleted successfully</response>
	/// <response code="404">Entry not found</response>
	[ProducesResponseType(typeof(ErrorResult), 404)]
	[ApiVersion(ApiSpecV1.VERSION_STRING)]
	[HttpDelete("~/v{version:apiVersion}/entries/{entryId:guid}")]
	public override ActionResult Handle(Guid entryId) {
		if (IsApiCall() && !HasApiPermission(AppConstants.TOKEN_ALLOW_DELETE)) {
			return StatusCode(403, "Your token does not permit access to this resource");
		}

		var entry = _context.Entries.SingleOrDefault(c => c.Id == entryId && c.UserId == LoggedInUser.Id);
		if (entry == default) {
			return NotFound(new ErrorResult("Entry does not exist"));
		}

		_context.Remove(entry);
		_context.SaveChanges();
		return Ok();
	}
}