person controller and service created

This commit is contained in:
2024-10-02 16:09:11 +02:00
Unverified
parent 3cd282a2e9
commit 9a9e29ecf4
22 changed files with 387 additions and 20 deletions

View File

@@ -0,0 +1,13 @@
using System.Text.Json.Serialization;
namespace WatchIt.Common.Model.Genders;
public class Gender
{
#region PROPERTIES
[JsonPropertyName("name")]
public required string Name { get; set; }
#endregion
}

View File

@@ -0,0 +1,27 @@
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;
namespace WatchIt.Common.Model.Genders;
public class GenderResponse : Gender
{
#region PROPERTIES
[JsonPropertyName("id")]
public required short? Id { get; set; }
#endregion
#region CONSTRUCTORS
[SetsRequiredMembers]
public GenderResponse(Database.Model.Common.Gender gender)
{
Id = gender.Id;
Name = gender.Name;
}
#endregion
}

View File

@@ -17,9 +17,9 @@ public class GenreQueryParameters : QueryParameters<GenreResponse>
#region PUBLIC METHODS
#region PRIVATE METHODS
public override bool IsMeetingConditions(GenreResponse item) =>
protected override bool IsMeetingConditions(GenreResponse item) =>
(
TestStringWithRegex(item.Name, Name)
&&

View File

@@ -59,9 +59,9 @@ public class MediaQueryParameters : QueryParameters<MediaResponse>
#region PUBLIC METHODS
#region PRIVATE METHODS
public override bool IsMeetingConditions(MediaResponse item) =>
protected override bool IsMeetingConditions(MediaResponse item) =>
(
Test(item.Type, Type)
&&

View File

@@ -51,7 +51,7 @@ public class MediaResponse : Media, IQueryOrderable<MediaResponse>
ReleaseDate = media.ReleaseDate;
Length = media.Length;
Type = mediaType;
Rating = new RatingResponse(media.RatingMedia);
Rating = RatingResponse.Create(media.RatingMedia);
}
#endregion

View File

@@ -65,9 +65,9 @@ public class MovieQueryParameters : QueryParameters<MovieResponse>
#region PUBLIC METHODS
#region PRIVATE METHODS
public override bool IsMeetingConditions(MovieResponse item) =>
protected override bool IsMeetingConditions(MovieResponse item) =>
(
TestStringWithRegex(item.Title, Title)
&&

View File

@@ -50,7 +50,7 @@ public class MovieResponse : Movie, IQueryOrderable<MovieResponse>
ReleaseDate = mediaMovie.Media.ReleaseDate;
Length = mediaMovie.Media.Length;
Budget = mediaMovie.Budget;
Rating = new RatingResponse(mediaMovie.Media.RatingMedia);
Rating = RatingResponse.Create(mediaMovie.Media.RatingMedia);
}
#endregion

View File

@@ -0,0 +1,25 @@
using System.Text.Json.Serialization;
namespace WatchIt.Common.Model.Persons;
public class Person
{
#region PROPERTIES
[JsonPropertyName("name")]
public required string Name { get; set; }
[JsonPropertyName("full_name")]
public string? FullName { get; set; }
[JsonPropertyName("description")]
public string? Description { get; set; }
[JsonPropertyName("birth_date")]
public DateOnly? BirthDate { get; set; }
[JsonPropertyName("death_date")]
public DateOnly? DeathDate { get; set; }
#endregion
}

View File

@@ -0,0 +1,84 @@
using Microsoft.AspNetCore.Mvc;
using WatchIt.Common.Query;
namespace WatchIt.Common.Model.Persons;
public class PersonQueryParameters : QueryParameters<PersonResponse>
{
#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; }
#endregion
#region PUBLIC METHODS
protected override bool IsMeetingConditions(PersonResponse 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)
);
#endregion
}

View File

@@ -0,0 +1,29 @@
using System.Text.Json.Serialization;
namespace WatchIt.Common.Model.Persons;
public class PersonRequest : Person
{
#region PROPERTIES
[JsonPropertyName("gender_id")]
public short? GenderId { get; set; }
#endregion
#region PUBLIC METHODS
public Database.Model.Person.Person CreatePerson() => new Database.Model.Person.Person
{
Name = Name,
FullName = FullName,
Description = Description,
BirthDate = BirthDate,
DeathDate = DeathDate,
GenderId = GenderId,
};
#endregion
}

View File

