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

Change SoftDelete for Favorites #1194

Merged
merged 2 commits into from
Aug 14, 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
@@ -0,0 +1,16 @@
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore;

namespace OutOfSchool.Services.Models.Configurations;

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

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

builder.Property(x => x.IsDeleted).HasDefaultValue(false);
}
}
2 changes: 2 additions & 0 deletions OutOfSchool/OutOfSchool.DataAccess/Models/Favorite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public class Favorite : IKeyedEntity<long>
{
public long 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 @@ -49,6 +49,7 @@ private void ApplySoftDelete(ModelBuilder builder)
.ApplySoftDelete<AchievementType>()
.ApplySoftDelete<Address>()
.ApplySoftDelete<Application>()
.ApplySoftDelete<AverageRating>()
.ApplySoftDelete<BlockedProviderParent>()
.ApplySoftDelete<ChatMessageWorkshop>()
.ApplySoftDelete<ChatRoomWorkshop>()
Expand All @@ -58,7 +59,7 @@ private void ApplySoftDelete(ModelBuilder builder)
.ApplySoftDelete<CompanyInformationItem>()
.ApplySoftDelete<DateTimeRange>()
.ApplySoftDelete<Direction>()
.ApplySoftDelete<Favorite>()
//.ApplySoftDelete<Favorite>()
//.ApplySoftDelete<Institution>()
.ApplySoftDelete<InstitutionFieldDescription>()
.ApplySoftDelete<InstitutionHierarchy>()
Expand All @@ -73,7 +74,6 @@ private void ApplySoftDelete(ModelBuilder builder)
.ApplySoftDelete<Teacher>()
.ApplySoftDelete<Workshop>()
.ApplySoftDelete<WorkshopDescriptionItem>()
.ApplySoftDelete<AverageRating>()
.ApplySoftDelete<QuartzJob>();
}
}
23 changes: 12 additions & 11 deletions OutOfSchool/OutOfSchool.DataAccess/OutOfSchoolDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,25 +122,26 @@ protected override void OnModelCreating(ModelBuilder builder)
builder.Entity<DateTimeRange>()
.HasCheckConstraint("CK_DateTimeRanges_EndTimeIsAfterStartTime", "EndTime >= StartTime");

builder.ApplyConfiguration(new TeacherConfiguration());
builder.ApplyConfiguration(new AchievementConfiguration());
builder.ApplyConfiguration(new AddressConfiguration());
builder.ApplyConfiguration(new ApplicationConfiguration());
builder.ApplyConfiguration(new AverageRatingConfiguration());
builder.ApplyConfiguration(new ChatMessageWorkshopConfiguration());
builder.ApplyConfiguration(new ChatRoomWorkshopConfiguration());
builder.ApplyConfiguration(new ChildConfiguration());
builder.ApplyConfiguration(new ProviderConfiguration());
builder.ApplyConfiguration(new CodeficatorConfiguration());
builder.ApplyConfiguration(new EntityImagesConfiguration<Provider>());
builder.ApplyConfiguration(new ProviderAdminConfiguration());
builder.ApplyConfiguration(new WorkshopConfiguration());
builder.ApplyConfiguration(new EntityImagesConfiguration<Workshop>());
builder.ApplyConfiguration(new NotificationConfiguration());
builder.ApplyConfiguration(new AchievementConfiguration());
builder.ApplyConfiguration(new AddressConfiguration());
builder.ApplyConfiguration(new CodeficatorConfiguration());
builder.ApplyConfiguration(new RatingConfiguration());
builder.ApplyConfiguration(new AverageRatingConfiguration());
builder.ApplyConfiguration(new OperationWithObjectConfiguration());
builder.ApplyConfiguration(new FavoriteConfiguration());
builder.ApplyConfiguration(new InstitutionConfiguration());
builder.ApplyConfiguration(new InstitutionStatusConfiguration());
builder.ApplyConfiguration(new NotificationConfiguration());
builder.ApplyConfiguration(new OperationWithObjectConfiguration());
builder.ApplyConfiguration(new ProviderConfiguration());
builder.ApplyConfiguration(new ProviderAdminConfiguration());
builder.ApplyConfiguration(new RatingConfiguration());
builder.ApplyConfiguration(new TeacherConfiguration());
builder.ApplyConfiguration(new WorkshopConfiguration());

