aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/Endpoints/V1/Categories
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/Endpoints/V1/Categories')
-rw-r--r--server/src/Endpoints/V1/Categories/CreateCategoryRoute.cs43
-rw-r--r--server/src/Endpoints/V1/Categories/DeleteCategoryRoute.cs38
-rw-r--r--server/src/Endpoints/V1/Categories/GetCategoriesRoute.cs35
-rw-r--r--server/src/Endpoints/V1/Categories/UpdateCategoryRoute.cs39
4 files changed, 155 insertions, 0 deletions
diff --git a/server/src/Endpoints/V1/Categories/CreateCategoryRoute.cs b/server/src/Endpoints/V1/Categories/CreateCategoryRoute.cs
new file mode 100644
index 0000000..fac2b5e
--- /dev/null
+++ b/server/src/Endpoints/V1/Categories/CreateCategoryRoute.cs
@@ -0,0 +1,43 @@
+namespace IOL.GreatOffice.Api.Endpoints.V1.Categories;
+
+public class CreateCategoryRoute : RouteBaseSync.WithRequest<TimeCategory.TimeCategoryDto>.WithActionResult<TimeCategory.TimeCategoryDto>
+{
+ private readonly AppDbContext _context;
+
+ public CreateCategoryRoute(AppDbContext context) {
+ _context = context;
+ }
+
+ /// <summary>
+ /// Create a new time entry category.
+ /// </summary>
+ /// <param name="categoryTimeCategoryDto"></param>
+ /// <returns></returns>
+ [ApiVersion(ApiSpecV1.VERSION_STRING)]
+ [BasicAuthentication(AppConstants.TOKEN_ALLOW_CREATE)]
+ [HttpPost("~/v{version:apiVersion}/categories/create")]
+ [ProducesResponseType(200, Type = typeof(TimeCategory.TimeCategoryDto))]
+ public override ActionResult<TimeCategory.TimeCategoryDto> Handle(TimeCategory.TimeCategoryDto categoryTimeCategoryDto) {
+ var duplicate = _context.TimeCategories
+ .Where(c => c.UserId == LoggedInUser.Id)
+ .Any(c => c.Name.Trim() == categoryTimeCategoryDto.Name.Trim());
+ if (duplicate) {
+ var category = _context.TimeCategories
+ .Where(c => c.UserId == LoggedInUser.Id)
+ .SingleOrDefault(c => c.Name.Trim() == categoryTimeCategoryDto.Name.Trim());
+ if (category != default) {
+ return Ok(category.AsDto);
+ }
+ }
+
+ var newCategory = new TimeCategory(LoggedInUser.Id) {
+ Name = categoryTimeCategoryDto.Name.Trim(),
+ Color = categoryTimeCategoryDto.Color
+ };
+
+ _context.TimeCategories.Add(newCategory);
+ _context.SaveChanges();
+ categoryTimeCategoryDto.Id = newCategory.Id;
+ return Ok(categoryTimeCategoryDto);
+ }
+}
diff --git a/server/src/Endpoints/V1/Categories/DeleteCategoryRoute.cs b/server/src/Endpoints/V1/Categories/DeleteCategoryRoute.cs
new file mode 100644
index 0000000..3d438a0
--- /dev/null
+++ b/server/src/Endpoints/V1/Categories/DeleteCategoryRoute.cs
@@ -0,0 +1,38 @@
+namespace IOL.GreatOffice.Api.Endpoints.V1.Categories;
+
+public class DeleteCategoryRoute : RouteBaseSync.WithRequest<Guid>.WithActionResult
+{
+ private readonly AppDbContext _context;
+
+ public DeleteCategoryRoute(AppDbContext context) {
+ _context = context;
+ }
+
+ /// <summary>
+ /// Delete a time entry category.
+ /// </summary>
+ /// <param name="id"></param>
+ /// <returns></returns>
+ [ApiVersion(ApiSpecV1.VERSION_STRING)]
+ [BasicAuthentication(AppConstants.TOKEN_ALLOW_DELETE)]
+ [HttpDelete("~/v{version:apiVersion}/categories/{id:guid}/delete")]
+ [ProducesResponseType(200)]
+ [ProducesResponseType(404)]
+ public override ActionResult Handle(Guid id) {
+ var category = _context.TimeCategories
+ .Where(c => c.UserId == LoggedInUser.Id)
+ .SingleOrDefault(c => c.Id == id);
+
+ if (category == default) {
+ return NotFound();
+ }
+
+ var entries = _context.TimeEntries
+ .Include(c => c.Category)
+ .Where(c => c.Category.Id == category.Id);
+ _context.TimeEntries.RemoveRange(entries);
+ _context.TimeCategories.Remove(category);
+ _context.SaveChanges();
+ return Ok();
+ }
+}
diff --git a/server/src/Endpoints/V1/Categories/GetCategoriesRoute.cs b/server/src/Endpoints/V1/Categories/GetCategoriesRoute.cs
new file mode 100644
index 0000000..a40a832
--- /dev/null
+++ b/server/src/Endpoints/V1/Categories/GetCategoriesRoute.cs
@@ -0,0 +1,35 @@
+namespace IOL.GreatOffice.Api.Endpoints.V1.Categories;
+
+/// <inheritdoc />
+public class GetCategoriesRoute : RouteBaseSync.WithoutRequest.WithActionResult<List<TimeCategory.TimeCategoryDto>>
+{
+ private readonly AppDbContext _context;
+
+ /// <inheritdoc />
+ public GetCategoriesRoute(AppDbContext context) {
+ _context = context;
+ }
+
+ /// <summary>
+ /// Get a minimal list of time entry categories.
+ /// </summary>
+ /// <returns></returns>
+ [ApiVersion(ApiSpecV1.VERSION_STRING)]
+ [ProducesResponseType(200, Type = typeof(List<TimeCategory.TimeCategoryDto>))]
+ [ProducesResponseType(204)]
+ [BasicAuthentication(AppConstants.TOKEN_ALLOW_READ)]
+ [HttpGet("~/v{version:apiVersion}/categories")]
+ public override ActionResult<List<TimeCategory.TimeCategoryDto>> Handle() {
+ var categories = _context.TimeCategories
+ .Where(c => c.UserId == LoggedInUser.Id)
+ .OrderByDescending(c => c.CreatedAt)
+ .Select(c => c.AsDto)
+ .ToList();
+
+ if (categories.Count == 0) {
+ return NoContent();
+ }
+
+ return Ok(categories);
+ }
+}
diff --git a/server/src/Endpoints/V1/Categories/UpdateCategoryRoute.cs b/server/src/Endpoints/V1/Categories/UpdateCategoryRoute.cs
new file mode 100644
index 0000000..ca7dfdf
--- /dev/null
+++ b/server/src/Endpoints/V1/Categories/UpdateCategoryRoute.cs
@@ -0,0 +1,39 @@
+namespace IOL.GreatOffice.Api.Endpoints.V1.Categories;
+
+public class UpdateCategoryRoute : RouteBaseSync.WithRequest<TimeCategory.TimeCategoryDto>.WithActionResult
+{
+ private readonly AppDbContext _context;
+
+ public UpdateCategoryRoute(AppDbContext context) {
+ _context = context;
+ }
+
+ /// <summary>
+ /// Update a time entry category.
+ /// </summary>
+ /// <param name="categoryTimeCategoryDto"></param>
+ /// <returns></returns>
+ [ApiVersion(ApiSpecV1.VERSION_STRING)]
+ [BasicAuthentication(AppConstants.TOKEN_ALLOW_UPDATE)]
+ [HttpPost("~/v{version:apiVersion}/categories/update")]
+ [ProducesResponseType(200)]
+ [ProducesResponseType(404)]
+ [ProducesResponseType(403)]
+ public override ActionResult Handle(TimeCategory.TimeCategoryDto categoryTimeCategoryDto) {
+ var category = _context.TimeCategories
+ .Where(c => c.UserId == LoggedInUser.Id)
+ .SingleOrDefault(c => c.Id == categoryTimeCategoryDto.Id);
+ if (category == default) {
+ return NotFound();
+ }
+
+ if (LoggedInUser.Id != category.UserId) {
+ return Forbid();
+ }
+
+ category.Name = categoryTimeCategoryDto.Name;
+ category.Color = categoryTimeCategoryDto.Color;
+ _context.SaveChanges();
+ return Ok();
+ }
+}