@@ -0,0 +1,57 @@
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;
using WatchIt.Common.Model.Genders;
using WatchIt.Common.Model.Rating;
using WatchIt.Common.Query;
namespace WatchIt.Common.Model.Persons;
public class PersonResponse : Person, IQueryOrderable<PersonResponse>
{
#region PROPERTIES
[JsonIgnore]
public static IDictionary<string, Func<PersonResponse, IComparable>> OrderableProperties { get; } = new Dictionary<string, Func<PersonResponse, 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 }
};
[JsonPropertyName("id")]
public required long Id { get; set; }
[JsonPropertyName("gender")]
public GenderResponse? Gender { get; set; }
[JsonPropertyName("rating")]
public required RatingResponse Rating { get; set; }
#endregion
#region CONSTRUCTORS
[SetsRequiredMembers]
public PersonResponse(Database.Model.Person.Person person)
{
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 = RatingResponse.Create(person.PersonActorRoles, person.PersonCreatorRoles);
}
#endregion
}

View File

@@ -32,9 +32,9 @@ public class PhotoQueryParameters : QueryParameters<PhotoResponse>
#region PUBLIC METHODS
#region PRIVATE METHODS
public override bool IsMeetingConditions(PhotoResponse item) =>
protected override bool IsMeetingConditions(PhotoResponse item) =>
(
TestStringWithRegex(item.MimeType, MimeType)
&&

View File

@@ -1,5 +1,6 @@
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;
using WatchIt.Database.Model.Person;
using WatchIt.Database.Model.Rating;
namespace WatchIt.Common.Model.Rating;
@@ -24,14 +25,24 @@ public class RatingResponse
public RatingResponse() {}
[SetsRequiredMembers]
public RatingResponse(IEnumerable<RatingMedia> ratingMedia) : this(ratingMedia.Any() ? (decimal)ratingMedia.Average(x => x.Rating) : 0, ratingMedia.Count()) {}
[SetsRequiredMembers]
public RatingResponse(decimal ratingAverage, long ratingCount)
private RatingResponse(long ratingSum, long ratingCount)
{
Average = ratingAverage;
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) => new RatingResponse(ratingMedia.Sum(x => x.Rating), ratingMedia.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);
long ratingSum = ratingsActorRoles.Sum(x => x.Rating) + ratingsCreatorRoles.Sum(x => x.Rating);
long ratingCount = ratingsActorRoles.Count() + ratingsCreatorRoles.Count();
return new RatingResponse(ratingSum, ratingCount);
}
#endregion
}

View File

@@ -59,9 +59,9 @@ public class SeriesQueryParameters : QueryParameters<SeriesResponse>
#region PUBLIC METHODS
#region PRIVATE METHODS
public override bool IsMeetingConditions(SeriesResponse item) =>
protected override bool IsMeetingConditions(SeriesResponse item) =>
(
TestStringWithRegex(item.Title, Title)
&&

View File

@@ -50,7 +50,7 @@ public class SeriesResponse : Series, IQueryOrderable<SeriesResponse>
ReleaseDate = mediaSeries.Media.ReleaseDate;
Length = mediaSeries.Media.Length;
HasEnded = mediaSeries.HasEnded;
Rating = new RatingResponse(mediaSeries.Media.RatingMedia);
Rating = RatingResponse.Create(mediaSeries.Media.RatingMedia);
}
#endregion

View File

