person controller finished

This commit is contained in:
2024-10-03 16:18:17 +02:00
Unverified
parent 9a9e29ecf4
commit 4f16596ea3
14 changed files with 379 additions and 2 deletions

View File

@@ -25,5 +25,15 @@ public class PersonRequest : Person
GenderId = GenderId,
};
public void UpdatePerson(Database.Model.Person.Person person)
{
person.Name = Name;
person.FullName = FullName;
person.Description = Description;
person.BirthDate = BirthDate;
person.DeathDate = DeathDate;
person.GenderId = GenderId;
}
#endregion
}

View File

@@ -1,4 +1,9 @@
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WatchIt.Common.Model.Persons;
using WatchIt.WebAPI.Services.Controllers.Persons;
namespace WatchIt.WebAPI.Controllers;
@@ -8,7 +13,72 @@ public class PersonsController : ControllerBase
{
#region SERVICES
private readonly IPersonsControllerService _personsControllerService;
#endregion
#region CONSTRUCTORS
public PersonsController(IPersonsControllerService personsControllerService)
{
_personsControllerService = personsControllerService;
}
#endregion
#region METHODS
#region Main
[HttpGet]
[AllowAnonymous]
[ProducesResponseType(typeof(IEnumerable<PersonResponse>), StatusCodes.Status200OK)]
public async Task<ActionResult> GetAllMovies(PersonQueryParameters query) => await _personsControllerService.GetAllPersons(query);
[HttpGet("{id}")]
[AllowAnonymous]
[ProducesResponseType(typeof(PersonResponse), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult> GetMovie([FromRoute] long id) => await _personsControllerService.GetPerson(id);
[HttpPost]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[ProducesResponseType(typeof(PersonResponse), StatusCodes.Status201Created)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
public async Task<ActionResult> PostMovie([FromBody] PersonRequest body) => await _personsControllerService.PostPerson(body);
[HttpPut("{id}")]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[ProducesResponseType(typeof(void), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
public async Task<ActionResult> PutMovie([FromRoute] long id, [FromBody]PersonRequest body) => await _personsControllerService.PutPerson(id, body);
[HttpDelete("{id}")]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[ProducesResponseType(typeof(void), StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
public async Task<ActionResult> DeleteMovie([FromRoute] long id) => await _personsControllerService.DeletePerson(id);
#endregion
#region View count
[HttpGet("view")]
[AllowAnonymous]
[ProducesResponseType(typeof(IEnumerable<PersonResponse>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<ActionResult> GetMoviesViewRank([FromQuery] int first = 5, [FromQuery] int days = 7) => await _personsControllerService.GetPersonsViewRank(first, days);
#endregion
#endregion
}

View File

@@ -17,6 +17,7 @@
<ProjectReference Include="..\WatchIt.WebAPI.Services\WatchIt.WebAPI.Services.Controllers\WatchIt.WebAPI.Services.Controllers.Genres\WatchIt.WebAPI.Services.Controllers.Genres.csproj" />
<ProjectReference Include="..\WatchIt.WebAPI.Services\WatchIt.WebAPI.Services.Controllers\WatchIt.WebAPI.Services.Controllers.Media\WatchIt.WebAPI.Services.Controllers.Media.csproj" />
<ProjectReference Include="..\WatchIt.WebAPI.Services\WatchIt.WebAPI.Services.Controllers\WatchIt.WebAPI.Services.Controllers.Movies\WatchIt.WebAPI.Services.Controllers.Movies.csproj" />
<ProjectReference Include="..\WatchIt.WebAPI.Services\WatchIt.WebAPI.Services.Controllers\WatchIt.WebAPI.Services.Controllers.Persons\WatchIt.WebAPI.Services.Controllers.Persons.csproj" />
<ProjectReference Include="..\WatchIt.WebAPI.Services\WatchIt.WebAPI.Services.Controllers\WatchIt.WebAPI.Services.Controllers.Photos\WatchIt.WebAPI.Services.Controllers.Photos.csproj" />
<ProjectReference Include="..\WatchIt.WebAPI.Services\WatchIt.WebAPI.Services.Controllers\WatchIt.WebAPI.Services.Controllers.Series\WatchIt.WebAPI.Services.Controllers.Series.csproj" />
</ItemGroup>

View File

@@ -0,0 +1,14 @@
using WatchIt.Common.Model.Persons;
using WatchIt.WebAPI.Services.Controllers.Common;
namespace WatchIt.WebAPI.Services.Controllers.Persons;
public interface IPersonsControllerService
{
Task<RequestResult> GetAllPersons(PersonQueryParameters query);
Task<RequestResult> GetPerson(long id);
Task<RequestResult> PostPerson(PersonRequest data);
Task<RequestResult> PutPerson(long id, PersonRequest data);
Task<RequestResult> DeletePerson(long id);
Task<RequestResult> GetPersonsViewRank(int first, int days);
}

View File

@@ -7,7 +7,7 @@ using Person = WatchIt.Database.Model.Person.Person;
namespace WatchIt.WebAPI.Services.Controllers.Persons;
public class PersonsControllerService
public class PersonsControllerService : IPersonsControllerService
{
#region SERVICES
@@ -42,7 +42,7 @@ public class PersonsControllerService
return RequestResult.Ok(data);
}
public async Task<RequestResult> GetMovie(long id)
public async Task<RequestResult> GetPerson(long id)
{
Person? item = await _database.Persons.FirstOrDefaultAsync(x => x.Id == id);
if (item is null)
@@ -69,6 +69,77 @@ public class PersonsControllerService
return RequestResult.Created($"persons/{personItem.Id}", new PersonResponse(personItem));
}
public async Task<RequestResult> PutPerson(long id, PersonRequest data)
{
UserValidator validator = _userService.GetValidator().MustBeAdmin();
if (!validator.IsValid)
{
return RequestResult.Forbidden();
}
Person? item = await _database.Persons.FirstOrDefaultAsync(x => x.Id == id);
if (item is null)
{
return RequestResult.NotFound();
}
data.UpdatePerson(item);
await _database.SaveChangesAsync();
return RequestResult.Ok();
}
public async Task<RequestResult> DeletePerson(long id)
{
UserValidator validator = _userService.GetValidator().MustBeAdmin();
if (!validator.IsValid)
{
return RequestResult.Forbidden();
}
Person? item = await _database.Persons.FirstOrDefaultAsync(x => x.Id == id);
if (item is null)
{
return RequestResult.NotFound();
}
_database.PersonCreatorRoles.AttachRange(item.PersonCreatorRoles);
_database.PersonCreatorRoles.RemoveRange(item.PersonCreatorRoles);
_database.PersonActorRoles.AttachRange(item.PersonActorRoles);
_database.PersonActorRoles.RemoveRange(item.PersonActorRoles);
_database.ViewCountsPerson.AttachRange(item.ViewCountsPerson);
_database.ViewCountsPerson.RemoveRange(item.ViewCountsPerson);
_database.Persons.Attach(item);
_database.Persons.Remove(item);
await _database.SaveChangesAsync();
return RequestResult.NoContent();
}
#endregion
#region View count
public async Task<RequestResult> GetPersonsViewRank(int first, int days)
{
if (first < 1 || days < 1)
{
return RequestResult.BadRequest();
}
DateOnly startDate = DateOnly.FromDateTime(DateTime.Now).AddDays(-days);
IEnumerable<Person> rawData = await _database.Persons.OrderByDescending(x => x.ViewCountsPerson.Where(y => y.Date >= startDate)
.Sum(y => y.ViewCount))
.ThenBy(x => x.Id)
.Take(first)
.ToListAsync();
IEnumerable<PersonResponse> data = rawData.Select(x => new PersonResponse(x));
return RequestResult.Ok(data);
}
#endregion
#endregion

View File

@@ -13,6 +13,7 @@ using WatchIt.WebAPI.Services.Controllers.Accounts;
using WatchIt.WebAPI.Services.Controllers.Genres;
using WatchIt.WebAPI.Services.Controllers.Media;
using WatchIt.WebAPI.Services.Controllers.Movies;
using WatchIt.WebAPI.Services.Controllers.Persons;
using WatchIt.WebAPI.Services.Controllers.Photos;
using WatchIt.WebAPI.Services.Controllers.Series;
using WatchIt.WebAPI.Services.Utility.Configuration;
@@ -158,6 +159,7 @@ public static class Program
builder.Services.AddTransient<IMediaControllerService, MediaControllerService>();
builder.Services.AddTransient<ISeriesControllerService, SeriesControllerService>();
builder.Services.AddTransient<IPhotosControllerService, PhotosControllerService>();
builder.Services.AddTransient<IPersonsControllerService, PersonsControllerService>();
return builder;
}

View File

@@ -27,6 +27,7 @@
<ItemGroup>
<ProjectReference Include="..\..\WatchIt.Database\WatchIt.Database\WatchIt.Database.csproj" />
<ProjectReference Include="..\WatchIt.WebAPI.Controllers\WatchIt.WebAPI.Controllers.csproj" />
<ProjectReference Include="..\WatchIt.WebAPI.Services\WatchIt.WebAPI.Services.Controllers\WatchIt.WebAPI.Services.Controllers.Persons\WatchIt.WebAPI.Services.Controllers.Persons.csproj" />
<ProjectReference Include="..\WatchIt.WebAPI.Services\WatchIt.WebAPI.Services.Utility\WatchIt.WebAPI.Services.Utility.Configuration\WatchIt.WebAPI.Services.Utility.Configuration.csproj" />
<ProjectReference Include="..\WatchIt.WebAPI.Services\WatchIt.WebAPI.Services.Utility\WatchIt.WebAPI.Services.Utility.Tokens\WatchIt.WebAPI.Services.Utility.Tokens.csproj" />
<ProjectReference Include="..\WatchIt.WebAPI.Validators\WatchIt.WebAPI.Validators.csproj" />

View File

@@ -9,4 +9,5 @@ public class Endpoints
public Movies Movies { get; set; }
public Series Series { get; set; }
public Photos Photos { get; set; }
public Persons Persons { get; set; }
}

View File

@@ -0,0 +1,12 @@
namespace WatchIt.Website.Services.Utility.Configuration.Model;
public class Persons
{
public string Base { get; set; }
public string GetAllPersons { get; set; }
public string GetPerson { get; set; }
public string PostPerson { get; set; }
public string PutPerson { get; set; }
public string DeletePerson { get; set; }
public string GetPersonsViewRank { get; set; }
}

View File

@@ -0,0 +1,13 @@
using WatchIt.Common.Model.Persons;
namespace WatchIt.Website.Services.WebAPI.Persons;
public interface IPersonsWebAPIService
{
Task GetAllPersons(PersonQueryParameters? query = null, Action<IEnumerable<PersonResponse>>? successAction = null);
Task GetPerson(long id, Action<PersonResponse>? successAction = null, Action? notFoundAction = null);
Task PostPerson(PersonRequest data, Action<PersonResponse>? successAction = null, Action<IDictionary<string, string[]>>? badRequestAction = null, Action? unauthorizedAction = null, Action? forbiddenAction = null);
Task PutPerson(long id, PersonRequest data, Action? successAction = null, Action<IDictionary<string, string[]>>? badRequestAction = null, Action? unauthorizedAction = null, Action? forbiddenAction = null);
Task DeletePerson(long id, Action? successAction = null, Action? unauthorizedAction = null, Action? forbiddenAction = null);
Task GetPersonsViewRank(int? first = null, int? days = null, Action<IEnumerable<PersonResponse>>? successAction = null, Action<IDictionary<string, string[]>>? badRequestAction = null);
}

View File

@@ -0,0 +1,152 @@
using System.Text;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Primitives;
using WatchIt.Common.Model.Persons;
using WatchIt.Common.Services.HttpClient;
using WatchIt.Website.Services.Utility.Configuration;
using WatchIt.Website.Services.WebAPI.Common;
namespace WatchIt.Website.Services.WebAPI.Persons;
public class PersonsWebAPIService : BaseWebAPIService, IPersonsWebAPIService
{
#region SERVICES
private IHttpClientService _httpClientService;
private IConfigurationService _configurationService;
#endregion
#region CONSTRUCTORS
public PersonsWebAPIService(IHttpClientService httpClientService, IConfigurationService configurationService) : base(configurationService)
{
_httpClientService = httpClientService;
_configurationService = configurationService;
}
#endregion
#region PUBLIC METHODS
#region Main
public async Task GetAllPersons(PersonQueryParameters? query = null, Action<IEnumerable<PersonResponse>>? successAction = null)
{
string url = GetUrl(EndpointsConfiguration.Persons.GetAllPersons);
HttpRequest request = new HttpRequest(HttpMethodType.Get, url);
request.Query = query;
HttpResponse response = await _httpClientService.SendRequestAsync(request);
response.RegisterActionFor2XXSuccess(successAction)
.ExecuteAction();
}
public async Task GetPerson(long id, Action<PersonResponse>? successAction = null, Action? notFoundAction = null)
{
string url = GetUrl(EndpointsConfiguration.Persons.GetPerson, id);
HttpRequest request = new HttpRequest(HttpMethodType.Get, url);
HttpResponse response = await _httpClientService.SendRequestAsync(request);
response.RegisterActionFor2XXSuccess(successAction)
.RegisterActionFor404NotFound(notFoundAction)
.ExecuteAction();
}
public async Task PostPerson(PersonRequest data, Action<PersonResponse>? successAction = null, Action<IDictionary<string, string[]>>? badRequestAction = null, Action? unauthorizedAction = null, Action? forbiddenAction = null)
{
string url = GetUrl(EndpointsConfiguration.Persons.PostPerson);
HttpRequest request = new HttpRequest(HttpMethodType.Post, url);
request.Body = data;
HttpResponse response = await _httpClientService.SendRequestAsync(request);
response.RegisterActionFor2XXSuccess(successAction)
.RegisterActionFor400BadRequest(badRequestAction)
.RegisterActionFor401Unauthorized(unauthorizedAction)
.RegisterActionFor403Forbidden(forbiddenAction)
.ExecuteAction();
}
public async Task PutPerson(long id, PersonRequest data, Action? successAction = null, Action<IDictionary<string, string[]>>? badRequestAction = null, Action? unauthorizedAction = null, Action? forbiddenAction = null)
{
string url = GetUrl(EndpointsConfiguration.Persons.PutPerson, id);
HttpRequest request = new HttpRequest(HttpMethodType.Put, url);
request.Body = data;
HttpResponse response = await _httpClientService.SendRequestAsync(request);
response.RegisterActionFor2XXSuccess(successAction)
.RegisterActionFor400BadRequest(badRequestAction)
.RegisterActionFor401Unauthorized(unauthorizedAction)
.RegisterActionFor403Forbidden(forbiddenAction)
.ExecuteAction();
}
public async Task DeletePerson(long id, Action? successAction = null, Action? unauthorizedAction = null, Action? forbiddenAction = null)
{
string url = GetUrl(EndpointsConfiguration.Persons.DeletePerson, id);
HttpRequest request = new HttpRequest(HttpMethodType.Delete, url);
HttpResponse response = await _httpClientService.SendRequestAsync(request);
response.RegisterActionFor2XXSuccess(successAction)
.RegisterActionFor401Unauthorized(unauthorizedAction)
.RegisterActionFor403Forbidden(forbiddenAction)
.ExecuteAction();
}
#endregion
#region View count
public async Task GetPersonsViewRank(int? first = null, int? days = null, Action<IEnumerable<PersonResponse>>? successAction = null, Action<IDictionary<string, string[]>>? badRequestAction = null)
{
string url = GetUrl(EndpointsConfiguration.Persons.GetPersonsViewRank);
if (first.HasValue || days.HasValue)
{
StringBuilder urlBuilder = new StringBuilder(url);
urlBuilder.Append('?');
bool firstParameter = true;
if (first.HasValue)
{
urlBuilder.Append($"first={first.Value}");
firstParameter = false;
}
if (days.HasValue)
{
if (!firstParameter)
{
urlBuilder.Append('&');
}
urlBuilder.Append($"days={days.Value}");
}
url = urlBuilder.ToString();
}
HttpRequest request = new HttpRequest(HttpMethodType.Get, url);
HttpResponse response = await _httpClientService.SendRequestAsync(request);
response.RegisterActionFor2XXSuccess(successAction)
.RegisterActionFor400BadRequest(badRequestAction)
.ExecuteAction();
}
#endregion
#endregion
#region PRIVATE METHODS
protected override string GetServiceBase() => EndpointsConfiguration.Persons.Base;
#endregion
}

View File

@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\WatchIt.Common\WatchIt.Common.Services\WatchIt.Common.Services.HttpClient\WatchIt.Common.Services.HttpClient.csproj" />
<ProjectReference Include="..\WatchIt.Website.Services.WebAPI.Common\WatchIt.Website.Services.WebAPI.Common.csproj" />
</ItemGroup>
</Project>

View File

@@ -70,6 +70,15 @@
"DeletePhoto": "/{0}",
"PutPhotoBackgroundData": "/{0}/background_data",
"DeletePhotoBackgroundData": "/{0}/background_data"
},
"Persons": {
"Base": "/persons",
"GetAllPersons": "",
"GetPerson": "/{0}",
"PostPerson": "",
"PutPerson": "/{0}",
"DeletePerson": "/{0}",
"GetPersonsViewRank": "/view"
}
}
}

View File

@@ -90,6 +90,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WatchIt.Website.Services.We
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
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WatchIt.Website.Services.WebAPI.Persons", "WatchIt.Website\WatchIt.Website.Services\WatchIt.Website.Services.WebAPI\WatchIt.Website.Services.WebAPI.Persons\WatchIt.Website.Services.WebAPI.Persons.csproj", "{83D42D72-FF67-4577-8280-2ABD5B20F985}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -137,6 +139,7 @@ Global
{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}
{83D42D72-FF67-4577-8280-2ABD5B20F985} = {46E3711F-18BD-4004-AF53-EA4D8643D92F}
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{23383776-1F27-4B5D-8C7C-57BFF75FA473}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
@@ -271,5 +274,9 @@ Global
{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
{83D42D72-FF67-4577-8280-2ABD5B20F985}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{83D42D72-FF67-4577-8280-2ABD5B20F985}.Debug|Any CPU.Build.0 = Debug|Any CPU
{83D42D72-FF67-4577-8280-2ABD5B20F985}.Release|Any CPU.ActiveCfg = Release|Any CPU
{83D42D72-FF67-4577-8280-2ABD5B20F985}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal