diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2022-01-22 22:43:38 +0100 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2022-01-22 22:43:38 +0100 |
| commit | 88110f536f9c3843ecf5016122e101f8a424af77 (patch) | |
| tree | e8be4e77ccfb5ad37f49f89adad59ff12b4c85ea /src/server/Api/V1/Entries/CreateEntryRoute.cs | |
| download | bookmark-thing-88110f536f9c3843ecf5016122e101f8a424af77.tar.xz bookmark-thing-88110f536f9c3843ecf5016122e101f8a424af77.zip | |
Initial commit
Diffstat (limited to 'src/server/Api/V1/Entries/CreateEntryRoute.cs')
| -rw-r--r-- | src/server/Api/V1/Entries/CreateEntryRoute.cs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/server/Api/V1/Entries/CreateEntryRoute.cs b/src/server/Api/V1/Entries/CreateEntryRoute.cs new file mode 100644 index 0000000..ebe49fc --- /dev/null +++ b/src/server/Api/V1/Entries/CreateEntryRoute.cs @@ -0,0 +1,34 @@ +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) { + 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)); + } +} |