ApplySoftDelete(builder);

Expand Down
44 changes: 19 additions & 25 deletions OutOfSchool/OutOfSchool.WebApi/Services/FavoriteService.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using AutoMapper;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using OutOfSchool.Common.Enums;
using OutOfSchool.Services.Models;
using OutOfSchool.Services.Repository;
using OutOfSchool.WebApi.Extensions;
using OutOfSchool.WebApi.Models;

namespace OutOfSchool.WebApi.Services;
Expand Down Expand Up @@ -44,7 +34,7 @@ public async Task<IEnumerable<FavoriteDto>> GetAll()
{
logger.LogInformation("Getting all Favorites started.");

var favorites = await favoriteRepository.GetAll().ConfigureAwait(false);
var favorites = await favoriteRepository.GetByFilter(x => !x.IsDeleted).ConfigureAwait(false);

logger.LogInformation(!favorites.Any()
? "Favorites table is empty."
Expand All @@ -58,7 +48,8 @@ public async Task<FavoriteDto> GetById(long id)
{
logger.LogInformation($"Getting Favorite by Id started. Looking Id = {id}.");

var favorite = await favoriteRepository.GetById(id).ConfigureAwait(false);
var favorites = await favoriteRepository.GetByFilter(x => !x.IsDeleted && x.Id == id).ConfigureAwait(false);
var favorite = favorites.SingleOrDefault();

if (favorite == null)
{
Expand All @@ -78,7 +69,7 @@ public async Task<IEnumerable<FavoriteDto>> GetAllByUser(string userId)
logger.LogInformation("Getting Favorites by User started. Looking UserId = {UserId}", userId);

var favoritesQuery = await favoriteRepository
.GetByFilter(x => x.UserId == userId && Provider.ValidProviderStatuses.Contains(x.Workshop.Provider.Status))
.GetByFilter(x => !x.IsDeleted && x.UserId == userId && Provider.ValidProviderStatuses.Contains(x.Workshop.Provider.Status))
.ConfigureAwait(false);

var favorites = favoritesQuery.ToList();
Expand All @@ -97,7 +88,7 @@ public async Task<SearchResult<WorkshopCard>> GetFavoriteWorkshopsByUser(string
logger.LogInformation($"Getting Favorites by User started. Looking UserId = {userId}.");

var favorites = await favoriteRepository
.Get(where: x => x.UserId == userId && Provider.ValidProviderStatuses.Contains(x.Workshop.Provider.Status))
.Get(where: x => !x.IsDeleted && x.UserId == userId && Provider.ValidProviderStatuses.Contains(x.Workshop.Provider.Status))
.Select(x => x.WorkshopId)
.ToListAsync()
.ConfigureAwait(false);
Expand Down Expand Up @@ -142,27 +133,30 @@ public async Task<FavoriteDto> Update(FavoriteDto dto)
{
logger.LogInformation($"Updating Favorite with Id = {dto?.Id} started.");

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

logger.LogInformation($"Favorite with Id = {favorite?.Id} updated succesfully.");
var favorites = await favoriteRepository.GetByFilter(x => !x.IsDeleted && x.Id == dto.Id).ConfigureAwait(false);
var favorite = favorites.SingleOrDefault();

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

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

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

return mapper.Map<FavoriteDto>(favorite);
}

/// <inheritdoc/>
public async Task Delete(long id)
{
logger.LogInformation($"Deleting Favorite with Id = {id} started.");

var favorite = await favoriteRepository.GetById(id).ConfigureAwait(false);
var favorites = await favoriteRepository.GetByFilter(x => !x.IsDeleted && x.Id == id).ConfigureAwait(false);
var favorite = favorites.SingleOrDefault();

if (favorite == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public async Task<IEnumerable<string>> GetNotificationsRecipientIds(
{
var recipientIds = new List<string>();

var favoriteWorkshopUsersIds = await favoriteRepository.Get(where: x => x.WorkshopId == objectId)
var favoriteWorkshopUsersIds = await favoriteRepository.Get(where: x => !x.IsDeleted && x.WorkshopId == objectId)
.Select(x => x.UserId)
.ToListAsync()
.ConfigureAwait(false);
Expand Down
Loading