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 | |
| download | bookmark-thing-88110f536f9c3843ecf5016122e101f8a424af77.tar.xz bookmark-thing-88110f536f9c3843ecf5016122e101f8a424af77.zip | |
Initial commit
Diffstat (limited to 'src/server/Api/V1/Entries')
| -rw-r--r-- | src/server/Api/V1/Entries/CreateEntryRequest.cs | 27 | ||||
| -rw-r--r-- | src/server/Api/V1/Entries/CreateEntryRoute.cs | 34 | ||||
| -rw-r--r-- | src/server/Api/V1/Entries/DeleteEntryRoute.cs | 30 | ||||
| -rw-r--r-- | src/server/Api/V1/Entries/Dtos/EntryDto.cs | 16 | ||||
| -rw-r--r-- | src/server/Api/V1/Entries/GetEntriesRoute.cs | 21 | ||||
| -rw-r--r-- | src/server/Api/V1/Entries/UpdateEntryRequest.cs | 18 | ||||
| -rw-r--r-- | src/server/Api/V1/Entries/UpdateEntryRoute.cs | 45 |
7 files changed, 191 insertions, 0 deletions
diff --git a/src/server/Api/V1/Entries/CreateEntryRequest.cs b/src/server/Api/V1/Entries/CreateEntryRequest.cs new file mode 100644 index 0000000..a9c5070 --- /dev/null +++ b/src/server/Api/V1/Entries/CreateEntryRequest.cs @@ -0,0 +1,27 @@ +namespace IOL.BookmarkThing.Server.Api.V1.Entries; + +public class CreateEntryRequest +{ + public Uri Url { get; set; } + public string Description { get; set; } + public string Tags { get; set; } + + public List<ErrorResult> GetErrors() { + var errors = new List<ErrorResult>(); + if (Url == default) { + errors.Add(new ErrorResult("Url is a required field")); + } + + return errors; + } + + public Entry AsDbEntity(Guid userId) { + return new Entry { + Id = Guid.NewGuid(), + UserId = userId, + Url = Url, + Description = Description, + Tags = Tags + }; + } +} 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)); + } +} diff --git a/src/server/Api/V1/Entries/DeleteEntryRoute.cs b/src/server/Api/V1/Entries/DeleteEntryRoute.cs new file mode 100644 index 0000000..fc79049 --- /dev/null +++ b/src/server/Api/V1/Entries/DeleteEntryRoute.cs @@ -0,0 +1,30 @@ +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) { + 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(); + } +} diff --git a/src/server/Api/V1/Entries/Dtos/EntryDto.cs b/src/server/Api/V1/Entries/Dtos/EntryDto.cs new file mode 100644 index 0000000..5161373 --- /dev/null +++ b/src/server/Api/V1/Entries/Dtos/EntryDto.cs @@ -0,0 +1,16 @@ +namespace IOL.BookmarkThing.Server.Api.V1.Entries.Dtos; + +public class EntryDto +{ + public EntryDto(Entry dbEntry) { + Id = dbEntry.Id; + Url = dbEntry.Url; + Description = dbEntry.Description; + Tags = dbEntry.Tags; + } + + public Guid Id { get; set; } + public Uri Url { get; set; } + public string Description { get; set; } + public string Tags { get; set; } +} diff --git a/src/server/Api/V1/Entries/GetEntriesRoute.cs b/src/server/Api/V1/Entries/GetEntriesRoute.cs new file mode 100644 index 0000000..adadf01 --- /dev/null +++ b/src/server/Api/V1/Entries/GetEntriesRoute.cs @@ -0,0 +1,21 @@ +using IOL.BookmarkThing.Server.Api.V1.Entries.Dtos; + +namespace IOL.BookmarkThing.Server.Api.V1.Entries; + +public class GetEntriesRoute : RouteBaseV1Sync.WithoutRequest.WithActionResult<List<EntryDto>> +{ + private readonly AppDbContext _context; + + public GetEntriesRoute(AppDbContext context) { + _context = context; + } + + /// <summary> + /// Get all entries + /// </summary> + [ApiVersion(ApiSpecV1.VERSION_STRING)] + [HttpGet("~/v{version:apiVersion}/entries")] + public override ActionResult<List<EntryDto>> Handle() { + return Ok(_context.Entries.Where(c => c.UserId == LoggedInUser.Id).Select(c => new EntryDto(c))); + } +} diff --git a/src/server/Api/V1/Entries/UpdateEntryRequest.cs b/src/server/Api/V1/Entries/UpdateEntryRequest.cs new file mode 100644 index 0000000..819e2d2 --- /dev/null +++ b/src/server/Api/V1/Entries/UpdateEntryRequest.cs @@ -0,0 +1,18 @@ +namespace IOL.BookmarkThing.Server.Api.V1.Entries; + +public class UpdateEntryRequest +{ + public Guid Id { get; set; } + public Uri Url { get; set; } + public string Description { get; set; } + public string Tags { get; set; } + + public List<ErrorResult> GetErrors() { + var errors = new List<ErrorResult>(); + if (Url == default) { + errors.Add(new ErrorResult("Url is a required field")); + } + + return errors; + } +} diff --git a/src/server/Api/V1/Entries/UpdateEntryRoute.cs b/src/server/Api/V1/Entries/UpdateEntryRoute.cs new file mode 100644 index 0000000..96c60fe --- /dev/null +++ b/src/server/Api/V1/Entries/UpdateEntryRoute.cs @@ -0,0 +1,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)); + } +} |
