Skip to content

Commit

Permalink
Disable soft delete for Directions (#1196)
Browse files Browse the repository at this point in the history
  • Loading branch information
alrom authored Aug 18, 2023
1 parent 5e9eaae commit 4f7b9f4
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace OutOfSchool.Services.Models.Configurations;

internal class DirectionConfiguration : IEntityTypeConfiguration<Direction>
{
public void Configure(EntityTypeBuilder<Direction> builder)
{
builder.HasKey(x => x.Id);

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

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

namespace OutOfSchool.Services.Models.Configurations;

Expand Down
2 changes: 2 additions & 0 deletions OutOfSchool/OutOfSchool.DataAccess/Models/Direction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ public class Direction : IKeyedEntity<long>
[MaxLength(500)]
public string Description { get; set; } = string.Empty;

public bool IsDeleted { get; set; }

public virtual List<InstitutionHierarchy> InstitutionHierarchies { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private void ApplySoftDelete(ModelBuilder builder)
.ApplySoftDelete<CompanyInformation>()
.ApplySoftDelete<CompanyInformationItem>()
.ApplySoftDelete<DateTimeRange>()
.ApplySoftDelete<Direction>()
//.ApplySoftDelete<Direction>()
//.ApplySoftDelete<Favorite>()
//.ApplySoftDelete<Institution>()
.ApplySoftDelete<InstitutionFieldDescription>()
Expand Down
1 change: 1 addition & 0 deletions OutOfSchool/OutOfSchool.DataAccess/OutOfSchoolDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ protected override void OnModelCreating(ModelBuilder builder)
builder.ApplyConfiguration(new ChatRoomWorkshopConfiguration());
builder.ApplyConfiguration(new ChildConfiguration());
builder.ApplyConfiguration(new CodeficatorConfiguration());
builder.ApplyConfiguration(new DirectionConfiguration());
builder.ApplyConfiguration(new EntityImagesConfiguration<Provider>());
builder.ApplyConfiguration(new EntityImagesConfiguration<Workshop>());
builder.ApplyConfiguration(new FavoriteConfiguration());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using OutOfSchool.Services.Models;
using OutOfSchool.Services.Models.SubordinationStructure;
using OutOfSchool.Services.Repository;
using OutOfSchool.Tests.Common;
using OutOfSchool.WebApi.Models;
using OutOfSchool.WebApi.Services;

Expand Down Expand Up @@ -168,11 +169,11 @@ public async Task Update_WhenEntityIsValid_UpdatesExistedEntity()
Id = 1,
Title = "ChangedTitle1",
};
var expected = new Direction()
{
Id = 1,
Title = "NewTitle",
};

var expected = (await repo.GetByFilter(
d => !d.IsDeleted && d.Id == changedEntity.Id)
.ConfigureAwait(false)).SingleOrDefault();

mapper.Setup(m => m.Map<Direction>(changedEntity)).Returns(expected);
mapper.Setup(m => m.Map<DirectionDto>(expected)).Returns(changedEntity);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ private Expression<Func<Workshop, bool>> PredicateBuild(WorkshopFilter filter)
var tempPredicate = PredicateBuilder.False<Workshop>();
foreach (var direction in filter.DirectionIds)
{
tempPredicate = tempPredicate.Or(x => x.InstitutionHierarchy.Directions.Any(d => d.Id == direction));
tempPredicate = tempPredicate.Or(x => x.InstitutionHierarchy.Directions.Any(d => !d.IsDeleted && d.Id == direction));
}

predicate = predicate.And(tempPredicate);
Expand Down
39 changes: 21 additions & 18 deletions OutOfSchool/OutOfSchool.WebApi/Services/DirectionService.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using System;
using System.Linq.Expressions;
using System.Linq.Expressions;
using AutoMapper;
using Microsoft.Extensions.Localization;
using Nest;
using OutOfSchool.Services.Enums;
using OutOfSchool.WebApi.Common;
using OutOfSchool.WebApi.Models;
using OutOfSchool.WebApi.Util;

namespace OutOfSchool.WebApi.Services;

Expand Down Expand Up @@ -76,7 +73,8 @@ public async Task<Result<DirectionDto>> Delete(long id)
{
logger.LogInformation($"Deleting Direction with Id = {id} started.");

var direction = await repository.GetById(id).ConfigureAwait(false);
var directions = await repository.GetByFilter(x => !x.IsDeleted && x.Id == id).ConfigureAwait(false);
var direction = directions.SingleOrDefault();

if (direction == null)
{
Expand Down Expand Up @@ -120,7 +118,7 @@ public async Task<IEnumerable<DirectionDto>> GetAll()
{
logger.LogInformation("Getting all Directions started.");

var directions = await repository.GetAll().ConfigureAwait(false);
var directions = await repository.GetByFilter(x => !x.IsDeleted).ConfigureAwait(false);

logger.LogInformation(!directions.Any()
? "Direction table is empty."
Expand Down Expand Up @@ -181,7 +179,8 @@ public async Task<DirectionDto> GetById(long id)
{
logger.LogInformation($"Getting Direction by Id started. Looking Id = {id}.");

var direction = await repository.GetById((int)id).ConfigureAwait(false);
var directions = await repository.GetByFilter(x => !x.IsDeleted && x.Id == (int)id).ConfigureAwait(false);
var direction = directions.SingleOrDefault();

if (direction == null)
{
Expand All @@ -200,24 +199,26 @@ public async Task<DirectionDto> Update(DirectionDto dto)
{
logger.LogInformation($"Updating Direction with Id = {dto?.Id} started.");

try
{
var direction = await repository.Update(mapper.Map<Direction>(dto)).ConfigureAwait(false);

logger.LogInformation($"Direction with Id = {direction?.Id} updated succesfully.");
var directions = await repository.GetByFilter(x => !x.IsDeleted && x.Id == dto.Id).ConfigureAwait(false);
var direction = directions.SingleOrDefault();

return mapper.Map<DirectionDto>(direction);
}
catch (DbUpdateConcurrencyException)
if (direction is null)
{
logger.LogError($"Updating failed. Direction with Id = {dto?.Id} doesn't exist in the system.");
throw;
throw new DbUpdateConcurrencyException($"Updating failed. Direction with Id = {dto?.Id} doesn't exist in the system.");
}

mapper.Map(dto, direction);
direction = await repository.Update(direction).ConfigureAwait(false);

logger.LogInformation($"Direction with Id = {direction?.Id} updated succesfully.");

return mapper.Map<DirectionDto>(direction);
}

private void DirectionValidation(DirectionDto dto)
{
if (repository.Get(where: x => x.Title == dto.Title).Any())
if (repository.Get(where: x => !x.IsDeleted && x.Title == dto.Title).Any())
{
throw new ArgumentException(localizer["There is already a Direction with such a data."]);
}
Expand All @@ -227,6 +228,8 @@ private void DirectionValidation(DirectionDto dto)
{
Expression<Func<Direction, bool>> predicate = PredicateBuilder.True<Direction>();

predicate = predicate.And(direction => !direction.IsDeleted);

if (!string.IsNullOrWhiteSpace(filter.Name))
{
predicate = predicate
Expand Down Expand Up @@ -260,7 +263,7 @@ private void DirectionValidation(DirectionDto dto)
workshopCountFilter = workshopCountFilter
.And<Workshop>(w => w.Address.CATOTTGId == filter.CatottgId);
}

return (predicate, workshopCountFilter);
}
}
2 changes: 1 addition & 1 deletion OutOfSchool/OutOfSchool.WebApi/Services/FavoriteService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public async Task<FavoriteDto> Update(FavoriteDto dto)
}

mapper.Map(dto, favorite);
favorite = await favoriteRepository.Update(mapper.Map<Favorite>(dto)).ConfigureAwait(false);
favorite = await favoriteRepository.Update(favorite).ConfigureAwait(false);

logger.LogInformation($"Favorite with Id = {favorite?.Id} updated succesfully.");

Expand Down
4 changes: 3 additions & 1 deletion OutOfSchool/OutOfSchool.WebApi/Services/StatisticService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public async Task<IEnumerable<DirectionDto>> GetPopularDirectionsFromDatabase(in

var directionsWithWorkshops = workshops
.SelectMany(w => w.InstitutionHierarchy.Directions)
.Where(d => !d.IsDeleted)
.GroupBy(d => d.Id)
.Select(g => new
{
Expand All @@ -90,6 +91,7 @@ public async Task<IEnumerable<DirectionDto>> GetPopularDirectionsFromDatabase(in

var directionsWithApplications = applications
.SelectMany(a => a.Workshop.InstitutionHierarchy.Directions)
.Where(d => !d.IsDeleted)
.GroupBy(d => d.Id)
.Select(g => new
{
Expand All @@ -116,7 +118,7 @@ public async Task<IEnumerable<DirectionDto>> GetPopularDirectionsFromDatabase(in
WorkshopsCount = x.directionWithWorkshop.WorkshopsCount,
});

var allDirections = directionRepository.Get();
var allDirections = directionRepository.Get(where: d => !d.IsDeleted);

var statistics = allDirections
.GroupJoin(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public ElasticProfile()
.ForMember(dest => dest.InstitutionHierarchy, opt => opt.MapFrom(src => src.InstitutionHierarchy.Title))
.ForMember(
dest => dest.DirectionIds,
opt => opt.MapFrom(src => src.InstitutionHierarchy.Directions.Select(d => d.Id)))
opt => opt.MapFrom(src => src.InstitutionHierarchy.Directions.Where(x => !x.IsDeleted).Select(d => d.Id)))
.ForMember(dest => dest.InstitutionId, opt => opt.MapFrom(src => src.InstitutionHierarchy.InstitutionId))
.ForMember(dest => dest.Institution, opt => opt.MapFrom(src => src.InstitutionHierarchy.Institution.Title))
.ForMember(
Expand Down
7 changes: 4 additions & 3 deletions OutOfSchool/OutOfSchool.WebApi/Util/MappingProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public MappingProfile()
.ForMember(dest => dest.ImageIds, opt => opt.MapFrom(src => src.Images.Select(x => x.ExternalStorageId)))
.ForMember(dest => dest.InstitutionHierarchy, opt => opt.MapFrom(src => src.InstitutionHierarchy.Title))
.ForMember(dest => dest.DirectionIds,
opt => opt.MapFrom(src => src.InstitutionHierarchy.Directions.Select(d => d.Id)))
opt => opt.MapFrom(src => src.InstitutionHierarchy.Directions.Where(x => !x.IsDeleted).Select(d => d.Id)))
.ForMember(dest => dest.InstitutionId, opt => opt.MapFrom(src => src.InstitutionHierarchy.InstitutionId))
.ForMember(dest => dest.Institution, opt => opt.MapFrom(src => src.InstitutionHierarchy.Institution.Title))
.ForMember(dest => dest.CoverImage, opt => opt.Ignore())
Expand Down Expand Up @@ -220,7 +220,7 @@ public MappingProfile()
.ForMember(dest => dest.WorkshopId, opt => opt.MapFrom(s => s.Id))
.ForMember(dest => dest.CoverImageId, opt => opt.MapFrom(s => s.CoverImageId))
.ForMember(dest => dest.DirectionIds,
opt => opt.MapFrom(src => src.InstitutionHierarchy.Directions.Select(x => x.Id)))
opt => opt.MapFrom(src => src.InstitutionHierarchy.Directions.Where(x => !x.IsDeleted).Select(x => x.Id)))
.ForMember(dest => dest.Rating, opt => opt.Ignore())
.ForMember(dest => dest.NumberOfRatings, opt => opt.Ignore())
.ForMember(dest => dest.ProviderLicenseStatus, opt =>
Expand Down Expand Up @@ -305,7 +305,8 @@ public MappingProfile()
.ForMember(dest => dest.MiddleName, opt => opt.MapFrom(src => src.MiddleName ?? string.Empty));

CreateMap<DirectionDto, Direction>()
.ForMember(dest => dest.InstitutionHierarchies, opt => opt.Ignore());
.ForMember(dest => dest.InstitutionHierarchies, opt => opt.Ignore())
.ForMember(dest => dest.IsDeleted, opt => opt.Ignore());

CreateMap<Direction, DirectionDto>()
.ForMember(dest => dest.WorkshopsCount, opt => opt.Ignore());
Expand Down

0 comments on commit 4f7b9f4

Please sign in to comment.