Refactoring, database structure changed
This commit is contained in:
19
WatchIt.DTO/Models/Generics/Image/ImageBase.cs
Normal file
19
WatchIt.DTO/Models/Generics/Image/ImageBase.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
namespace WatchIt.DTO.Models.Generics.Image;
|
||||
|
||||
public abstract class ImageBase
|
||||
{
|
||||
#region PROPERTIES
|
||||
|
||||
public byte[] Image { get; set; } = default!;
|
||||
public string MimeType { get; set; } = default!;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
public override string ToString() => $"data:{MimeType};base64,{Convert.ToBase64String(Image)}";
|
||||
|
||||
#endregion
|
||||
}
|
||||
24
WatchIt.DTO/Models/Generics/Image/ImageMappers.cs
Normal file
24
WatchIt.DTO/Models/Generics/Image/ImageMappers.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using WatchIt.Database.Model;
|
||||
|
||||
namespace WatchIt.DTO.Models.Generics.Image;
|
||||
|
||||
public static class ImageMappers
|
||||
{
|
||||
#region PUBLIC METHODS
|
||||
|
||||
public static ImageResponse ToResponse(this IImageEntity entity) => new ImageResponse
|
||||
{
|
||||
Image = entity.Image,
|
||||
MimeType = entity.MimeType,
|
||||
UploadDate = entity.UploadDate,
|
||||
};
|
||||
|
||||
public static void UpdateWithRequest(this IImageEntity entity, ImageRequest request)
|
||||
{
|
||||
entity.Image = request.Image;
|
||||
entity.MimeType = request.MimeType;
|
||||
entity.UploadDate = DateTimeOffset.UtcNow;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
3
WatchIt.DTO/Models/Generics/Image/ImageRequest.cs
Normal file
3
WatchIt.DTO/Models/Generics/Image/ImageRequest.cs
Normal file
@@ -0,0 +1,3 @@
|
||||
namespace WatchIt.DTO.Models.Generics.Image;
|
||||
|
||||
public class ImageRequest : ImageBase;
|
||||
16
WatchIt.DTO/Models/Generics/Image/ImageRequestValidator.cs
Normal file
16
WatchIt.DTO/Models/Generics/Image/ImageRequestValidator.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace WatchIt.DTO.Models.Generics.Image;
|
||||
|
||||
public class ImageRequestValidator : AbstractValidator<ImageRequest>
|
||||
{
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public ImageRequestValidator()
|
||||
{
|
||||
RuleFor(x => x.Image).NotEmpty();
|
||||
RuleFor(x => x.MimeType).Matches(@"\w+/.+").WithMessage("Incorrect mimetype");
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
10
WatchIt.DTO/Models/Generics/Image/ImageResponse.cs
Normal file
10
WatchIt.DTO/Models/Generics/Image/ImageResponse.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace WatchIt.DTO.Models.Generics.Image;
|
||||
|
||||
public class ImageResponse : ImageBase
|
||||
{
|
||||
#region PROPERTIES
|
||||
|
||||
public DateTimeOffset UploadDate { get; set; }
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
30
WatchIt.DTO/Models/Generics/ViewCount/ViewCountMappers.cs
Normal file
30
WatchIt.DTO/Models/Generics/ViewCount/ViewCountMappers.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using WatchIt.Database.Model;
|
||||
|
||||
namespace WatchIt.DTO.Models.Generics.ViewCount;
|
||||
|
||||
public static class ViewCountMappers
|
||||
{
|
||||
#region PUBLIC METHODS
|
||||
|
||||
public static ViewCountResponse ToResponse(this IEnumerable<IViewCountEntity> viewCounts)
|
||||
{
|
||||
IEnumerable<IViewCountEntity> viewCountEntities = viewCounts.ToList();
|
||||
return new ViewCountResponse
|
||||
{
|
||||
Last24Hours = viewCountEntities.CountFrom(DateTime.Now.AddDays(-1)),
|
||||
LastWeek = viewCountEntities.CountFrom(DateTime.Now.AddDays(-7)),
|
||||
LastMonth = viewCountEntities.CountFrom(DateTime.Now.AddMonths(-1)),
|
||||
LastYear = viewCountEntities.CountFrom(DateTime.Now.AddYears(-1)),
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PRIVATE METHODS
|
||||
|
||||
private static long CountFrom(this IEnumerable<IViewCountEntity> viewCounts, DateTime date) => viewCounts.Where(x => x.Date >= DateOnly.FromDateTime(date)).Sum(x => x.ViewCount);
|
||||
|
||||
#endregion
|
||||
}
|
||||
13
WatchIt.DTO/Models/Generics/ViewCount/ViewCountResponse.cs
Normal file
13
WatchIt.DTO/Models/Generics/ViewCount/ViewCountResponse.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
namespace WatchIt.DTO.Models.Generics.ViewCount;
|
||||
|
||||
public class ViewCountResponse
|
||||
{
|
||||
#region PROPERTIES
|
||||
|
||||
public long Last24Hours { get; set; }
|
||||
public long LastWeek { get; set; }
|
||||
public long LastMonth { get; set; }
|
||||
public long LastYear { get; set; }
|
||||
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user