blob: b502e4a36e9d755b6bf51700a4f073a1b8ea1363 (
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
|
using IOL.BookmarkThing.Server.Api.V1.Entries.Dtos;
namespace IOL.BookmarkThing.Server.Api.V1.Entries;
public class CreateEntryRoute : RouteBaseV1Sync.WithRequest<CreateEntryRequest>.WithActionResult<EntryDto>
{
private readonly AppDbContext _context;
public CreateEntryRoute(AppDbContext context) {
_context = context;
}
/// <summary>
/// Create a new entry
/// </summary>
/// <param name="entry">The entry to create</param>
/// <response code="200">Entry created successfully</response>
/// <response code="400">Invalid entry</response>
[ProducesResponseType(typeof(EntryDto), 200)]
[ProducesResponseType(typeof(List<ErrorResult>), 400)]
[ApiVersion(ApiSpecV1.VERSION_STRING)]
[HttpPost("~/v{version:apiVersion}/entries/create")]
public override ActionResult<EntryDto> Handle(CreateEntryRequest entry) {
if (IsApiCall() && !HasApiPermission(Constants.TOKEN_ALLOW_CREATE)) {
return StatusCode(403, "Your token does not permit access to this resource");
}
var errors = entry.GetErrors();
if (errors.Count != 0) {
return BadRequest(errors);
}
var dbEntry = entry.AsDbEntity(LoggedInUser.Id);
_context.Entries.Add(dbEntry);
_context.SaveChanges();
return Ok(new EntryDto(dbEntry));
}
}
|