presence/presence_api/Controllers/AdminController.cs
Your Name 8d435f71b5 Logs
2024-11-14 15:30:27 +03:00

124 lines
3.8 KiB
C#

using Demo.Domain.Models;
using Demo.Domain.UseCase;
using Microsoft.AspNetCore.Mvc;
using Npgsql.TypeMapping;
namespace presence_api.Controllers;
[ApiController]
[Route("api/[controller]")]
public class AdminController: ControllerBase{
private readonly GroupUseCase _groupUseCase;
private readonly UserUseCase _userUseCase;
private readonly PresenceUseCase _presenceUseCase;
private readonly ILogger<PresenceController> _logger;
public AdminController(GroupUseCase groupUseCase, UserUseCase userUseCase, PresenceUseCase presenceUseCase, ILogger<PresenceController> logger){
_groupUseCase = groupUseCase;
_userUseCase = userUseCase;
_presenceUseCase = presenceUseCase;
_logger = logger;
}
//post
[HttpPost]
public ActionResult<bool> CreateGroup([FromBody] GroupWithUsers request)
{
if (request == null || string.IsNullOrEmpty(request.GroupName))
{
_logger.LogWarning("NotFound");
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);
}
//delete
[HttpDelete("user")]
public ActionResult<bool> DeleteUser(Guid userGuid){
if (userGuid == Guid.Empty){
_logger.LogWarning("Invalid request");
return BadRequest("Invalid request");
}
bool isDeleted = _userUseCase.RemoveUserByGuid(userGuid);
if (isDeleted == false){
_logger.LogWarning("NotFound");
return NotFound("User not found");
}
return Ok(true);
}
[HttpDelete("users")]
public ActionResult<bool> DeleteUsers([FromBody] DeleteUsersRequest request){
if (request == null){
_logger.LogWarning("Invalid request");
return BadRequest("Invalid request");
}
foreach (Guid userGuid in request.UsersGuid)
{
bool isDeleted = _userUseCase.RemoveUserByGuid(userGuid);
if (isDeleted == false){
_logger.LogWarning("NotFound");
return NotFound("User not found");
}
}
return Ok(true);
}
[HttpDelete("group")]
public ActionResult<bool> DeleteGroup(int GroupID){
bool isDeleted = _groupUseCase.RemoveGroupByID(GroupID);
if (isDeleted == false){
_logger.LogWarning("NotFound");
return NotFound("Group not found");
}
return Ok(true);
}
[HttpDelete("groups")]
public ActionResult<bool> DeleteGroups([FromBody] DeleteGroupsRequest request){
if (request == null){
_logger.LogWarning("Invalid request");
return BadRequest("Invalid request");
}
foreach (int GroupID in request.GroupIDs)
{
bool isDeleted = _groupUseCase.RemoveGroupByID(GroupID);
if (isDeleted == false){
_logger.LogWarning("NotFound");
return NotFound("Group not found");
}
}
return Ok(true);
}
[HttpDelete("presence")]
public ActionResult<bool> DeletePresence(){
return Ok(_presenceUseCase.DeletePresence());
}
//get
[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){
return Ok(user);
} else{
_logger.LogWarning("NotFound");
return NotFound("User not found");
}
}
}