gender controller finished

This commit is contained in:
2024-10-05 09:12:28 +02:00
Unverified
parent b43b319855
commit 065c27642e
16 changed files with 389 additions and 1 deletions

View File

@@ -0,0 +1,22 @@
using Microsoft.AspNetCore.Mvc;
using WatchIt.Common.Query;
namespace WatchIt.Common.Model.Genders;
public class GenderQueryParameters : QueryParameters<GenderResponse>
{
#region PROPERTIES
[FromQuery(Name = "name")]
public string? Name { get; set; }
#endregion
#region PRIVATE METHODS
protected override bool IsMeetingConditions(GenderResponse item) => TestStringWithRegex(item.Name, Name);
#endregion
}

View File

@@ -0,0 +1,13 @@
namespace WatchIt.Common.Model.Genders;
public class GenderRequest : Gender
{
#region PUBLIC METHODS
public Database.Model.Common.Gender CreateGender() => new Database.Model.Common.Gender()
{
Name = Name
};
#endregion
}

View File

@@ -1,12 +1,20 @@
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;
using WatchIt.Common.Query;
namespace WatchIt.Common.Model.Genders;
public class GenderResponse : Gender
public class GenderResponse : Gender, IQueryOrderable<GenderResponse>
{
#region PROPERTIES
[JsonIgnore]
public static IDictionary<string, Func<GenderResponse, IComparable>> OrderableProperties { get; } = new Dictionary<string, Func<GenderResponse, IComparable>>
{
{ "name", item => item.Name }
};
[JsonPropertyName("id")]
public required short? Id { get; set; }

View File

@@ -0,0 +1,66 @@
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WatchIt.Common.Model.Genders;
using WatchIt.WebAPI.Services.Controllers.Genders;
namespace WatchIt.WebAPI.Controllers;
[ApiController]
[Route("genders")]
public class GendersController : ControllerBase
{
#region SERVICES
private readonly IGendersControllerService _gendersControllerService;
#endregion
#region CONSTRUCTORS
public GendersController(IGendersControllerService gendersControllerService)
{
_gendersControllerService = gendersControllerService;
}
#endregion
#region METHODS
#region Main
[HttpGet]
[AllowAnonymous]
[ProducesResponseType(typeof(IEnumerable<GenderResponse>), StatusCodes.Status200OK)]
public async Task<ActionResult> GetAllGenders(GenderQueryParameters query) => await _gendersControllerService.GetAllGenders(query);
[HttpGet("{id}")]
[AllowAnonymous]
[ProducesResponseType(typeof(GenderResponse), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult> GetGender([FromRoute]short id) => await _gendersControllerService.GetGender(id);
[HttpPost]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[ProducesResponseType(typeof(GenderResponse), StatusCodes.Status201Created)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
public async Task<ActionResult> PostGender([FromBody]GenderRequest body) => await _gendersControllerService.PostGender(body);
[HttpDelete("{id}")]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[ProducesResponseType(typeof(GenderResponse), StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
public async Task<ActionResult> DeleteGender([FromRoute]short id) => await _gendersControllerService.DeleteGender(id);
#endregion
#endregion
}

View File

@@ -14,6 +14,7 @@
<ItemGroup>
<ProjectReference Include="..\..\WatchIt.Common\WatchIt.Common.Model\WatchIt.Common.Model.csproj" />
<ProjectReference Include="..\WatchIt.WebAPI.Services\WatchIt.WebAPI.Services.Controllers\WatchIt.WebAPI.Services.Controllers.Accounts\WatchIt.WebAPI.Services.Controllers.Accounts.csproj" />
<ProjectReference Include="..\WatchIt.WebAPI.Services\WatchIt.WebAPI.Services.Controllers\WatchIt.WebAPI.Services.Controllers.Genders\WatchIt.WebAPI.Services.Controllers.Genders.csproj" />
<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" />

View File

@@ -0,0 +1,93 @@
using Microsoft.EntityFrameworkCore;
using WatchIt.Common.Model.Genders;
using WatchIt.Database;
using WatchIt.Database.Model.Common;
using WatchIt.WebAPI.Services.Controllers.Common;
using WatchIt.WebAPI.Services.Utility.User;
using Gender = WatchIt.Database.Model.Common.Gender;
namespace WatchIt.WebAPI.Services.Controllers.Genders;
public class GendersControllerService : IGendersControllerService
{
#region SERVICES
private readonly DatabaseContext _database;
private readonly IUserService _userService;
#endregion
#region CONSTRUCTORS
public GendersControllerService(DatabaseContext database, IUserService userService)
{
_database = database;
_userService = userService;
}
#endregion
#region PUBLIC METHODS
public async Task<RequestResult> GetAllGenders(GenderQueryParameters query)
{
IEnumerable<Gender> rawData = await _database.Genders.ToListAsync();
IEnumerable<GenderResponse> data = rawData.Select(x => new GenderResponse(x));
data = query.PrepareData(data);
return RequestResult.Ok(data);
}
public async Task<RequestResult> GetGender(short id)
{
Gender? item = await _database.Genders.FirstOrDefaultAsync(x => x.Id == id);
if (item is null)
{
return RequestResult.NotFound();
}
GenderResponse data = new GenderResponse(item);
return RequestResult.Ok(data);
}
public async Task<RequestResult> PostGender(GenderRequest data)
{
UserValidator validator = _userService.GetValidator().MustBeAdmin();
if (!validator.IsValid)
{
return RequestResult.Forbidden();
}
Gender item = data.CreateGender();
await _database.Genders.AddAsync(item);
await _database.SaveChangesAsync();
return RequestResult.Created($"genres/{item.Id}", new GenderResponse(item));
}
public async Task<RequestResult> DeleteGender(short id)
{
UserValidator validator = _userService.GetValidator().MustBeAdmin();
if (!validator.IsValid)
{
return RequestResult.Forbidden();
}
Gender? item = await _database.Genders.FirstOrDefaultAsync(x => x.Id == id);
if (item is null)
{
return RequestResult.NoContent();
}
_database.Genders.Attach(item);
_database.Genders.Remove(item);
await _database.SaveChangesAsync();
return RequestResult.NoContent();
}
#endregion
}

View File

@@ -0,0 +1,12 @@
using WatchIt.Common.Model.Genders;
using WatchIt.WebAPI.Services.Controllers.Common;
namespace WatchIt.WebAPI.Services.Controllers.Genders;
public interface IGendersControllerService
{
Task<RequestResult> GetAllGenders(GenderQueryParameters query);
Task<RequestResult> GetGender(short id);
Task<RequestResult> PostGender(GenderRequest data);
Task<RequestResult> DeleteGender(short id);
}

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

@@ -10,6 +10,7 @@ using Microsoft.IdentityModel.JsonWebTokens;
using Microsoft.IdentityModel.Tokens;
using WatchIt.Database;
using WatchIt.WebAPI.Services.Controllers.Accounts;
using WatchIt.WebAPI.Services.Controllers.Genders;
using WatchIt.WebAPI.Services.Controllers.Genres;
using WatchIt.WebAPI.Services.Controllers.Media;
using WatchIt.WebAPI.Services.Controllers.Movies;
@@ -154,6 +155,7 @@ public static class Program
// Controller
builder.Services.AddTransient<IAccountsControllerService, AccountsControllerService>();
builder.Services.AddTransient<IGendersControllerService, GendersControllerService>();
builder.Services.AddTransient<IGenresControllerService, GenresControllerService>();
builder.Services.AddTransient<IMoviesControllerService, MoviesControllerService>();
builder.Services.AddTransient<IMediaControllerService, MediaControllerService>();

View File

@@ -4,6 +4,7 @@ public class Endpoints
{
public string Base { get; set; }
public Accounts Accounts { get; set; }
public Genders Genders { get; set; }
public Genres Genres { get; set; }
public Media Media { get; set; }
public Movies Movies { get; set; }

View File

@@ -0,0 +1,10 @@
namespace WatchIt.Website.Services.Utility.Configuration.Model;
public class Genders
{
public string Base { get; set; }
public string GetAllGenders { get; set; }
public string GetGender { get; set; }
public string PostGender { get; set; }
public string DeleteGender { get; set; }
}

View File

@@ -0,0 +1,98 @@
using WatchIt.Common.Model.Genders;
using WatchIt.Common.Services.HttpClient;
using WatchIt.Website.Services.Utility.Configuration;
using WatchIt.Website.Services.WebAPI.Common;
namespace WatchIt.Website.Services.WebAPI.Genders;
public class GendersWebAPIService : BaseWebAPIService, IGendersWebAPIService
{
#region SERVICES
private IHttpClientService _httpClientService;
private IConfigurationService _configurationService;
#endregion
#region CONSTRUCTORS
public GendersWebAPIService(IHttpClientService httpClientService, IConfigurationService configurationService) : base(configurationService)
{
_httpClientService = httpClientService;
_configurationService = configurationService;
}
#endregion
#region PUBLIC METHODS
#region Main
public async Task GetAllGenders(GenderQueryParameters? query = null, Action<IEnumerable<GenderResponse>>? successAction = null)
{
string url = GetUrl(EndpointsConfiguration.Genders.GetAllGenders);
HttpRequest request = new HttpRequest(HttpMethodType.Get, url);
request.Query = query;
HttpResponse response = await _httpClientService.SendRequestAsync(request);
response.RegisterActionFor2XXSuccess(successAction)
.ExecuteAction();
}
public async Task GetGender(long id, Action<GenderResponse>? successAction = null, Action? notFoundAction = null)
{
string url = GetUrl(EndpointsConfiguration.Genders.GetGender, id);
HttpRequest request = new HttpRequest(HttpMethodType.Get, url);
HttpResponse response = await _httpClientService.SendRequestAsync(request);
response.RegisterActionFor2XXSuccess(successAction)
.RegisterActionFor404NotFound(notFoundAction)
.ExecuteAction();
}
public async Task PostGender(GenderRequest data, Action<GenderResponse>? successAction = null, Action<IDictionary<string, string[]>>? badRequestAction = null, Action? unauthorizedAction = null, Action? forbiddenAction = null)
{
string url = GetUrl(EndpointsConfiguration.Genders.PostGender);
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 DeleteGender(long id, Action? successAction = null, Action? unauthorizedAction = null, Action? forbiddenAction = null)
{
string url = GetUrl(EndpointsConfiguration.Genders.DeleteGender, id);
HttpRequest request = new HttpRequest(HttpMethodType.Delete, url);
HttpResponse response = await _httpClientService.SendRequestAsync(request);
response.RegisterActionFor2XXSuccess(successAction)
.RegisterActionFor401Unauthorized(unauthorizedAction)
.RegisterActionFor403Forbidden(forbiddenAction)
.ExecuteAction();
}
#endregion
#endregion
#region PRIVATE METHODS
protected override string GetServiceBase() => EndpointsConfiguration.Genders.Base;
#endregion
}

View File

@@ -0,0 +1,11 @@
using WatchIt.Common.Model.Genders;
namespace WatchIt.Website.Services.WebAPI.Genders;
public interface IGendersWebAPIService
{
Task GetAllGenders(GenderQueryParameters? query = null, Action<IEnumerable<GenderResponse>>? successAction = null);
Task GetGender(long id, Action<GenderResponse>? successAction = null, Action? notFoundAction = null);
Task PostGender(GenderRequest data, Action<GenderResponse>? successAction = null, Action<IDictionary<string, string[]>>? badRequestAction = null, Action? unauthorizedAction = null, Action? forbiddenAction = null);
Task DeleteGender(long id, Action? successAction = null, Action? unauthorizedAction = null, Action? forbiddenAction = null);
}

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

@@ -20,6 +20,13 @@
"Logout": "/logout",
"GetProfilePicture": "/{0}/profile-picture"
},
"Genders": {
"Base": "/genders",
"GetAllGenders": "",
"GetGender": "/{0}",
"PostGender": "",
"DeleteGender": "/{0}"
},
"Genres": {
"Base": "/genres",
"GetAll": "",

View File

@@ -92,6 +92,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WatchIt.WebAPI.Services.Con
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
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WatchIt.WebAPI.Services.Controllers.Genders", "WatchIt.WebAPI\WatchIt.WebAPI.Services\WatchIt.WebAPI.Services.Controllers\WatchIt.WebAPI.Services.Controllers.Genders\WatchIt.WebAPI.Services.Controllers.Genders.csproj", "{13BE36AB-2120-4F1B-815A-6F5E3F589EE8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WatchIt.Website.Services.WebAPI.Genders", "WatchIt.Website\WatchIt.Website.Services\WatchIt.Website.Services.WebAPI\WatchIt.Website.Services.WebAPI.Genders\WatchIt.Website.Services.WebAPI.Genders.csproj", "{B74144DE-EF62-430A-AB80-5D185DD03C05}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -140,6 +144,8 @@ Global
{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}
{13BE36AB-2120-4F1B-815A-6F5E3F589EE8} = {CEC468DB-CC49-47D3-9E3E-1CC9530C3CE7}
{B74144DE-EF62-430A-AB80-5D185DD03C05} = {46E3711F-18BD-4004-AF53-EA4D8643D92F}
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{23383776-1F27-4B5D-8C7C-57BFF75FA473}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
@@ -278,5 +284,13 @@ Global
{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
{13BE36AB-2120-4F1B-815A-6F5E3F589EE8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{13BE36AB-2120-4F1B-815A-6F5E3F589EE8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{13BE36AB-2120-4F1B-815A-6F5E3F589EE8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{13BE36AB-2120-4F1B-815A-6F5E3F589EE8}.Release|Any CPU.Build.0 = Release|Any CPU
{B74144DE-EF62-430A-AB80-5D185DD03C05}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B74144DE-EF62-430A-AB80-5D185DD03C05}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B74144DE-EF62-430A-AB80-5D185DD03C05}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B74144DE-EF62-430A-AB80-5D185DD03C05}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal