Rated people panel added
This commit is contained in:
@@ -3,6 +3,7 @@ using System.Text.Json.Serialization;
|
||||
using WatchIt.Common.Model.Genres;
|
||||
using WatchIt.Common.Model.Rating;
|
||||
using WatchIt.Common.Query;
|
||||
using WatchIt.Database.Model.Rating;
|
||||
|
||||
namespace WatchIt.Common.Model.Media;
|
||||
|
||||
@@ -55,7 +56,9 @@ public class MediaResponse : Media, IQueryOrderable<MediaResponse>
|
||||
ReleaseDate = media.ReleaseDate;
|
||||
Length = media.Length;
|
||||
Type = mediaType;
|
||||
Rating = RatingResponse.Create(media.RatingMedia);
|
||||
Rating = RatingResponseBuilder.Initialize()
|
||||
.Add(media.RatingMedia, x => x.Rating)
|
||||
.Build();
|
||||
Genres = media.Genres.Select(x => new GenreResponse(x)).ToList();
|
||||
}
|
||||
|
||||
|
||||
@@ -49,7 +49,9 @@ public class MovieRatedResponse : MovieResponse, IQueryOrderable<MovieRatedRespo
|
||||
ReleaseDate = mediaMovie.Media.ReleaseDate;
|
||||
Length = mediaMovie.Media.Length;
|
||||
Budget = mediaMovie.Budget;
|
||||
Rating = RatingResponse.Create(mediaMovie.Media.RatingMedia);
|
||||
Rating = RatingResponseBuilder.Initialize()
|
||||
.Add(mediaMovie.Media.RatingMedia, x => x.Rating)
|
||||
.Build();
|
||||
Genres = mediaMovie.Media.Genres.Select(x => new GenreResponse(x)).ToList();
|
||||
UserRating = response.Rating;
|
||||
}
|
||||
|
||||
@@ -54,7 +54,9 @@ public class MovieResponse : Movie, IQueryOrderable<MovieResponse>
|
||||
ReleaseDate = mediaMovie.Media.ReleaseDate;
|
||||
Length = mediaMovie.Media.Length;
|
||||
Budget = mediaMovie.Budget;
|
||||
Rating = RatingResponse.Create(mediaMovie.Media.RatingMedia);
|
||||
Rating = RatingResponseBuilder.Initialize()
|
||||
.Add(mediaMovie.Media.RatingMedia, x => x.Rating)
|
||||
.Build();
|
||||
Genres = mediaMovie.Media.Genres.Select(x => new GenreResponse(x)).ToList();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using WatchIt.Common.Query;
|
||||
|
||||
namespace WatchIt.Common.Model.Persons;
|
||||
|
||||
public class PersonRatedQueryParameters : QueryParameters<PersonRatedResponse>
|
||||
{
|
||||
#region PROPERTIES
|
||||
|
||||
[FromQuery(Name = "name")]
|
||||
public string? Name { get; set; }
|
||||
|
||||
[FromQuery(Name = "full_name")]
|
||||
public string? FullName { get; set; }
|
||||
|
||||
[FromQuery(Name = "description")]
|
||||
public string? Description { get; set; }
|
||||
|
||||
[FromQuery(Name = "birth_date")]
|
||||
public DateOnly? BirthDate { get; set; }
|
||||
|
||||
[FromQuery(Name = "birth_date_from")]
|
||||
public DateOnly? BirthDateFrom { get; set; }
|
||||
|
||||
[FromQuery(Name = "birth_date_to")]
|
||||
public DateOnly? BirthDateTo { get; set; }
|
||||
|
||||
[FromQuery(Name = "death_date")]
|
||||
public DateOnly? DeathDate { get; set; }
|
||||
|
||||
[FromQuery(Name = "death_date_from")]
|
||||
public DateOnly? DeathDateFrom { get; set; }
|
||||
|
||||
[FromQuery(Name = "death_date_to")]
|
||||
public DateOnly? DeathDateTo { get; set; }
|
||||
|
||||
[FromQuery(Name = "gender_id")]
|
||||
public short? GenderId { get; set; }
|
||||
|
||||
[FromQuery(Name = "rating_average")]
|
||||
public decimal? RatingAverage { get; set; }
|
||||
|
||||
[FromQuery(Name = "rating_average_from")]
|
||||
public decimal? RatingAverageFrom { get; set; }
|
||||
|
||||
[FromQuery(Name = "rating_average_to")]
|
||||
public decimal? RatingAverageTo { get; set; }
|
||||
|
||||
[FromQuery(Name = "rating_count")]
|
||||
public long? RatingCount { get; set; }
|
||||
|
||||
[FromQuery(Name = "rating_count_from")]
|
||||
public long? RatingCountFrom { get; set; }
|
||||
|
||||
[FromQuery(Name = "rating_count_to")]
|
||||
public long? RatingCountTo { get; set; }
|
||||
|
||||
[FromQuery(Name = "user_rating_average")]
|
||||
public decimal? UserRatingAverage { get; set; }
|
||||
|
||||
[FromQuery(Name = "user_rating_average_from")]
|
||||
public decimal? UserRatingAverageFrom { get; set; }
|
||||
|
||||
[FromQuery(Name = "user_rating_average_to")]
|
||||
public decimal? UserRatingAverageTo { get; set; }
|
||||
|
||||
[FromQuery(Name = "user_rating_count")]
|
||||
public long? UserRatingCount { get; set; }
|
||||
|
||||
[FromQuery(Name = "user_rating_count_from")]
|
||||
public long? UserRatingCountFrom { get; set; }
|
||||
|
||||
[FromQuery(Name = "user_rating_count_to")]
|
||||
public long? UserRatingCountTo { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
protected override bool IsMeetingConditions(PersonRatedResponse item) =>
|
||||
(
|
||||
TestStringWithRegex(item.Name, Name)
|
||||
&&
|
||||
TestStringWithRegex(item.FullName, FullName)
|
||||
&&
|
||||
TestStringWithRegex(item.Description, Description)
|
||||
&&
|
||||
TestComparable(item.BirthDate, BirthDate, BirthDateFrom, BirthDateTo)
|
||||
&&
|
||||
TestComparable(item.DeathDate, DeathDate, DeathDateFrom, DeathDateTo)
|
||||
&&
|
||||
Test(item.Gender?.Id, GenderId)
|
||||
&&
|
||||
TestComparable(item.Rating.Average, RatingAverage, RatingAverageFrom, RatingAverageTo)
|
||||
&&
|
||||
TestComparable(item.Rating.Count, RatingCount, RatingCountFrom, RatingCountTo)
|
||||
&&
|
||||
TestComparable(item.UserRating.Average, UserRatingAverage, UserRatingAverageFrom, UserRatingAverageTo)
|
||||
&&
|
||||
TestComparable(item.UserRating.Count, UserRatingCount, UserRatingCountFrom, UserRatingCountTo)
|
||||
);
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Text.Json.Serialization;
|
||||
using WatchIt.Common.Model.Genders;
|
||||
using WatchIt.Common.Model.Rating;
|
||||
using WatchIt.Common.Query;
|
||||
using WatchIt.Database.Model.Rating;
|
||||
|
||||
namespace WatchIt.Common.Model.Persons;
|
||||
|
||||
public class PersonRatedResponse : PersonResponse, IQueryOrderable<PersonRatedResponse>
|
||||
{
|
||||
#region PROPERTIES
|
||||
|
||||
[JsonIgnore]
|
||||
public static IDictionary<string, Func<PersonRatedResponse, IComparable>> OrderableProperties { get; } = new Dictionary<string, Func<PersonRatedResponse, IComparable>>
|
||||
{
|
||||
{ "id", x => x.Id },
|
||||
{ "name", x => x.Name },
|
||||
{ "full_name", x => x.FullName },
|
||||
{ "description", x => x.Description },
|
||||
{ "birth_date", x => x.BirthDate },
|
||||
{ "death_date", x => x.BirthDate },
|
||||
{ "gender", x => x.Gender.Name },
|
||||
{ "rating.average", x => x.Rating.Average },
|
||||
{ "rating.count", x => x.Rating.Count },
|
||||
{ "user_rating.average", x => x.UserRating.Average },
|
||||
{ "user_rating.count", x => x.UserRating.Count }
|
||||
};
|
||||
|
||||
[JsonPropertyName("user_rating")]
|
||||
public RatingResponse UserRating { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
[JsonConstructor]
|
||||
public PersonRatedResponse() { }
|
||||
|
||||
[SetsRequiredMembers]
|
||||
public PersonRatedResponse(Database.Model.Person.Person person, IEnumerable<RatingPersonActorRole> actorUserRatings, IEnumerable<RatingPersonCreatorRole> creatorUserRatings)
|
||||
{
|
||||
Id = person.Id;
|
||||
Name = person.Name;
|
||||
FullName = person.FullName;
|
||||
Description = person.Description;
|
||||
BirthDate = person.BirthDate;
|
||||
DeathDate = person.DeathDate;
|
||||
Gender = person.Gender is not null ? new GenderResponse(person.Gender) : null;
|
||||
Rating = RatingResponseBuilder.Initialize()
|
||||
.Add(person.PersonActorRoles.SelectMany(x => x.RatingPersonActorRole), x => x.Rating)
|
||||
.Add(person.PersonCreatorRoles.SelectMany(x => x.RatingPersonCreatorRole), x => x.Rating)
|
||||
.Build();
|
||||
UserRating = RatingResponseBuilder.Initialize()
|
||||
.Add(actorUserRatings, x => x.Rating)
|
||||
.Add(creatorUserRatings, x => x.Rating)
|
||||
.Build();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -3,6 +3,7 @@ using System.Text.Json.Serialization;
|
||||
using WatchIt.Common.Model.Genders;
|
||||
using WatchIt.Common.Model.Rating;
|
||||
using WatchIt.Common.Query;
|
||||
using WatchIt.Database.Model.Rating;
|
||||
|
||||
namespace WatchIt.Common.Model.Persons;
|
||||
|
||||
@@ -53,7 +54,10 @@ public class PersonResponse : Person, IQueryOrderable<PersonResponse>
|
||||
BirthDate = person.BirthDate;
|
||||
DeathDate = person.DeathDate;
|
||||
Gender = person.Gender is not null ? new GenderResponse(person.Gender) : null;
|
||||
Rating = RatingResponse.Create(person.PersonActorRoles, person.PersonCreatorRoles);
|
||||
Rating = RatingResponseBuilder.Initialize()
|
||||
.Add(person.PersonActorRoles.SelectMany(x => x.RatingPersonActorRole), x => x.Rating)
|
||||
.Add(person.PersonCreatorRoles.SelectMany(x => x.RatingPersonCreatorRole), x => x.Rating)
|
||||
.Build();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -25,35 +25,11 @@ public class RatingResponse
|
||||
public RatingResponse() {}
|
||||
|
||||
[SetsRequiredMembers]
|
||||
private RatingResponse(long ratingSum, long ratingCount)
|
||||
internal RatingResponse(long ratingSum, long ratingCount)
|
||||
{
|
||||
Average = ratingCount > 0 ? (decimal)ratingSum / ratingCount : 0;
|
||||
Count = ratingCount;
|
||||
}
|
||||
|
||||
public static RatingResponse Create(long ratingSum, long ratingCount) => new RatingResponse(ratingSum, ratingCount);
|
||||
|
||||
public static RatingResponse Create(IEnumerable<RatingMedia> ratingMedia) => Create(ratingMedia, x => x.Rating);
|
||||
|
||||
public static RatingResponse Create(IEnumerable<RatingPersonActorRole> ratingPersonActorRoles) => Create(ratingPersonActorRoles, x => x.Rating);
|
||||
|
||||
public static RatingResponse Create(IEnumerable<RatingPersonCreatorRole> ratingPersonCreatorRoles) => Create(ratingPersonCreatorRoles, x => x.Rating);
|
||||
|
||||
public static RatingResponse Create<T>(IEnumerable<T> ratingList, Func<T, short> ratingSelector) => new RatingResponse(ratingList.Sum(x => ratingSelector(x)), ratingList.Count());
|
||||
|
||||
public static RatingResponse Create(IEnumerable<PersonActorRole> personActorRoles, IEnumerable<PersonCreatorRole> personCreatorRoles)
|
||||
{
|
||||
IEnumerable<RatingPersonActorRole> ratingsActorRoles = personActorRoles.SelectMany(x => x.RatingPersonActorRole);
|
||||
IEnumerable<RatingPersonCreatorRole> ratingsCreatorRoles = personCreatorRoles.SelectMany(x => x.RatingPersonCreatorRole);
|
||||
return Create(ratingsActorRoles, ratingsCreatorRoles);
|
||||
}
|
||||
|
||||
public static RatingResponse Create(IEnumerable<RatingPersonActorRole> ratingsActorRoles, IEnumerable<RatingPersonCreatorRole> ratingsCreatorRoles)
|
||||
{
|
||||
long ratingSum = ratingsActorRoles.Sum(x => x.Rating) + ratingsCreatorRoles.Sum(x => x.Rating);
|
||||
long ratingCount = ratingsActorRoles.Count() + ratingsCreatorRoles.Count();
|
||||
return new RatingResponse(ratingSum, ratingCount);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
namespace WatchIt.Common.Model.Rating;
|
||||
|
||||
public class RatingResponseBuilder
|
||||
{
|
||||
#region FIELDS
|
||||
|
||||
private long _sum;
|
||||
private long _count;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
private RatingResponseBuilder() { }
|
||||
|
||||
public static RatingResponseBuilder Initialize() => new RatingResponseBuilder();
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
public RatingResponseBuilder Add<T>(IEnumerable<T> collection, Func<T, short> selector)
|
||||
{
|
||||
_sum += collection.Sum(x => selector(x));
|
||||
_count += collection.Count();
|
||||
return this;
|
||||
}
|
||||
public RatingResponse Build() => new RatingResponse(_sum, _count);
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -49,7 +49,9 @@ public class SeriesRatedResponse : SeriesResponse, IQueryOrderable<SeriesRatedRe
|
||||
ReleaseDate = mediaSeries.Media.ReleaseDate;
|
||||
Length = mediaSeries.Media.Length;
|
||||
HasEnded = mediaSeries.HasEnded;
|
||||
Rating = RatingResponse.Create(mediaSeries.Media.RatingMedia);
|
||||
Rating = RatingResponseBuilder.Initialize()
|
||||
.Add(mediaSeries.Media.RatingMedia, x => x.Rating)
|
||||
.Build();
|
||||
Genres = mediaSeries.Media.Genres.Select(x => new GenreResponse(x)).ToList();
|
||||
UserRating = response.Rating;
|
||||
}
|
||||
|
||||
@@ -54,7 +54,9 @@ public class SeriesResponse : Series, IQueryOrderable<SeriesResponse>
|
||||
ReleaseDate = mediaSeries.Media.ReleaseDate;
|
||||
Length = mediaSeries.Media.Length;
|
||||
HasEnded = mediaSeries.HasEnded;
|
||||
Rating = RatingResponse.Create(mediaSeries.Media.RatingMedia);
|
||||
Rating = RatingResponseBuilder.Initialize()
|
||||
.Add(mediaSeries.Media.RatingMedia, x => x.Rating)
|
||||
.Build();
|
||||
Genres = mediaSeries.Media.Genres.Select(x => new GenreResponse(x)).ToList();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user