Group API

This commit is contained in:
Zagrebin 2024-11-28 10:36:11 +03:00
parent 75492cea54
commit 70db09b247
5 changed files with 69 additions and 11 deletions

View File

@ -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<GroupResponse> 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<GroupResponse> 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<GroupResponse> 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();
}
}
}

View File

@ -10,10 +10,6 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
</ItemGroup>
<ItemGroup>
<Folder Include="Request\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\data\data.csproj" />
<ProjectReference Include="..\domain\domain.csproj" />

View File

@ -0,0 +1,7 @@
namespace Presence.API.Request
{
public class AddGroupRequest
{
public string Name { get; set; }
}
}

View File

@ -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);
}
}

View File

@ -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);