Skip to content

Commit

Permalink
Implement UpdatedAt, ArchivedAt and DeletedAt
Browse files Browse the repository at this point in the history
  • Loading branch information
Dentsor committed Feb 25, 2022
1 parent f9febc6 commit 5d51d95
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 17 deletions.
15 changes: 11 additions & 4 deletions SpeiderappAPI/Controllers/BadgeController.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -25,16 +30,18 @@ public BadgeController(ApiContext context, IMapper mapper)
[HttpGet]
public async Task<ActionResult<IEnumerable<BadgeDto>>> GetBadges()
{
return _mapper.Map<List<BadgeDto>>(
await _context.Badges.ToListAsync()
);
return await _context.Badges
.ExcludeDeleted().ExcludeArchived()
.ProjectTo<BadgeDto>(_mapper.ConfigurationProvider).ToListAsync();
}

// GET: api/Badge/<id>
[HttpGet("{id:long}")]
public async Task<ActionResult<BadgeDto>> 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)
{
Expand Down
13 changes: 10 additions & 3 deletions SpeiderappAPI/Controllers/RequirementController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using SpeiderappAPI.Database;
using SpeiderappAPI.DataTransferObjects;
using SpeiderappAPI.Models;
using SpeiderappAPI.Models.Interfaces;

namespace SpeiderappAPI.Controllers
{
Expand All @@ -28,22 +29,28 @@ public RequirementController(ApiContext context, IMapper mapper)
[HttpGet]
public async Task<ActionResult<IEnumerable<RequirementDto>>> GetRequirements()
{
return await _context.Requirements.Where(requirement => requirement.RequirementType == nameof(Requirement))
return await _context.Requirements
.ExcludeDeleted().ExcludeArchived()
.Where(requirement => requirement.RequirementType == nameof(Requirement))
.ProjectTo<RequirementDto>(_mapper.ConfigurationProvider).ToListAsync();
}

// GET: api/Requirement/all
[HttpGet("all")]
public async Task<ActionResult<IEnumerable<RequirementDto>>> GetRequirementsAll()
{
return await _context.Requirements.ProjectTo<RequirementDto>(_mapper.ConfigurationProvider).ToListAsync();
return await _context.Requirements
.ExcludeDeleted().ExcludeArchived()
.ProjectTo<RequirementDto>(_mapper.ConfigurationProvider).ToListAsync();
}

// Get: api/Requirement/<id>
[HttpGet("{id:long}")]
public async Task<ActionResult<RequirementDto>> 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)
{
Expand Down
10 changes: 8 additions & 2 deletions SpeiderappAPI/Controllers/ResourceController.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SpeiderappAPI.Database;
using SpeiderappAPI.DataTransferObjects;
using SpeiderappAPI.Models.Interfaces;

namespace SpeiderappAPI.Controllers
{
Expand All @@ -26,14 +28,18 @@ public ResourceController(ApiContext context, IMapper mapper)
[HttpGet]
public async Task<ActionResult<IEnumerable<ResourceDto>>> GetResource()
{
return await _context.Resources.ProjectTo<ResourceDto>(_mapper.ConfigurationProvider).ToListAsync();
return await _context.Resources
.ExcludeDeleted()
.ProjectTo<ResourceDto>(_mapper.ConfigurationProvider).ToListAsync();
}

// GET: api/Resource/<id>
[HttpGet("{id:long}")]
public async Task<ActionResult<ResourceDto>> 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)
{
Expand Down
10 changes: 10 additions & 0 deletions SpeiderappAPI/Models/Interfaces/IArchivable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Linq;

namespace SpeiderappAPI.Models.Interfaces
{
public interface IArchivable
{
public DateTime? ArchivedAt { get; set; }
}
}
19 changes: 19 additions & 0 deletions SpeiderappAPI/Models/Interfaces/IArchivableExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Linq;

namespace SpeiderappAPI.Models.Interfaces
{
public static class IArchivableExtensions
{
public static IQueryable<TArchivable> ExcludeArchived<TArchivable>(this IQueryable<TArchivable> queryable)
where TArchivable : IArchivable
{
return queryable.Where(e => e.ArchivedAt == null);
}

public static IQueryable<TArchivable> OnlyArchived<TArchivable>(IQueryable<TArchivable> queryable)
where TArchivable : IArchivable
{
return queryable.Where(e => e.ArchivedAt != null);
}
}
}
9 changes: 9 additions & 0 deletions SpeiderappAPI/Models/Interfaces/ISoftDeletable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace SpeiderappAPI.Models.Interfaces
{
public interface ISoftDeletable
{
public DateTime? DeletedAt { get; set; }
}
}
19 changes: 19 additions & 0 deletions SpeiderappAPI/Models/Interfaces/ISoftDeleteExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Linq;

namespace SpeiderappAPI.Models.Interfaces
{
public static class ISoftDeleteExtensions
{
public static IQueryable<TSoftDeletable> ExcludeDeleted<TSoftDeletable>(
this IQueryable<TSoftDeletable> queryable) where TSoftDeletable : ISoftDeletable
{
return queryable.Where(e => e.DeletedAt == null);
}

public static IQueryable<TSoftDeletable> OnlyDeleted<TSoftDeletable>(this IQueryable<TSoftDeletable> queryable)
where TSoftDeletable : ISoftDeletable
{
return queryable.Where(e => e.DeletedAt != null);
}
}
}
9 changes: 9 additions & 0 deletions SpeiderappAPI/Models/Interfaces/IUpdatable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace SpeiderappAPI.Models.Interfaces
{
public interface IUpdatable
{
public DateTime UpdatedAt { get; set; }
}
}
6 changes: 5 additions & 1 deletion SpeiderappAPI/Models/Requirement.cs
Original file line number Diff line number Diff line change
@@ -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)
{
Expand All @@ -21,6 +22,9 @@ public Requirement(string description, DateTime publishTime)
public virtual ICollection<Resource> Resources { get; set; } = null!;
public virtual ICollection<RequirementPrerequisite> RequiredBy { get; set; } = null!;
public virtual ICollection<RequirementPrerequisite> Requiring { get; set; } = null!;
public DateTime UpdatedAt { get; set; }
public DateTime? ArchivedAt { get; set; }
public DateTime? DeletedAt { get; set; }

public override string ToString()
{
Expand Down
9 changes: 7 additions & 2 deletions SpeiderappAPI/Models/Resource.cs
Original file line number Diff line number Diff line change
@@ -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)
{
Expand All @@ -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; }
}
}
8 changes: 6 additions & 2 deletions SpeiderappAPI/Models/Tag.cs
Original file line number Diff line number Diff line change
@@ -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)
{
Expand All @@ -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<TaggedWith> TaggedWiths { get; set; } = null!;
public DateTime UpdatedAt { get; set; }
public DateTime? DeletedAt { get; set; }
}
}
4 changes: 1 addition & 3 deletions SpeiderappAPI/Models/User.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;

namespace SpeiderappAPI.Models
namespace SpeiderappAPI.Models
{
public class User
{
Expand Down

0 comments on commit 5d51d95

Please sign in to comment.