Refactoring, database structure changed
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
namespace WatchIt.DTO.Models.Generics.Rating;
|
||||
|
||||
public interface IRatingOverallResponse : IRatingResponse
|
||||
{
|
||||
long Count { get; set; }
|
||||
}
|
||||
6
WatchIt.DTO/Models/Generics/Rating/IRatingResponse.cs
Normal file
6
WatchIt.DTO/Models/Generics/Rating/IRatingResponse.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace WatchIt.DTO.Models.Generics.Rating;
|
||||
|
||||
public interface IRatingResponse
|
||||
{
|
||||
decimal? Rating { get; }
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace WatchIt.DTO.Models.Generics.Rating;
|
||||
|
||||
public interface IRatingUserResponse : IRatingResponse
|
||||
{
|
||||
DateTimeOffset? Date { get; }
|
||||
}
|
||||
51
WatchIt.DTO/Models/Generics/Rating/RatingMappers.cs
Normal file
51
WatchIt.DTO/Models/Generics/Rating/RatingMappers.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using WatchIt.Database.Model;
|
||||
|
||||
namespace WatchIt.DTO.Models.Generics.Rating;
|
||||
|
||||
public static class RatingMappers
|
||||
{
|
||||
#region PUBLIC METHODS
|
||||
|
||||
public static void UpdateWithRequest(this IRatingEntity entity, RatingRequest ratingRequest)
|
||||
{
|
||||
entity.Rating = ratingRequest.Rating;
|
||||
}
|
||||
|
||||
public static RatingOverallResponse ToOverallResponse(this IEnumerable<IRatingEntity> entities)
|
||||
{
|
||||
IEnumerable<IRatingEntity> ratingEntities = entities.ToList();
|
||||
|
||||
long sum = ratingEntities.Sum(x => x.Rating);
|
||||
long count = ratingEntities.Count();
|
||||
|
||||
return new RatingOverallResponse
|
||||
{
|
||||
Rating = count > 0 ? (decimal)sum / count : null,
|
||||
Count = count
|
||||
};
|
||||
}
|
||||
|
||||
public static RatingUserResponse ToUserResponse(this IRatingEntity entity) => new RatingUserResponse
|
||||
{
|
||||
Date = entity.Date,
|
||||
Rating = entity.Rating,
|
||||
};
|
||||
|
||||
public static RatingUserOverallResponse ToUserOverallResponse(this IEnumerable<IRatingEntity> entities)
|
||||
{
|
||||
IEnumerable<IRatingEntity> ratingEntities = entities.ToList();
|
||||
|
||||
long sum = ratingEntities.Sum(x => x.Rating);
|
||||
long count = ratingEntities.Count();
|
||||
DateTimeOffset? lastDate = count == 0 ? null : ratingEntities.Max(x => x.Date);
|
||||
|
||||
return new RatingUserOverallResponse
|
||||
{
|
||||
Rating = count > 0 ? (decimal)sum / count : null,
|
||||
Date = lastDate,
|
||||
Count = count
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
11
WatchIt.DTO/Models/Generics/Rating/RatingOverallResponse.cs
Normal file
11
WatchIt.DTO/Models/Generics/Rating/RatingOverallResponse.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace WatchIt.DTO.Models.Generics.Rating;
|
||||
|
||||
public class RatingOverallResponse : IRatingOverallResponse
|
||||
{
|
||||
#region PROPERTIES
|
||||
|
||||
public decimal? Rating { get; set; }
|
||||
public long Count { get; set; }
|
||||
|
||||
#endregion
|
||||
}
|
||||
10
WatchIt.DTO/Models/Generics/Rating/RatingRequest.cs
Normal file
10
WatchIt.DTO/Models/Generics/Rating/RatingRequest.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace WatchIt.DTO.Models.Generics.Rating;
|
||||
|
||||
public class RatingRequest
|
||||
{
|
||||
#region PROPERTIES
|
||||
|
||||
public byte Rating { get; set; }
|
||||
|
||||
#endregion
|
||||
}
|
||||
15
WatchIt.DTO/Models/Generics/Rating/RatingRequestValidator.cs
Normal file
15
WatchIt.DTO/Models/Generics/Rating/RatingRequestValidator.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace WatchIt.DTO.Models.Generics.Rating;
|
||||
|
||||
public class RatingRequestValidator : AbstractValidator<RatingRequest>
|
||||
{
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public RatingRequestValidator()
|
||||
{
|
||||
RuleFor(x => x.Rating).InclusiveBetween((byte)1, (byte)10);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace WatchIt.DTO.Models.Generics.Rating;
|
||||
|
||||
public class RatingUserOverallResponse : IRatingUserResponse, IRatingOverallResponse
|
||||
{
|
||||
#region PROPERTIES
|
||||
|
||||
public decimal? Rating { get; set; }
|
||||
public long Count { get; set; }
|
||||
public DateTimeOffset? Date { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
16
WatchIt.DTO/Models/Generics/Rating/RatingUserResponse.cs
Normal file
16
WatchIt.DTO/Models/Generics/Rating/RatingUserResponse.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace WatchIt.DTO.Models.Generics.Rating;
|
||||
|
||||
public class RatingUserResponse : IRatingUserResponse
|
||||
{
|
||||
#region PROPERTIES
|
||||
|
||||
public byte Rating { get; set; }
|
||||
public DateTimeOffset Date { get; set; }
|
||||
|
||||
[JsonIgnore] decimal? IRatingResponse.Rating => Rating;
|
||||
[JsonIgnore] DateTimeOffset? IRatingUserResponse.Date => Date;
|
||||
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user