From eb8d87dfe6306d6d33dec636ea0e0bf50f3780eb Mon Sep 17 00:00:00 2001 From: Mateusz Skoczek Date: Sun, 6 Oct 2024 22:02:13 +0200 Subject: [PATCH] roles endpoints in roles controller added --- .../Roles/ActorRoleRequest.cs | 8 ++ .../Roles/CreatorRoleRequest.cs | 7 ++ .../RolesController.cs | 50 +++++++- .../IRolesControllerService.cs | 14 ++- .../RolesControllerService.cs | 108 +++++++++++++++++- 5 files changed, 177 insertions(+), 10 deletions(-) diff --git a/WatchIt.Common/WatchIt.Common.Model/Roles/ActorRoleRequest.cs b/WatchIt.Common/WatchIt.Common.Model/Roles/ActorRoleRequest.cs index cfb7c1d..c51f77c 100644 --- a/WatchIt.Common/WatchIt.Common.Model/Roles/ActorRoleRequest.cs +++ b/WatchIt.Common/WatchIt.Common.Model/Roles/ActorRoleRequest.cs @@ -25,6 +25,14 @@ public class ActorRoleRequest : ActorRole, IActorRoleMediaRequest, IActorRolePer PersonActorRoleTypeId = TypeId, RoleName = Name, }; + + public void UpdateActorRole(PersonActorRole item) + { + item.MediaId = MediaId; + item.PersonId = PersonId; + item.PersonActorRoleTypeId = TypeId; + item.RoleName = Name; + } #endregion } \ No newline at end of file diff --git a/WatchIt.Common/WatchIt.Common.Model/Roles/CreatorRoleRequest.cs b/WatchIt.Common/WatchIt.Common.Model/Roles/CreatorRoleRequest.cs index 6021528..ad9fa5c 100644 --- a/WatchIt.Common/WatchIt.Common.Model/Roles/CreatorRoleRequest.cs +++ b/WatchIt.Common/WatchIt.Common.Model/Roles/CreatorRoleRequest.cs @@ -24,6 +24,13 @@ public class CreatorRoleRequest : CreatorRole, ICreatorRoleMediaRequest, ICreato PersonId = PersonId, PersonCreatorRoleTypeId = TypeId, }; + + public void UpdateCreatorRole(PersonCreatorRole item) + { + item.MediaId = MediaId; + item.PersonId = PersonId; + item.PersonCreatorRoleTypeId = TypeId; + } #endregion } \ No newline at end of file diff --git a/WatchIt.WebAPI/WatchIt.WebAPI.Controllers/RolesController.cs b/WatchIt.WebAPI/WatchIt.WebAPI.Controllers/RolesController.cs index f730a14..138a2dc 100644 --- a/WatchIt.WebAPI/WatchIt.WebAPI.Controllers/RolesController.cs +++ b/WatchIt.WebAPI/WatchIt.WebAPI.Controllers/RolesController.cs @@ -34,16 +34,37 @@ public class RolesController : ControllerBase #region Actor + [HttpGet("actor/{id}")] + [AllowAnonymous] + [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] + public async Task GetActorRole([FromRoute]Guid id) => await _rolesControllerService.GetActorRole(id); + + [HttpPut("actor/{id}")] + [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)] + [ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + public async Task PutActorRole([FromRoute]Guid id, [FromBody]ActorRoleRequest body) => await _rolesControllerService.PutActorRole(id, body); + + [HttpDelete("actor/{id}")] + [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)] + [ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)] + public async Task DeleteActorRole([FromRoute]Guid id) => await _rolesControllerService.DeleteActorRole(id); + [HttpGet("actor/type")] [AllowAnonymous] [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] public async Task GetAllActorRoleTypes(RoleTypeQueryParameters query) => await _rolesControllerService.GetAllActorRoleTypes(query); - [HttpGet("actor/type/{id}")] + [HttpGet("actor/type/{type_id}")] [AllowAnonymous] [ProducesResponseType(typeof(RoleTypeResponse), StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] - public async Task GetActorRoleType([FromRoute]short id) => await _rolesControllerService.GetActorRoleType(id); + public async Task GetActorRoleType([FromRoute(Name = "type_id")]short typeId) => await _rolesControllerService.GetActorRoleType(typeId); [HttpPost("actor/type")] [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] @@ -53,17 +74,38 @@ public class RolesController : ControllerBase [ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)] public async Task PostActorRoleType([FromBody]RoleTypeRequest body) => await _rolesControllerService.PostActorRoleType(body); - [HttpDelete("actor/type/{id}")] + [HttpDelete("actor/type/{type_id}")] [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)] [ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)] - public async Task DeleteActorRoleType([FromRoute]short id) => await _rolesControllerService.DeleteActorRoleType(id); + public async Task DeleteActorRoleType([FromRoute(Name = "type_id")]short typeId) => await _rolesControllerService.DeleteActorRoleType(typeId); #endregion #region Creator + [HttpGet("creator/{id}")] + [AllowAnonymous] + [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] + public async Task GetCreatorRole([FromRoute]Guid id) => await _rolesControllerService.GetCreatorRole(id); + + [HttpPut("creator/{id}")] + [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)] + [ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + public async Task PutCreatorRole([FromRoute]Guid id, [FromBody]CreatorRoleRequest body) => await _rolesControllerService.PutCreatorRole(id, body); + + [HttpDelete("creator/{id}")] + [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)] + [ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)] + public async Task DeleteCreatorRole([FromRoute]Guid id) => await _rolesControllerService.DeleteCreatorRole(id); + [HttpGet("creator/type")] [AllowAnonymous] [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] diff --git a/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Roles/IRolesControllerService.cs b/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Roles/IRolesControllerService.cs index 7193002..6841b05 100644 --- a/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Roles/IRolesControllerService.cs +++ b/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Roles/IRolesControllerService.cs @@ -5,13 +5,19 @@ namespace WatchIt.WebAPI.Services.Controllers.Roles; public interface IRolesControllerService { + Task GetActorRole(Guid id); + Task PutActorRole(Guid id, ActorRoleRequest data); + Task DeleteActorRole(Guid id); Task GetAllActorRoleTypes(RoleTypeQueryParameters query); - Task GetActorRoleType(short id); + Task GetActorRoleType(short typeId); Task PostActorRoleType(RoleTypeRequest data); - Task DeleteActorRoleType(short id); + Task DeleteActorRoleType(short typeId); + Task GetCreatorRole(Guid id); + Task PutCreatorRole(Guid id, CreatorRoleRequest data); + Task DeleteCreatorRole(Guid id); Task GetAllCreatorRoleTypes(RoleTypeQueryParameters query); - Task GetCreatorRoleType(short id); + Task GetCreatorRoleType(short typeId); Task PostCreatorRoleType(RoleTypeRequest data); - Task DeleteCreatorRoleType(short id); + Task DeleteCreatorRoleType(short typeId); } \ No newline at end of file diff --git a/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Roles/RolesControllerService.cs b/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Roles/RolesControllerService.cs index 969410a..f004d9b 100644 --- a/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Roles/RolesControllerService.cs +++ b/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Roles/RolesControllerService.cs @@ -34,6 +34,59 @@ public class RolesControllerService : IRolesControllerService #region Actor + public async Task GetActorRole(Guid id) + { + PersonActorRole? item = await _database.PersonActorRoles.FirstOrDefaultAsync(x => x.Id == id); + if (item is null) + { + return RequestResult.NotFound(); + } + + ActorRoleResponse data = new ActorRoleResponse(item); + return RequestResult.Ok(data); + } + + public async Task PutActorRole(Guid id, ActorRoleRequest data) + { + UserValidator validator = _userService.GetValidator().MustBeAdmin(); + if (!validator.IsValid) + { + return RequestResult.Forbidden(); + } + + PersonActorRole? item = await _database.PersonActorRoles.FirstOrDefaultAsync(x => x.Id == id); + if (item is null) + { + return RequestResult.NotFound(); + } + + data.UpdateActorRole(item); + await _database.SaveChangesAsync(); + + return RequestResult.Ok(); + } + + public async Task DeleteActorRole(Guid id) + { + UserValidator validator = _userService.GetValidator().MustBeAdmin(); + if (!validator.IsValid) + { + return RequestResult.Forbidden(); + } + + PersonActorRole? item = await _database.PersonActorRoles.FirstOrDefaultAsync(x => x.Id == id); + if (item is null) + { + return RequestResult.NoContent(); + } + + _database.PersonActorRoles.Attach(item); + _database.PersonActorRoles.Remove(item); + await _database.SaveChangesAsync(); + + return RequestResult.NoContent(); + } + public async Task GetAllActorRoleTypes(RoleTypeQueryParameters query) { IEnumerable rawData = await _database.PersonActorRoleTypes.ToListAsync(); @@ -92,10 +145,61 @@ public class RolesControllerService : IRolesControllerService #endregion - - #region Creator + public async Task GetCreatorRole(Guid id) + { + PersonCreatorRole? item = await _database.PersonCreatorRoles.FirstOrDefaultAsync(x => x.Id == id); + if (item is null) + { + return RequestResult.NotFound(); + } + + CreatorRoleResponse data = new CreatorRoleResponse(item); + return RequestResult.Ok(data); + } + + public async Task PutCreatorRole(Guid id, CreatorRoleRequest data) + { + UserValidator validator = _userService.GetValidator().MustBeAdmin(); + if (!validator.IsValid) + { + return RequestResult.Forbidden(); + } + + PersonCreatorRole? item = await _database.PersonCreatorRoles.FirstOrDefaultAsync(x => x.Id == id); + if (item is null) + { + return RequestResult.NotFound(); + } + + data.UpdateCreatorRole(item); + await _database.SaveChangesAsync(); + + return RequestResult.Ok(); + } + + public async Task DeleteCreatorRole(Guid id) + { + UserValidator validator = _userService.GetValidator().MustBeAdmin(); + if (!validator.IsValid) + { + return RequestResult.Forbidden(); + } + + PersonCreatorRole? item = await _database.PersonCreatorRoles.FirstOrDefaultAsync(x => x.Id == id); + if (item is null) + { + return RequestResult.NoContent(); + } + + _database.PersonCreatorRoles.Attach(item); + _database.PersonCreatorRoles.Remove(item); + await _database.SaveChangesAsync(); + + return RequestResult.NoContent(); + } + public async Task GetAllCreatorRoleTypes(RoleTypeQueryParameters query) { IEnumerable rawData = await _database.PersonCreatorRoleTypes.ToListAsync();