Skip to content

Commit

Permalink
Avoids deleting dependent records
Browse files Browse the repository at this point in the history
A dependent cannot be removed if he/she has medical care, otherwise the data would be inconsistent. For this reason, soft delete was applied in the Dependents table.
  • Loading branch information
MrDave1999 committed Aug 4, 2022
1 parent 8284610 commit 9a93908
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/DataAccess/AppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.ApplyConfiguration(new RoleConfiguration());
modelBuilder.ApplyConfiguration(new GenderConfiguration());
modelBuilder.ApplyConfiguration(new KinshipConfiguration());
modelBuilder.ApplyConfiguration(new DependentConfiguration());
}
}
2 changes: 1 addition & 1 deletion src/Features/Dependents/Dependent.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace DentallApp.Features.Dependents;

public class Dependent : ModelBase
public class Dependent : ModelWithStatus
{
public int UserId { get; set; }
public User User { get; set; }
Expand Down
9 changes: 9 additions & 0 deletions src/Features/Dependents/DependentConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace DentallApp.Features.Dependents;

public class DependentConfiguration : IEntityTypeConfiguration<Dependent>
{
public void Configure(EntityTypeBuilder<Dependent> builder)
{
builder.HasQueryFilter(dependent => dependent.StatusId == StatusId.Active);
}
}
3 changes: 2 additions & 1 deletion src/Features/Dependents/DependentMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public static Person MapToPerson(this DependentInsertDto dependentDto)
public static Dependent MapToDependent(this DependentInsertDto dependentDto)
=> new()
{
KinshipId = dependentDto.KinshipId
KinshipId = dependentDto.KinshipId,
StatusId = StatusId.Active
};

public static DependentGetDto MapToDependentGetDto(this Dependent dependent)
Expand Down
3 changes: 1 addition & 2 deletions src/Features/Dependents/DependentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public async Task<Response> RemoveDependentAsync(int dependentId, int userId)
if (dependent.UserId != userId)
return new Response(ResourceFromAnotherUserMessage);

_unitOfWork.DependentRepository.Delete(dependent);
_unitOfWork.PersonRepository.Delete(new Person { Id = dependent.PersonId });
dependent.StatusId = StatusId.Inactive;
await _unitOfWork.SaveChangesAsync();

return new Response
Expand Down
1 change: 1 addition & 0 deletions src/Features/StatusManager/Status.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ public class Status : ModelBase
{
public string Name { get; set; }
public ICollection<GeneralTreatment> GeneralTreatments { get; set; }
public ICollection<Dependent> Dependents { get; set; }
}

0 comments on commit 9a93908

Please sign in to comment.