Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Romanchuk / Change soft delete. Episode 2 #1249

Merged
merged 4 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

namespace OutOfSchool.Services.Models;

public class BlockedProviderParent : IKeyedEntity<Guid>
public class BlockedProviderParent : IKeyedEntity<Guid>, ISoftDeleted
{
public Guid Id { get; set; }

public bool IsDeleted { get; set; }

[Required]
public Guid ParentId { get; set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using OutOfSchool.Common;
using System;
using System;
using System.ComponentModel.DataAnnotations;
using System.Reflection.Metadata;
using OutOfSchool.Common;

namespace OutOfSchool.Services.Models.ChatWorkshop;

public class ChatMessageWorkshop : IKeyedEntity<Guid>
public class ChatMessageWorkshop : IKeyedEntity<Guid>, ISoftDeleted
{
public Guid Id { get; set; }

public bool IsDeleted { get; set; }

[Required]
public Guid ChatRoomId { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

namespace OutOfSchool.Services.Models.ChatWorkshop;

public class ChatRoomWorkshop : IKeyedEntity<Guid>
public class ChatRoomWorkshop : IKeyedEntity<Guid>, ISoftDeleted
{
public Guid Id { get; set; }

public bool IsDeleted { get; set; }

[Required]
public Guid WorkshopId { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

namespace OutOfSchool.Services.Models;

public class CompanyInformation : IKeyedEntity<Guid>
public class CompanyInformation : IKeyedEntity<Guid>, ISoftDeleted
{
public Guid Id { get; set; }

public bool IsDeleted { get; set; }

[MaxLength(200)]
public string Title { get; set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace OutOfSchool.Services.Models.Configurations;

internal class BlockedProviderParentConfiguration : IEntityTypeConfiguration<BlockedProviderParent>
{
public void Configure(EntityTypeBuilder<BlockedProviderParent> builder)
{
builder.HasIndex(x => x.IsDeleted);

builder.Property(x => x.IsDeleted).HasDefaultValue(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public void Configure(EntityTypeBuilder<ChatMessageWorkshop> builder)
{
builder.HasKey(x => x.Id);

builder.HasIndex(x => x.IsDeleted);

builder.Property(x => x.IsDeleted).HasDefaultValue(false);

builder
.HasOne(m => m.ChatRoom)
.WithMany(r => r.ChatMessages)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public void Configure(EntityTypeBuilder<ChatRoomWorkshop> builder)
{
builder.HasKey(x => x.Id);

builder.HasIndex(x => x.IsDeleted);

builder.Property(x => x.IsDeleted).HasDefaultValue(false);

builder
.HasOne(r => r.Parent)
.WithMany(p => p.ChatRooms)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace OutOfSchool.Services.Models.Configurations;

internal class CompanyInformationConfiguration : IEntityTypeConfiguration<CompanyInformation>
{
public void Configure(EntityTypeBuilder<CompanyInformation> builder)
{
builder.HasIndex(x => x.IsDeleted);

builder.Property(x => x.IsDeleted).HasDefaultValue(false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace OutOfSchool.Services.Models.Configurations;

internal class SocialGroupConfiguration : IEntityTypeConfiguration<SocialGroup>
{
public void Configure(EntityTypeBuilder<SocialGroup> builder)
{
builder.HasIndex(x => x.IsDeleted);

builder.Property(x => x.IsDeleted).HasDefaultValue(false);
}
}
4 changes: 3 additions & 1 deletion OutOfSchool/OutOfSchool.DataAccess/Models/SocialGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

namespace OutOfSchool.Services.Models;

public class SocialGroup : IKeyedEntity<long>
public class SocialGroup : IKeyedEntity<long>, ISoftDeleted
{
public long Id { get; set; }

public bool IsDeleted { get; set; }

[Required]
[DataType(DataType.Text)]
[MaxLength(100)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,15 @@ private void ApplySoftDelete(ModelBuilder builder)
.ApplySoftDelete<User>()
.ApplySoftDelete<Address>()
.ApplySoftDelete<Application>()
.ApplySoftDelete<BlockedProviderParent>()
.ApplySoftDelete<ChatMessageWorkshop>()
.ApplySoftDelete<ChatRoomWorkshop>()
.ApplySoftDelete<Child>()
.ApplySoftDelete<CATOTTG>()
.ApplySoftDelete<CompanyInformation>()
.ApplySoftDelete<CompanyInformationItem>()
.ApplySoftDelete<DateTimeRange>()
.ApplySoftDelete<Parent>()
.ApplySoftDelete<Provider>()
.ApplySoftDelete<ProviderAdmin>()
.ApplySoftDelete<ProviderSectionItem>()
.ApplySoftDelete<RegionAdmin>()
.ApplySoftDelete<SocialGroup>()
.ApplySoftDelete<Workshop>()
.ApplySoftDelete<WorkshopDescriptionItem>();
}
Expand Down
3 changes: 3 additions & 0 deletions OutOfSchool/OutOfSchool.DataAccess/OutOfSchoolDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,12 @@ protected override void OnModelCreating(ModelBuilder builder)
builder.ApplyConfiguration(new AddressConfiguration());
builder.ApplyConfiguration(new ApplicationConfiguration());
builder.ApplyConfiguration(new AverageRatingConfiguration());
builder.ApplyConfiguration(new BlockedProviderParentConfiguration());
builder.ApplyConfiguration(new ChatMessageWorkshopConfiguration());
builder.ApplyConfiguration(new ChatRoomWorkshopConfiguration());
builder.ApplyConfiguration(new ChildConfiguration());
builder.ApplyConfiguration(new CodeficatorConfiguration());
builder.ApplyConfiguration(new CompanyInformationConfiguration());
builder.ApplyConfiguration(new DirectionConfiguration());
builder.ApplyConfiguration(new EntityImagesConfiguration<Provider>());
builder.ApplyConfiguration(new EntityImagesConfiguration<Workshop>());
Expand All @@ -146,6 +148,7 @@ protected override void OnModelCreating(ModelBuilder builder)
builder.ApplyConfiguration(new ProviderAdminConfiguration());
builder.ApplyConfiguration(new QuartzJobConfiguration());
builder.ApplyConfiguration(new RatingConfiguration());
builder.ApplyConfiguration(new SocialGroupConfiguration());
builder.ApplyConfiguration(new TeacherConfiguration());
builder.ApplyConfiguration(new WorkshopConfiguration());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace OutOfSchool.Services.Repository;

public class BlockedProviderParentRepository : SensitiveEntityRepository<BlockedProviderParent>, IBlockedProviderParentRepository
public class BlockedProviderParentRepository : SensitiveEntityRepositorySoftDeleted<BlockedProviderParent>, IBlockedProviderParentRepository
{
private readonly OutOfSchoolDbContext db;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ private Task<List<ChatRoomWorkshopForChatList>> GetByParametersAsync(Expression<
{
var query = dbSet
.Where(condition)
.Where(x => !x.IsDeleted)
.Select(item => new ChatRoomWorkshopForChatList()
{
Id = item.Id,
Expand All @@ -102,7 +103,7 @@ private Task<List<ChatRoomWorkshopForChatList>> GetByParametersAsync(Expression<
Email = item.Parent.User.Email,
PhoneNumber = item.Parent.User.PhoneNumber,
},
LastMessage = item.ChatMessages.Where(mess => mess.CreatedDateTime == item.ChatMessages.Max(m => m.CreatedDateTime))
LastMessage = item.ChatMessages.Where(mess => !mess.IsDeleted && mess.CreatedDateTime == item.ChatMessages.Where(m => !m.IsDeleted).Max(m => m.CreatedDateTime))
.Select(message => new ChatMessageInfoForChatList()
{
Id = message.Id,
Expand All @@ -113,7 +114,7 @@ private Task<List<ChatRoomWorkshopForChatList>> GetByParametersAsync(Expression<
ReadDateTime = message.ReadDateTime,
})
.FirstOrDefault(),
NotReadByCurrentUserMessagesCount = item.ChatMessages.Count(mess => mess.ReadDateTime == null && (mess.SenderRoleIsProvider != searchMessagesForProvider)),
NotReadByCurrentUserMessagesCount = item.ChatMessages.Count(mess => !mess.IsDeleted && mess.ReadDateTime == null && (mess.SenderRoleIsProvider != searchMessagesForProvider)),
})
.OrderByDescending(x => x.LastMessage.CreatedDateTime);
return query.ToListAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace OutOfSchool.Services.Repository;

public interface IBlockedProviderParentRepository : ISensitiveEntityRepository<BlockedProviderParent>
public interface IBlockedProviderParentRepository : ISensitiveEntityRepositorySoftDeleted<BlockedProviderParent>
{
/// <summary>
/// Create entity BlockedProviderParent. Update dependent entities (IsBlocked = true).
Expand Down
8 changes: 4 additions & 4 deletions OutOfSchool/OutOfSchool.WebApi.Tests/EntityRepositoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void GetById_Id_ReturnEntity()
{
using var context = new OutOfSchoolDbContext(UnitTestHelper.GetUnitTestDbOptions());
{
var repository = new EntityRepository<long, SocialGroup>(context);
var repository = new EntityRepositorySoftDeleted<long, SocialGroup>(context);

// Act
var group = repository.GetById(1).Result;
Expand Down Expand Up @@ -103,7 +103,7 @@ public async Task Delete_DeleteEntity_DeleteFromDatabaseAsync()
{
using var context = new OutOfSchoolDbContext(UnitTestHelper.GetUnitTestDbOptions());
{
var repository = new EntityRepository<long, SocialGroup>(context);
var repository = new EntityRepositorySoftDeleted<long, SocialGroup>(context);
SocialGroup socialGroup = new SocialGroup { Id = 1, Name = "sg1" };

// Act
Expand All @@ -120,7 +120,7 @@ public void GetAll_ReturnAllValues()
{
using var context = new OutOfSchoolDbContext(UnitTestHelper.GetUnitTestDbOptions());
{
var repository = new EntityRepository<long, SocialGroup>(context);
var repository = new EntityRepositorySoftDeleted<long, SocialGroup>(context);

// Act
var socialGroups = repository.GetAll();
Expand All @@ -135,7 +135,7 @@ public void Update_UpatedInfo_UpdateEntityInDatabase()
{
using var context = new OutOfSchoolDbContext(UnitTestHelper.GetUnitTestDbOptions());
{
var repository = new EntityRepository<long, SocialGroup>(context);
var repository = new EntityRepositorySoftDeleted<long, SocialGroup>(context);

// Act
SocialGroup socialGroup = new SocialGroup { Id = 2, Name = "sg22" };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class ChatMessageWorkshopServiceTests
WorkshopId = Guid.NewGuid(),
};

private IEntityRepository<Guid, ChatMessageWorkshop> messageRepository;
private IEntityRepositorySoftDeleted<Guid, ChatMessageWorkshop> messageRepository;
private Mock<IChatRoomWorkshopService> roomServiceMock;
private Mock<IHubContext<ChatWorkshopHub>> workshopHub;
private Mock<ILogger<ChatMessageWorkshopService>> loggerMock;
Expand All @@ -60,7 +60,7 @@ public void SetUp()
options = builder.Options;
dbContext = new OutOfSchoolDbContext(options);

messageRepository = new EntityRepository<Guid, ChatMessageWorkshop>(dbContext);
messageRepository = new EntityRepositorySoftDeleted<Guid, ChatMessageWorkshop>(dbContext);
roomServiceMock = new Mock<IChatRoomWorkshopService>();
workshopHub = new Mock<IHubContext<ChatWorkshopHub>>();
loggerMock = new Mock<ILogger<ChatMessageWorkshopService>>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class ChatRoomWorkshopServiceTests

private static ChatRoomWorkshop[] rooms;

private IEntityRepository<Guid, ChatRoomWorkshop> roomRepository;
private IEntityRepositorySoftDeleted<Guid, ChatRoomWorkshop> roomRepository;
private Mock<IChatRoomWorkshopModelForChatListRepository> roomWithSpecialModelRepositoryMock;
private Mock<ILogger<ChatRoomWorkshopService>> loggerMock;
private IMapper mapper;
Expand Down Expand Up @@ -84,7 +84,7 @@ public void SetUp()
options = builder.Options;
dbContext = new OutOfSchoolDbContext(options);

roomRepository = new EntityRepository<Guid, ChatRoomWorkshop>(dbContext);
roomRepository = new EntityRepositorySoftDeleted<Guid, ChatRoomWorkshop>(dbContext);
roomWithSpecialModelRepositoryMock = new Mock<IChatRoomWorkshopModelForChatListRepository>();
loggerMock = new Mock<ILogger<ChatRoomWorkshopService>>();
mapper = TestHelper.CreateMapperInstanceOfProfileType<MappingProfile>();
Expand Down Expand Up @@ -142,13 +142,13 @@ public async Task Delete_WhenRoomExist_ShouldDeleteEntities()
{
// Arrange
var existingId = rooms[0].Id;
var roomCount = dbContext.ChatRoomWorkshops.Count();
var roomCount = dbContext.ChatRoomWorkshops.Count(x => !x.IsDeleted);

// Act
await roomService.DeleteAsync(existingId).ConfigureAwait(false);

// Assert
Assert.AreEqual(roomCount - 1, dbContext.ChatRoomWorkshops.Count());
Assert.AreEqual(roomCount - 1, dbContext.ChatRoomWorkshops.Count(x => !x.IsDeleted));
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class CompanyInformationServiceTests
{
private DbContextOptions<OutOfSchoolDbContext> options;
private OutOfSchoolDbContext context;
private ISensitiveEntityRepository<CompanyInformation> repository;
private ISensitiveEntityRepositorySoftDeleted<CompanyInformation> repository;
private ICompanyInformationService service;
private Mock<ILogger<CompanyInformationService>> logger;
private Mock<IMapper> mapper;
Expand All @@ -39,7 +39,7 @@ public void SetUp()

logger = new Mock<ILogger<CompanyInformationService>>();
mapper = new Mock<IMapper>();
repository = new SensitiveEntityRepository<CompanyInformation>(context);
repository = new SensitiveEntityRepositorySoftDeleted<CompanyInformation>(context);
service = new CompanyInformationService(repository, logger.Object, mapper.Object);

SeedDatabase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class WorkshopServiceTests
private IWorkshopService workshopService;
private Mock<IWorkshopRepository> workshopRepository;
private Mock<IEntityRepository<long, DateTimeRange>> dateTimeRangeRepository;
private Mock<IEntityRepository<Guid, ChatRoomWorkshop>> roomRepository;
private Mock<IEntityRepositorySoftDeleted<Guid, ChatRoomWorkshop>> roomRepository;
private Mock<ITeacherService> teacherService;
private Mock<ILogger<WorkshopService>> logger;
private Mock<IMapper> mapperMock;
Expand All @@ -47,7 +47,7 @@ public void SetUp()
{
workshopRepository = new Mock<IWorkshopRepository>();
dateTimeRangeRepository = new Mock<IEntityRepository<long, DateTimeRange>>();
roomRepository = new Mock<IEntityRepository<Guid, ChatRoomWorkshop>>();
roomRepository = new Mock<IEntityRepositorySoftDeleted<Guid, ChatRoomWorkshop>>();
teacherService = new Mock<ITeacherService>();
logger = new Mock<ILogger<WorkshopService>>();
mapperMock = new Mock<IMapper>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class SocialGroupServiceTests
{
private ISocialGroupService service;
private OutOfSchoolDbContext context;
private IEntityRepository<long, SocialGroup> repository;
private IEntityRepositorySoftDeleted<long, SocialGroup> repository;
private Mock<IStringLocalizer<SharedResource>> localizer;
private Mock<ILogger<SocialGroupService>> logger;
private DbContextOptions<OutOfSchoolDbContext> options;
Expand All @@ -40,7 +40,7 @@ public void SetUp()
options = builder.Options;
context = new OutOfSchoolDbContext(options);
localizer = new Mock<IStringLocalizer<SharedResource>>();
repository = new EntityRepository<long, SocialGroup>(context);
repository = new EntityRepositorySoftDeleted<long, SocialGroup>(context);
logger = new Mock<ILogger<SocialGroupService>>();
mapper = TestHelper.CreateMapperInstanceOfProfileType<MappingProfile>();
service = new SocialGroupService(repository, logger.Object, localizer.Object, mapper);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using OutOfSchool.Common.PermissionsModule;
using OutOfSchool.Services.Enums;
using OutOfSchool.WebApi.Models;
using OutOfSchool.WebApi.Services;

namespace OutOfSchool.WebApi.Controllers.V1;

Expand Down
Loading
Loading