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 AverageRatings, Ratings and QuartzJobs. #1241

Merged
merged 7 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 3 additions & 6 deletions OutOfSchool/OutOfSchool.DataAccess/Models/AverageRating.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OutOfSchool.Services.Enums;

namespace OutOfSchool.Services.Models;

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

public bool IsDeleted { get; set; }

public float Rate { get; set; }

public int RateQuantity { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@ internal class AverageRatingConfiguration : IEntityTypeConfiguration<AverageRati
public void Configure(EntityTypeBuilder<AverageRating> builder)
{
builder.HasIndex(x => x.EntityId);

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,16 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace OutOfSchool.Services.Models.Configurations;

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

builder.HasIndex(x => x.IsDeleted);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we ready need to track deleted quartz jobs? They they are either useful and exist in db or not usefull and we can safely delete it completely.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I'll fix it in the next PR.


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

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace OutOfSchool.Services.Models.Configurations;
Expand All @@ -9,5 +8,9 @@ internal class RatingConfiguration : IEntityTypeConfiguration<Rating>
public void Configure(EntityTypeBuilder<Rating> builder)
{
builder.HasIndex(x => x.EntityId);

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

builder.Property(x => x.IsDeleted).HasDefaultValue(false);
}
}
9 changes: 4 additions & 5 deletions OutOfSchool/OutOfSchool.DataAccess/Models/QuartzJob.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OutOfSchool.Services.Models;
public class QuartzJob : IKeyedEntity<long>

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

public bool IsDeleted { get; set; }

public string Name { get; set; }

public DateTimeOffset LastSuccessLaunch { get; set; }
Expand Down
6 changes: 3 additions & 3 deletions OutOfSchool/OutOfSchool.DataAccess/Models/Rating.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using System;
using System.ComponentModel.DataAnnotations;

using OutOfSchool.Services.Enums;

namespace OutOfSchool.Services.Models;

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

public bool IsDeleted { get; set; }

[Range(1, 5)]
public int Rate { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ private void ApplySoftDelete(ModelBuilder builder)
.ApplySoftDelete<User>()
.ApplySoftDelete<Address>()
.ApplySoftDelete<Application>()
.ApplySoftDelete<AverageRating>()
.ApplySoftDelete<BlockedProviderParent>()
.ApplySoftDelete<ChatMessageWorkshop>()
.ApplySoftDelete<ChatRoomWorkshop>()
Expand All @@ -59,11 +58,9 @@ private void ApplySoftDelete(ModelBuilder builder)
.ApplySoftDelete<Provider>()
.ApplySoftDelete<ProviderAdmin>()
.ApplySoftDelete<ProviderSectionItem>()
.ApplySoftDelete<Rating>()
.ApplySoftDelete<RegionAdmin>()
.ApplySoftDelete<SocialGroup>()
.ApplySoftDelete<Workshop>()
.ApplySoftDelete<WorkshopDescriptionItem>()
.ApplySoftDelete<QuartzJob>();
.ApplySoftDelete<WorkshopDescriptionItem>();
}
}
1 change: 1 addition & 0 deletions OutOfSchool/OutOfSchool.DataAccess/OutOfSchoolDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ protected override void OnModelCreating(ModelBuilder builder)
builder.ApplyConfiguration(new OperationWithObjectConfiguration());
builder.ApplyConfiguration(new ProviderConfiguration());
builder.ApplyConfiguration(new ProviderAdminConfiguration());
builder.ApplyConfiguration(new QuartzJobConfiguration());
builder.ApplyConfiguration(new RatingConfiguration());
builder.ApplyConfiguration(new TeacherConfiguration());
builder.ApplyConfiguration(new WorkshopConfiguration());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,30 @@
using AutoMapper;
using Microsoft.EntityFrameworkCore.Storage;
using Nest;
using NuGet.Packaging;
using OutOfSchool.ElasticsearchData.Models;
using OutOfSchool.Services.Enums;
using OutOfSchool.Services.Models;
using OutOfSchool.Services.Repository;
using OutOfSchool.WebApi.Common.QuartzConstants;
using OutOfSchool.WebApi.Models;
using System.Collections.Generic;
using System.Drawing.Text;
using System.Linq;
using System.Net.WebSockets;

