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,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>();