2024-11-08 13:14:43 +00:00
|
|
|
using Demo.Domain.Models;
|
|
|
|
using Demo.Domain.UseCase;
|
2024-11-08 06:16:20 +00:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2024-11-08 13:14:43 +00:00
|
|
|
using Npgsql.TypeMapping;
|
2024-11-08 06:16:20 +00:00
|
|
|
|
|
|
|
namespace presence_api.Controllers;
|
|
|
|
[ApiController]
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
|
|
|
|
public class AdminController: ControllerBase{
|
2024-11-08 13:14:43 +00:00
|
|
|
private readonly GroupUseCase _groupUseCase;
|
|
|
|
private readonly UserUseCase _userUseCase;
|
|
|
|
private readonly PresenceUseCase _presenceUseCase;
|
2024-11-14 13:32:52 +00:00
|
|
|
private readonly ILogger<AdminController> _logger;
|
|
|
|
public AdminController(GroupUseCase groupUseCase, UserUseCase userUseCase, PresenceUseCase presenceUseCase, ILogger<AdminController> logger){
|
2024-11-08 13:14:43 +00:00
|
|
|
_groupUseCase = groupUseCase;
|
|
|
|
_userUseCase = userUseCase;
|
|
|
|
_presenceUseCase = presenceUseCase;
|
2024-11-14 12:30:27 +00:00
|
|
|
_logger = logger;
|
2024-11-08 13:14:43 +00:00
|
|
|
}
|
2024-11-08 06:16:20 +00:00
|
|
|
|
|
|
|
//post
|
2024-11-08 13:14:43 +00:00
|
|
|
[HttpPost]
|
|
|
|
public ActionResult<bool> CreateGroup([FromBody] GroupWithUsers request)
|
|
|
|
{
|
|
|
|
if (request == null || string.IsNullOrEmpty(request.GroupName))
|
|
|
|
{
|
2024-11-21 12:27:51 +00:00
|
|
|
_logger.LogWarning("CreateGroup: Invalid request");
|
2024-11-08 13:14:43 +00:00
|
|
|
return BadRequest("Invalid request");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isCreated = _groupUseCase.CreateGroup(request.GroupName);
|
|
|
|
|
|
|
|
|
|
|
|
foreach(var user in request.Users){
|
|
|
|
var usert = new User{FIO = user.FIO, Guid = Guid.NewGuid(), Group = user.Group};
|
|
|
|
_userUseCase.CreateUser(usert);
|
|
|
|
}
|
|
|
|
return Ok(isCreated);
|
|
|
|
}
|
2024-11-08 06:16:20 +00:00
|
|
|
|
|
|
|
//delete
|
2024-11-08 13:14:43 +00:00
|
|
|
[HttpDelete("user")]
|
|
|
|
public ActionResult<bool> DeleteUser(Guid userGuid){
|
|
|
|
if (userGuid == Guid.Empty){
|
2024-11-21 12:27:51 +00:00
|
|
|
_logger.LogWarning("DeleteUser: Invalid Guid or empty");
|
2024-11-08 13:14:43 +00:00
|
|
|
return BadRequest("Invalid request");
|
|
|
|
}
|
|
|
|
bool isDeleted = _userUseCase.RemoveUserByGuid(userGuid);
|
|
|
|
if (isDeleted == false){
|
2024-11-21 12:27:51 +00:00
|
|
|
_logger.LogWarning("DeleteUser: User with Guid '{UserGuid}' not found", userGuid);
|
2024-11-08 13:14:43 +00:00
|
|
|
return NotFound("User not found");
|
|
|
|
}
|
|
|
|
return Ok(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpDelete("users")]
|
|
|
|
public ActionResult<bool> DeleteUsers([FromBody] DeleteUsersRequest request){
|
|
|
|
if (request == null){
|
2024-11-21 12:27:51 +00:00
|
|
|
_logger.LogWarning("DeleteUsers: Invalid request");
|
2024-11-08 13:14:43 +00:00
|
|
|
return BadRequest("Invalid request");
|
|
|
|
}
|
|
|
|
foreach (Guid userGuid in request.UsersGuid)
|
|
|
|
{
|
|
|
|
bool isDeleted = _userUseCase.RemoveUserByGuid(userGuid);
|
|
|
|
if (isDeleted == false){
|
2024-11-21 12:27:51 +00:00
|
|
|
_logger.LogWarning("DeleteUsers: User with Guid '{UserGuid}' not found", userGuid);
|
2024-11-08 13:14:43 +00:00
|
|
|
return NotFound("User not found");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Ok(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpDelete("group")]
|
|
|
|
public ActionResult<bool> DeleteGroup(int GroupID){
|
|
|
|
bool isDeleted = _groupUseCase.RemoveGroupByID(GroupID);
|
|
|
|
if (isDeleted == false){
|
2024-11-21 12:27:51 +00:00
|
|
|
_logger.LogWarning("DeleteGroup: Group with GroupID '{GroupID}' not found", GroupID);
|
2024-11-08 13:14:43 +00:00
|
|
|
return NotFound("Group not found");
|
|
|
|
}
|
|
|
|
return Ok(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpDelete("groups")]
|
|
|
|
public ActionResult<bool> DeleteGroups([FromBody] DeleteGroupsRequest request){
|
|
|
|
if (request == null){
|
2024-11-21 12:27:51 +00:00
|
|
|
_logger.LogWarning("DeleteGroups: Invalid request");
|
2024-11-08 13:14:43 +00:00
|
|
|
return BadRequest("Invalid request");
|
|
|
|
}
|
|
|
|
foreach (int GroupID in request.GroupIDs)
|
|
|
|
{
|
|
|
|
bool isDeleted = _groupUseCase.RemoveGroupByID(GroupID);
|
|
|
|
if (isDeleted == false){
|
2024-11-21 12:27:51 +00:00
|
|
|
_logger.LogWarning("DeleteGroup: Group with GroupID '{GroupID}' not found", GroupID);
|
2024-11-08 13:14:43 +00:00
|
|
|
return NotFound("Group not found");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Ok(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpDelete("presence")]
|
|
|
|
public ActionResult<bool> DeletePresence(){
|
|
|
|
return Ok(_presenceUseCase.DeletePresence());
|
|
|
|
}
|
2024-11-08 06:16:20 +00:00
|
|
|
|
|
|
|
//get
|
2024-11-08 13:14:43 +00:00
|
|
|
[HttpGet]
|
|
|
|
public ActionResult<IEnumerable<GroupCon>> getGroupsWithUsers()
|
|
|
|
{
|
|
|
|
return Ok(_groupUseCase.GetAllGroupsWithUsers());
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet("user/{userGuid}")]
|
|
|
|
public ActionResult<User> GetUserByGuid(Guid userGuid)
|
|
|
|
{
|
|
|
|
var user = _userUseCase.GetUserByGuid(userGuid);
|
|
|
|
if (user != null){
|
2024-11-21 12:27:51 +00:00
|
|
|
_logger.LogWarning("found");
|
2024-11-08 13:14:43 +00:00
|
|
|
return Ok(user);
|
|
|
|
} else{
|
2024-11-21 12:27:51 +00:00
|
|
|
_logger.LogWarning("GetUserByGuid: User with Guid '{UserGuid}' not found", userGuid);
|
2024-11-08 13:14:43 +00:00
|
|
|
return NotFound("User not found");
|
|
|
|
}
|
|
|
|
}
|
2024-11-08 06:16:20 +00:00
|
|
|
}
|