@@ -122,7 +122,7 @@ public abstract class QueryParameters<T> : QueryParameters where T : IQueryOrder
{
#region PUBLIC METHODS
public abstract bool IsMeetingConditions(T item);
protected abstract bool IsMeetingConditions(T item);
public IEnumerable<T> PrepareData(IEnumerable<T> data)
{

View File

@@ -0,0 +1,14 @@
using Microsoft.AspNetCore.Mvc;
namespace WatchIt.WebAPI.Controllers;
[ApiController]
[Route("persons")]
public class PersonsController : ControllerBase
{
#region SERVICES
#endregion
}

View File

@@ -108,7 +108,7 @@ public class MediaControllerService(DatabaseContext database, IUserService userS
return RequestResult.NotFound();
}
RatingResponse ratingResponse = new RatingResponse(item.RatingMedia);
RatingResponse ratingResponse = RatingResponse.Create(item.RatingMedia);
return RequestResult.Ok(ratingResponse);
}

View File

@@ -0,0 +1,75 @@
using Microsoft.EntityFrameworkCore;
using WatchIt.Common.Model.Persons;
using WatchIt.Database;
using WatchIt.WebAPI.Services.Controllers.Common;
using WatchIt.WebAPI.Services.Utility.User;
using Person = WatchIt.Database.Model.Person.Person;
namespace WatchIt.WebAPI.Services.Controllers.Persons;
public class PersonsControllerService
{
#region SERVICES
private readonly DatabaseContext _database;
private readonly IUserService _userService;
#endregion
#region CONSTRUCTORS
public PersonsControllerService(DatabaseContext database, IUserService userService)
{
_database = database;
_userService = userService;
}
#endregion
#region PUBLIC METHODS
#region Main
public async Task<RequestResult> GetAllPersons(PersonQueryParameters query)
{
IEnumerable<Person> rawData = await _database.Persons.ToListAsync();
IEnumerable<PersonResponse> data = rawData.Select(x => new PersonResponse(x));
data = query.PrepareData(data);
return RequestResult.Ok(data);
}
public async Task<RequestResult> GetMovie(long id)
{
Person? item = await _database.Persons.FirstOrDefaultAsync(x => x.Id == id);
if (item is null)
{
return RequestResult.NotFound();
}
PersonResponse data = new PersonResponse(item);
return RequestResult.Ok(data);
}
public async Task<RequestResult> PostPerson(PersonRequest data)
{
UserValidator validator = _userService.GetValidator().MustBeAdmin();
if (!validator.IsValid)
{
return RequestResult.Forbidden();
}
Person personItem = data.CreatePerson();
await _database.Persons.AddAsync(personItem);
await _database.SaveChangesAsync();
return RequestResult.Created($"persons/{personItem.Id}", new PersonResponse(personItem));
}
#endregion
#endregion
}

View File

@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\WatchIt.Common\WatchIt.Common.Model\WatchIt.Common.Model.csproj" />
<ProjectReference Include="..\..\..\..\WatchIt.Database\WatchIt.Database\WatchIt.Database.csproj" />
<ProjectReference Include="..\..\WatchIt.WebAPI.Services.Utility\WatchIt.WebAPI.Services.Utility.User\WatchIt.WebAPI.Services.Utility.User.csproj" />
<ProjectReference Include="..\WatchIt.WebAPI.Services.Controllers.Common\WatchIt.WebAPI.Services.Controllers.Common.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,9 @@
using FluentValidation;
using WatchIt.Common.Model.Persons;
namespace WatchIt.WebAPI.Validators.Persons;
public class PersonRequestValidator : AbstractValidator<PersonRequest>
{
}

View File

@@ -88,6 +88,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WatchIt.WebAPI.Services.Con
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WatchIt.Website.Services.WebAPI.Photos", "WatchIt.Website\WatchIt.Website.Services\WatchIt.Website.Services.WebAPI\WatchIt.Website.Services.WebAPI.Photos\WatchIt.Website.Services.WebAPI.Photos.csproj", "{960A833F-C195-4D1D-AD4F-D00B57067181}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WatchIt.WebAPI.Services.Controllers.Persons", "WatchIt.WebAPI\WatchIt.WebAPI.Services\WatchIt.WebAPI.Services.Controllers\WatchIt.WebAPI.Services.Controllers.Persons\WatchIt.WebAPI.Services.Controllers.Persons.csproj", "{335686F5-65B8-4D66-AAA8-C5032906451D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -134,6 +136,7 @@ Global
{783C743A-85BF-4382-BFE5-7A90E3F3B8B6} = {46E3711F-18BD-4004-AF53-EA4D8643D92F}
{ABDF8471-2FAB-4930-B016-7DD3E48AE6B8} = {CEC468DB-CC49-47D3-9E3E-1CC9530C3CE7}
{960A833F-C195-4D1D-AD4F-D00B57067181} = {46E3711F-18BD-4004-AF53-EA4D8643D92F}
{335686F5-65B8-4D66-AAA8-C5032906451D} = {CEC468DB-CC49-47D3-9E3E-1CC9530C3CE7}
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{23383776-1F27-4B5D-8C7C-57BFF75FA473}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
@@ -264,5 +267,9 @@ Global
{960A833F-C195-4D1D-AD4F-D00B57067181}.Debug|Any CPU.Build.0 = Debug|Any CPU
{960A833F-C195-4D1D-AD4F-D00B57067181}.Release|Any CPU.ActiveCfg = Release|Any CPU
{960A833F-C195-4D1D-AD4F-D00B57067181}.Release|Any CPU.Build.0 = Release|Any CPU
{335686F5-65B8-4D66-AAA8-C5032906451D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{335686F5-65B8-4D66-AAA8-C5032906451D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{335686F5-65B8-4D66-AAA8-C5032906451D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{335686F5-65B8-4D66-AAA8-C5032906451D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal