From a6f99d4a621a29c09bb4bf0f7fc75fe292344b39 Mon Sep 17 00:00:00 2001 From: Dentsor Date: Wed, 16 Feb 2022 23:18:16 +0100 Subject: [PATCH] Implement UpdatedAt, ArchivedAt and DeletedAt --- SpeiderappAPI/Controllers/BadgeController.cs | 15 +++++++++++---- .../Controllers/RequirementController.cs | 13 ++++++++++--- .../Controllers/ResourceController.cs | 10 ++++++++-- .../Models/Interfaces/IArchivable.cs | 10 ++++++++++ .../Interfaces/IArchivableExtensions.cs | 19 +++++++++++++++++++ .../Models/Interfaces/ISoftDeletable.cs | 9 +++++++++ .../Interfaces/ISoftDeleteExtensions.cs | 19 +++++++++++++++++++ SpeiderappAPI/Models/Interfaces/IUpdatable.cs | 9 +++++++++ SpeiderappAPI/Models/Requirement.cs | 6 +++++- SpeiderappAPI/Models/Resource.cs | 9 +++++++-- SpeiderappAPI/Models/Tag.cs | 8 ++++++-- SpeiderappAPI/Models/User.cs | 4 +--- 12 files changed, 114 insertions(+), 17 deletions(-) create mode 100644 SpeiderappAPI/Models/Interfaces/IArchivable.cs create mode 100644 SpeiderappAPI/Models/Interfaces/IArchivableExtensions.cs create mode 100644 SpeiderappAPI/Models/Interfaces/ISoftDeletable.cs create mode 100644 SpeiderappAPI/Models/Interfaces/ISoftDeleteExtensions.cs create mode 100644 SpeiderappAPI/Models/Interfaces/IUpdatable.cs diff --git a/SpeiderappAPI/Controllers/BadgeController.cs b/SpeiderappAPI/Controllers/BadgeController.cs index 85da27b..738710b 100644 --- a/SpeiderappAPI/Controllers/BadgeController.cs +++ b/SpeiderappAPI/Controllers/BadgeController.cs @@ -1,10 +1,15 @@ using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using AutoMapper; +using AutoMapper.QueryableExtensions; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; +using NuGet.Versioning; using SpeiderappAPI.Database; using SpeiderappAPI.DataTransferObjects; +using SpeiderappAPI.Models; +using SpeiderappAPI.Models.Interfaces; namespace SpeiderappAPI.Controllers { @@ -25,16 +30,18 @@ public BadgeController(ApiContext context, IMapper mapper) [HttpGet] public async Task>> GetBadges() { - return _mapper.Map>( - await _context.Badges.ToListAsync() - ); + return await _context.Badges + .ExcludeDeleted().ExcludeArchived() + .ProjectTo(_mapper.ConfigurationProvider).ToListAsync(); } // GET: api/Badge/ [HttpGet("{id:long}")] public async Task> GetBadge(long id) { - var badge = await _context.Badges.FindAsync(id); + var badge = await _context.Badges + .ExcludeDeleted().ExcludeArchived() + .Where(e => e.RequirementID == id).FirstOrDefaultAsync(); if (badge == null) { diff --git a/SpeiderappAPI/Controllers/RequirementController.cs b/SpeiderappAPI/Controllers/RequirementController.cs index 456a5a6..b1c2b63 100644 --- a/SpeiderappAPI/Controllers/RequirementController.cs +++ b/SpeiderappAPI/Controllers/RequirementController.cs @@ -8,6 +8,7 @@ using SpeiderappAPI.Database; using SpeiderappAPI.DataTransferObjects; using SpeiderappAPI.Models; +using SpeiderappAPI.Models.Interfaces; namespace SpeiderappAPI.Controllers { @@ -28,7 +29,9 @@ public RequirementController(ApiContext context, IMapper mapper) [HttpGet] public async Task>> GetRequirements() { - return await _context.Requirements.Where(requirement => requirement.RequirementType == nameof(Requirement)) + return await _context.Requirements + .ExcludeDeleted().ExcludeArchived() + .Where(requirement => requirement.RequirementType == nameof(Requirement)) .ProjectTo(_mapper.ConfigurationProvider).ToListAsync(); } @@ -36,14 +39,18 @@ public async Task>> GetRequirements() [HttpGet("all")] public async Task>> GetRequirementsAll() { - return await _context.Requirements.ProjectTo(_mapper.ConfigurationProvider).ToListAsync(); + return await _context.Requirements + .ExcludeDeleted().ExcludeArchived() + .ProjectTo(_mapper.ConfigurationProvider).ToListAsync(); } // Get: api/Requirement/ [HttpGet("{id:long}")] public async Task> GetRequirement(long id) { - var requirement = await _context.Requirements.FindAsync(id); + var requirement = await _context.Requirements + .ExcludeDeleted().ExcludeArchived() + .Where(e => e.RequirementID == id).FirstOrDefaultAsync(); if (requirement == null) { diff --git a/SpeiderappAPI/Controllers/ResourceController.cs b/SpeiderappAPI/Controllers/ResourceController.cs index 0d1e4b9..be70c2f 100644 --- a/SpeiderappAPI/Controllers/ResourceController.cs +++ b/SpeiderappAPI/Controllers/ResourceController.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using AutoMapper; using AutoMapper.QueryableExtensions; @@ -6,6 +7,7 @@ using Microsoft.EntityFrameworkCore; using SpeiderappAPI.Database; using SpeiderappAPI.DataTransferObjects; +using SpeiderappAPI.Models.Interfaces; namespace SpeiderappAPI.Controllers { @@ -26,14 +28,18 @@ public ResourceController(ApiContext context, IMapper mapper) [HttpGet] public async Task>> GetResource() { - return await _context.Resources.ProjectTo(_mapper.ConfigurationProvider).ToListAsync(); + return await _context.Resources + .ExcludeDeleted() + .ProjectTo(_mapper.ConfigurationProvider).ToListAsync(); } // GET: api/Resource/ [HttpGet("{id:long}")] public async Task> GetResource(long id) { - var resource = await _context.Resources.FindAsync(id); + var resource = await _context.Resources + .ExcludeDeleted() + .Where(e => e.ResourceID == id).FirstOrDefaultAsync(); if (resource == null) { diff --git a/SpeiderappAPI/Models/Interfaces/IArchivable.cs b/SpeiderappAPI/Models/Interfaces/IArchivable.cs new file mode 100644 index 0000000..a9c10db --- /dev/null +++ b/SpeiderappAPI/Models/Interfaces/IArchivable.cs @@ -0,0 +1,10 @@ +using System; +using System.Linq; + +namespace SpeiderappAPI.Models.Interfaces +{ + public interface IArchivable + { + public DateTime? ArchivedAt { get; set; } + } +} diff --git a/SpeiderappAPI/Models/Interfaces/IArchivableExtensions.cs b/SpeiderappAPI/Models/Interfaces/IArchivableExtensions.cs new file mode 100644 index 0000000..6abf7f7 --- /dev/null +++ b/SpeiderappAPI/Models/Interfaces/IArchivableExtensions.cs @@ -0,0 +1,19 @@ +using System.Linq; + +namespace SpeiderappAPI.Models.Interfaces +{ + public static class IArchivableExtensions + { + public static IQueryable ExcludeArchived(this IQueryable queryable) + where TArchivable : IArchivable + { + return queryable.Where(e => e.ArchivedAt == null); + } + + public static IQueryable OnlyArchived(IQueryable queryable) + where TArchivable : IArchivable + { + return queryable.Where(e => e.ArchivedAt != null); + } + } +} diff --git a/SpeiderappAPI/Models/Interfaces/ISoftDeletable.cs b/SpeiderappAPI/Models/Interfaces/ISoftDeletable.cs new file mode 100644 index 0000000..1c75e91 --- /dev/null +++ b/SpeiderappAPI/Models/Interfaces/ISoftDeletable.cs @@ -0,0 +1,9 @@ +using System; + +namespace SpeiderappAPI.Models.Interfaces +{ + public interface ISoftDeletable + { + public DateTime? DeletedAt { get; set; } + } +} diff --git a/SpeiderappAPI/Models/Interfaces/ISoftDeleteExtensions.cs b/SpeiderappAPI/Models/Interfaces/ISoftDeleteExtensions.cs new file mode 100644 index 0000000..56b61df --- /dev/null +++ b/SpeiderappAPI/Models/Interfaces/ISoftDeleteExtensions.cs @@ -0,0 +1,19 @@ +using System.Linq; + +namespace SpeiderappAPI.Models.Interfaces +{ + public static class ISoftDeleteExtensions + { + public static IQueryable ExcludeDeleted( + this IQueryable queryable) where TSoftDeletable : ISoftDeletable + { + return queryable.Where(e => e.DeletedAt == null); + } + + public static IQueryable OnlyDeleted(this IQueryable queryable) + where TSoftDeletable : ISoftDeletable + { + return queryable.Where(e => e.DeletedAt != null); + } + } +} diff --git a/SpeiderappAPI/Models/Interfaces/IUpdatable.cs b/SpeiderappAPI/Models/Interfaces/IUpdatable.cs new file mode 100644 index 0000000..f6c0e89 --- /dev/null +++ b/SpeiderappAPI/Models/Interfaces/IUpdatable.cs @@ -0,0 +1,9 @@ +using System; + +namespace SpeiderappAPI.Models.Interfaces +{ + public interface IUpdatable + { + public DateTime UpdatedAt { get; set; } + } +} diff --git a/SpeiderappAPI/Models/Requirement.cs b/SpeiderappAPI/Models/Requirement.cs index a0ec7e7..db1de1c 100644 --- a/SpeiderappAPI/Models/Requirement.cs +++ b/SpeiderappAPI/Models/Requirement.cs @@ -1,9 +1,10 @@ using System; using System.Collections.Generic; +using SpeiderappAPI.Models.Interfaces; namespace SpeiderappAPI.Models { - public class Requirement + public class Requirement : IUpdatable, IArchivable, ISoftDeletable { public Requirement(string description, DateTime publishTime) { @@ -21,6 +22,9 @@ public Requirement(string description, DateTime publishTime) public virtual ICollection Resources { get; set; } = null!; public virtual ICollection RequiredBy { get; set; } = null!; public virtual ICollection Requiring { get; set; } = null!; + public DateTime UpdatedAt { get; set; } + public DateTime? ArchivedAt { get; set; } + public DateTime? DeletedAt { get; set; } public override string ToString() { diff --git a/SpeiderappAPI/Models/Resource.cs b/SpeiderappAPI/Models/Resource.cs index dc1a89b..44368c9 100644 --- a/SpeiderappAPI/Models/Resource.cs +++ b/SpeiderappAPI/Models/Resource.cs @@ -1,6 +1,9 @@ -namespace SpeiderappAPI.Models +using System; +using SpeiderappAPI.Models.Interfaces; + +namespace SpeiderappAPI.Models { - public class Resource + public class Resource : IUpdatable, ISoftDeletable { public Resource(long requirementID, string name, string description, string location) { @@ -16,5 +19,7 @@ public Resource(long requirementID, string name, string description, string loca public string Location { get; set; } public long RequirementID { get; set; } public virtual Requirement Requirement { get; set; } = null!; + public DateTime UpdatedAt { get; set; } + public DateTime? DeletedAt { get; set; } } } diff --git a/SpeiderappAPI/Models/Tag.cs b/SpeiderappAPI/Models/Tag.cs index d3c022f..77fe441 100644 --- a/SpeiderappAPI/Models/Tag.cs +++ b/SpeiderappAPI/Models/Tag.cs @@ -1,8 +1,10 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; +using SpeiderappAPI.Models.Interfaces; namespace SpeiderappAPI.Models { - public class Tag + public class Tag : IUpdatable, ISoftDeletable { public Tag(long tagID, string value, long categoryID) { @@ -16,5 +18,7 @@ public Tag(long tagID, string value, long categoryID) public long CategoryID { get; set; } public virtual Category Category { get; set; } = null!; public virtual ICollection TaggedWiths { get; set; } = null!; + public DateTime UpdatedAt { get; set; } + public DateTime? DeletedAt { get; set; } } } diff --git a/SpeiderappAPI/Models/User.cs b/SpeiderappAPI/Models/User.cs index aa86eb9..ad001c9 100644 --- a/SpeiderappAPI/Models/User.cs +++ b/SpeiderappAPI/Models/User.cs @@ -1,6 +1,4 @@ -using System.Collections.Generic; - -namespace SpeiderappAPI.Models +namespace SpeiderappAPI.Models { public class User {