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 _logger; public AdminController(GroupUseCase groupUseCase, UserUseCase userUseCase, PresenceUseCase presenceUseCase, ILogger logger){ _groupUseCase = groupUseCase; _userUseCase = userUseCase; _presenceUseCase = presenceUseCase; _logger = logger; } //post [HttpPost] public ActionResult 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 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 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 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 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 DeletePresence(){ return Ok(_presenceUseCase.DeletePresence()); } //get [HttpGet] public ActionResult> getGroupsWithUsers() { return Ok(_groupUseCase.GetAllGroupsWithUsers()); } [HttpGet("user/{userGuid}")] public ActionResult GetUserByGuid(Guid userGuid) { var user = _userUseCase.GetUserByGuid(userGuid); if (user != null){ return Ok(user); } else{ _logger.LogWarning("NotFound"); return NotFound("User not found"); } } }