From 70db09b2477bbb7474f55a379720b3d83d4b9f0a Mon Sep 17 00:00:00 2001 From: Zagrebin Date: Thu, 28 Nov 2024 10:36:11 +0300 Subject: [PATCH] Group API --- Presence.API/Controllers/GroupController.cs | 61 ++++++++++++++++++++- Presence.API/Presence.API.csproj | 4 -- Presence.API/Request/AddGroupRequest.cs | 7 +++ domain/Service/GroupService.cs | 4 +- domain/UseCase/IGroupUseCase.cs | 4 +- 5 files changed, 69 insertions(+), 11 deletions(-) create mode 100644 Presence.API/Request/AddGroupRequest.cs diff --git a/Presence.API/Controllers/GroupController.cs b/Presence.API/Controllers/GroupController.cs index 5272d88..7426d52 100644 --- a/Presence.API/Controllers/GroupController.cs +++ b/Presence.API/Controllers/GroupController.cs @@ -41,9 +41,64 @@ namespace Presence.API.Controllers public IActionResult DeleteGroup(int id) { RemoveGroupRequest removeGroupRequest = new() { GroupId = id }; - _groupService.RemoveGroup(removeGroupRequest); - return NoContent(); + bool isDeleted = _groupService.RemoveGroup(removeGroupRequest); + if (!isDeleted) return NotFound(); + else return NoContent(); + } + + [HttpPost("/group")] + public ActionResult PostGroup(AddGroupRequest addGroupRequest) + { + if (addGroupRequest is null) + return BadRequest(new ArgumentNullException()); + + bool isCreated = _groupService.AddGroup(addGroupRequest); + if (isCreated) return CreatedAtAction(nameof(GetGroupByName), new { name = addGroupRequest.Name }, addGroupRequest ); + else return BadRequest(); + } +/* + [HttpGet("/group/{id}")] + public ActionResult GetGroupById(int id) + { + var result = _groupService + .GetGroupsWithStudents() + .Where(g => g.Id == id) + .Select(group => new GroupResponse + { + Id = group.Id, + Name = group.Name, + Users = group.Users?.Select( + user => new UserResponse + { + Id = user.Id, + LastName = user.LastName, + FirstName = user.FirstName, + Patronymic = user.Patronymic + }).ToList() + }).ToList(); + return result.Count > 0 ? Ok(result) : NotFound(); + } +*/ + [HttpGet("/group/{name}")] + public ActionResult GetGroupByName(string name) + { + var result = _groupService + .GetGroupsWithStudents() + .Where(g => g.Name == name) + .Select(group => new GroupResponse + { + Id = group.Id, + Name = group.Name, + Users = group.Users?.Select( + user => new UserResponse + { + Id = user.Id, + LastName = user.LastName, + FirstName = user.FirstName, + Patronymic = user.Patronymic + }).ToList() + }).ToList(); + return result.Count > 0 ? Ok(result) : NotFound(); } - } } diff --git a/Presence.API/Presence.API.csproj b/Presence.API/Presence.API.csproj index 01b9d0e..754e1e0 100644 --- a/Presence.API/Presence.API.csproj +++ b/Presence.API/Presence.API.csproj @@ -10,10 +10,6 @@ - - - - diff --git a/Presence.API/Request/AddGroupRequest.cs b/Presence.API/Request/AddGroupRequest.cs new file mode 100644 index 0000000..2709e7d --- /dev/null +++ b/Presence.API/Request/AddGroupRequest.cs @@ -0,0 +1,7 @@ +namespace Presence.API.Request +{ + public class AddGroupRequest + { + public string Name { get; set; } + } +} diff --git a/domain/Service/GroupService.cs b/domain/Service/GroupService.cs index 507fb28..fa3a308 100644 --- a/domain/Service/GroupService.cs +++ b/domain/Service/GroupService.cs @@ -19,7 +19,7 @@ namespace domain.Service _groupRepository = groupRepository; } - public void AddGroup(AddGroupRequest addGroupRequest) + public bool AddGroup(AddGroupRequest addGroupRequest) => _groupRepository.CreateGroup(new Group { Name = addGroupRequest.Name }); @@ -60,7 +60,7 @@ namespace domain.Service }); } - public void RemoveGroup(RemoveGroupRequest removeGroupRequest) + public bool RemoveGroup(RemoveGroupRequest removeGroupRequest) => _groupRepository.DeleteGroup(removeGroupRequest.GroupId); } } diff --git a/domain/UseCase/IGroupUseCase.cs b/domain/UseCase/IGroupUseCase.cs index 3dd452f..7589a56 100644 --- a/domain/UseCase/IGroupUseCase.cs +++ b/domain/UseCase/IGroupUseCase.cs @@ -10,11 +10,11 @@ namespace domain.UseCase { public interface IGroupUseCase { - public void AddGroup(AddGroupRequest addGroupRequest); + public bool AddGroup(AddGroupRequest addGroupRequest); public void AddGroupWithStudents(AddGroupWithStudentRequest addGroupWithStudentRequest); - public void RemoveGroup(RemoveGroupRequest removeGroupRequest); + public bool RemoveGroup(RemoveGroupRequest removeGroupRequest); public void EditGroup(EditGroupRequest editGroupRequest);