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 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
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,12 @@
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);
}
}
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);
}
}
7 changes: 7 additions & 0 deletions OutOfSchool/OutOfSchool.DataAccess/Models/IDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace OutOfSchool.Services.Models;

public interface IDto<TEntity, TKey>
where TEntity : IKeyedEntity<TKey>
{
TKey Id { get; set; }
}
6 changes: 1 addition & 5 deletions OutOfSchool/OutOfSchool.DataAccess/Models/IKeyedEntity.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace OutOfSchool.Services.Models;
namespace OutOfSchool.Services.Models;

public interface IKeyedEntity<TKey> : IKeyedEntity
{
Expand Down
5 changes: 1 addition & 4 deletions OutOfSchool/OutOfSchool.DataAccess/Models/QuartzJob.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
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 long Id { 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
Expand Up @@ -171,6 +171,22 @@ public virtual async Task<TEntity> Update(TEntity entity)
return entity;
}

public async Task<TEntity> ReadAndUpdateWith<TDto>(TDto dto, Func<TDto, TEntity, TEntity> map)
where TDto : IDto<TEntity, TKey>
{
ArgumentNullException.ThrowIfNull(dto);

var entity = await GetById(dto.Id).ConfigureAwait(false);

if (entity is null)
{
var name = typeof(TEntity).Name;
throw new DbUpdateConcurrencyException($"Updating failed. {name} with Id = {dto.Id} doesn't exist in the system.");
}

return await Update(map(dto, entity)).ConfigureAwait(false);
}

/// <inheritdoc/>
public virtual Task<int> Count(Expression<Func<TEntity, bool>> whereExpression = null)
{
Expand Down
12 changes: 12 additions & 0 deletions OutOfSchool/OutOfSchool.DataAccess/Repository/IEntityRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ public interface IEntityRepositoryBase<TKey, TEntity>
/// <returns>A <see cref="Task"/> representing the result of the asynchronous operation.</returns>
Task RunInTransaction(Func<Task> operation);

/// <summary>
/// Read element from database by id and update information.
/// </summary>
/// <param name="dto">New data with information.</param>
/// <param name="map">Method that represents mapping operation.</param>
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.
/// The task result contains the entity that was updated.</returns>
/// <exception cref="DbUpdateException">An exception that is thrown when an error is encountered while saving to the database.</exception>
/// <exception cref="DbUpdateConcurrencyException">If a concurrency violation is encountered while saving to database.</exception>
Task<TEntity> ReadAndUpdateWith<TDto>(TDto dto, Func<TDto, TEntity, TEntity> map)
where TDto : IDto<TEntity, TKey>;

/// <summary>
/// Update information about element.
/// </summary>
Expand Down
Loading
Loading