namespace OutOfSchool.WebApi.Services.AverageRatings;

public class AverageRatingService : IAverageRatingService
{
private readonly ILogger<AverageRatingService> logger;
private readonly IMapper mapper;
private readonly IEntityRepository<long, AverageRating> averageRatingRepository;
private readonly IEntityRepositorySoftDeleted<long, AverageRating> averageRatingRepository;
private readonly IRatingService ratingService;
private readonly IWorkshopRepository workshopRepository;
private readonly IEntityRepository<long, Rating> ratingRepository;
private readonly IEntityRepository<long, QuartzJob> quartzJobRepository;
private readonly IEntityRepositorySoftDeleted<long, Rating> ratingRepository;
private readonly IEntityRepositorySoftDeleted<long, QuartzJob> quartzJobRepository;
private readonly IOperationWithObjectService operationWithObjectService;

public AverageRatingService(
ILogger<AverageRatingService> logger,
IMapper mapper,
IEntityRepository<long, AverageRating> averageRatingRepository,
IEntityRepositorySoftDeleted<long, AverageRating> averageRatingRepository,
IRatingService ratingService,
IWorkshopRepository workshopRepository,
IEntityRepository<long, Rating> ratingRepository,
IEntityRepository<long, QuartzJob> quartzJobRepository,
IEntityRepositorySoftDeleted<long, Rating> ratingRepository,
IEntityRepositorySoftDeleted<long, QuartzJob> quartzJobRepository,
IOperationWithObjectService operationWithObjectService)
{
this.logger = logger;
Expand Down
20 changes: 6 additions & 14 deletions OutOfSchool/OutOfSchool.WebApi/Services/RatingService.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Linq.Expressions;
using AutoMapper;
using Castle.Core.Internal;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.ObjectPool;
using Nest;
using OutOfSchool.Services.Enums;
using OutOfSchool.Services.Models;
using OutOfSchool.Services.Repository;
using OutOfSchool.WebApi.Extensions;
using OutOfSchool.WebApi.Models;

namespace OutOfSchool.WebApi.Services;
Expand All @@ -22,7 +12,7 @@ namespace OutOfSchool.WebApi.Services;
/// </summary>
public class RatingService : IRatingService
{
private readonly IEntityRepository<long, Rating> ratingRepository;
private readonly IEntityRepositorySoftDeleted<long, Rating> ratingRepository;
private readonly IWorkshopRepository workshopRepository;
private readonly IParentRepository parentRepository;
private readonly ILogger<RatingService> logger;
Expand All @@ -41,7 +31,7 @@ public class RatingService : IRatingService
/// <param name="mapper">Mapper.</param>
/// <param name="operationWithObjectService">Service operation with rating.</param>
public RatingService(
IEntityRepository<long, Rating> ratingRepository,
IEntityRepositorySoftDeleted<long, Rating> ratingRepository,
IWorkshopRepository workshopRepository,
IParentRepository parentRepository,
ILogger<RatingService> logger,
Expand Down Expand Up @@ -209,7 +199,9 @@ public async Task<RatingDto> Update(RatingDto dto)

if (await CheckRatingUpdate(dto).ConfigureAwait(false))
{
var rating = await ratingRepository.Update(mapper.Map<Rating>(dto)).ConfigureAwait(false);
var rating = await ratingRepository.GetById(dto.Id).ConfigureAwait(false);
mapper.Map(dto, rating);
rating = await ratingRepository.Update(rating).ConfigureAwait(false);
VadymLevkovskyi marked this conversation as resolved.
Show resolved Hide resolved

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

Expand Down
2 changes: 2 additions & 0 deletions OutOfSchool/OutOfSchool.WebApi/Util/MappingProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,9 @@ public MappingProfile()
CreateMap<Rating, RatingDto>()
.ForMember(dest => dest.FirstName, opt => opt.Ignore())
.ForMember(dest => dest.LastName, opt => opt.Ignore());

CreateMap<RatingDto, Rating>()
.ForMember(dest => dest.IsDeleted, opt => opt.Ignore())
VadymLevkovskyi marked this conversation as resolved.
Show resolved Hide resolved
.ForMember(dest => dest.Parent, opt => opt.Ignore());

CreateMap<AverageRating, AverageRatingDto>();
Expand Down