2024-11-25 04:33:26 +00:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Demo.Data.RemoteData.RemoteDataBase.DAO;
|
|
|
|
|
using Demo.Domain.UseCase;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace Demo.API.Controllers
|
|
|
|
|
{
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
|
public class AdminController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly UserUseCase _userUseCase;
|
|
|
|
|
private readonly GroupUseCase _groupUseCase;
|
|
|
|
|
private readonly UseCasePresence _presenceUseCase;
|
|
|
|
|
|
|
|
|
|
public AdminController(UserUseCase userUseCase, GroupUseCase groupUseCase, UseCasePresence presenceUseCase)
|
|
|
|
|
{
|
|
|
|
|
_userUseCase = userUseCase;
|
|
|
|
|
_groupUseCase = groupUseCase;
|
|
|
|
|
_presenceUseCase = presenceUseCase;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-02 10:24:02 +00:00
|
|
|
|
[HttpGet("getAllUsers")]
|
|
|
|
|
public IActionResult GetAllUsers()
|
2024-11-25 04:33:26 +00:00
|
|
|
|
{
|
2024-12-02 10:24:02 +00:00
|
|
|
|
var users = _userUseCase.GetAllUsers();
|
|
|
|
|
return Ok(users);
|
2024-11-25 04:33:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-02 10:24:02 +00:00
|
|
|
|
[HttpPost("addGroup")]
|
|
|
|
|
public IActionResult AddGroup([FromBody] GroupWithStudents groupWithStudents)
|
2024-11-25 04:33:26 +00:00
|
|
|
|
{
|
2024-12-02 10:24:02 +00:00
|
|
|
|
var group = new Group { Name = groupWithStudents.GroupName };
|
|
|
|
|
_groupUseCase.AddGroup(group);
|
|
|
|
|
|
|
|
|
|
foreach (var student in groupWithStudents.Students)
|
|
|
|
|
{
|
|
|
|
|
student.GroupID = group.Id;
|
|
|
|
|
_userUseCase.UpdateUser(student);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-25 04:33:26 +00:00
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-02 10:24:02 +00:00
|
|
|
|
[HttpDelete("deleteUser")]
|
|
|
|
|
public IActionResult DeleteUser([FromQuery] Guid id)
|
2024-11-25 04:33:26 +00:00
|
|
|
|
{
|
|
|
|
|
_userUseCase.DeleteUser(id);
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-02 10:24:02 +00:00
|
|
|
|
[HttpDelete("deleteGroup")]
|
|
|
|
|
public IActionResult DeleteGroup([FromQuery] int id)
|
2024-11-25 04:33:26 +00:00
|
|
|
|
{
|
2024-12-02 10:24:02 +00:00
|
|
|
|
_groupUseCase.DeleteGroup(id);
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpDelete("clearPresence")]
|
|
|
|
|
public IActionResult ClearPresence([FromQuery] int? groupId, [FromQuery] Guid? userId, [FromQuery] DateTime? startDate, [FromQuery] DateTime? endDate)
|
|
|
|
|
{
|
|
|
|
|
var presence = _presenceUseCase.PresenceRepository.GetAllPresence();
|
|
|
|
|
|
|
|
|
|
if (groupId.HasValue)
|
2024-11-25 04:33:26 +00:00
|
|
|
|
{
|
2024-12-02 10:24:02 +00:00
|
|
|
|
presence = presence.Where(p => _userUseCase.GetAllUsers().Any(u => u.Id == p.UserId && u.GroupID == groupId.Value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (userId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
presence = presence.Where(p => p.UserId == userId.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (startDate.HasValue)
|
|
|
|
|
{
|
|
|
|
|
presence = presence.Where(p => p.Date >= startDate.Value);
|
2024-11-25 04:33:26 +00:00
|
|
|
|
}
|
2024-12-02 10:24:02 +00:00
|
|
|
|
|
|
|
|
|
if (endDate.HasValue)
|
|
|
|
|
{
|
|
|
|
|
presence = presence.Where(p => p.Date <= endDate.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var p in presence)
|
|
|
|
|
{
|
|
|
|
|
_presenceUseCase.PresenceRepository.DeletePresence(p.Id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Ok();
|
2024-11-25 04:33:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-02 10:24:02 +00:00
|
|
|
|
[HttpGet("getAllGroupsWithStudents")]
|
|
|
|
|
public IActionResult GetAllGroupsWithStudents()
|
2024-11-25 04:33:26 +00:00
|
|
|
|
{
|
2024-12-02 10:24:02 +00:00
|
|
|
|
var groups = _groupUseCase.GetAllGroups();
|
2024-11-25 04:33:26 +00:00
|
|
|
|
var users = _userUseCase.GetAllUsers();
|
2024-12-02 10:24:02 +00:00
|
|
|
|
|
|
|
|
|
var result = groups.Select(g => new
|
|
|
|
|
{
|
|
|
|
|
Group = g,
|
|
|
|
|
Students = users.Where(u => u.GroupID == g.Id).ToList()
|
|
|
|
|
}).ToList();
|
|
|
|
|
|
|
|
|
|
return Ok(result);
|
2024-11-25 04:33:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-02 10:24:02 +00:00
|
|
|
|
[HttpGet("getUserInfo")]
|
|
|
|
|
public IActionResult GetUserInfo([FromQuery] Guid id)
|
2024-11-25 04:33:26 +00:00
|
|
|
|
{
|
2024-12-02 10:24:02 +00:00
|
|
|
|
var user = _userUseCase.GetUserById(id);
|
|
|
|
|
if (user == null)
|
2024-11-25 04:33:26 +00:00
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
2024-12-02 10:24:02 +00:00
|
|
|
|
|
|
|
|
|
var group = _groupUseCase.GetAllGroups().FirstOrDefault(g => g.Id == user.GroupID);
|
|
|
|
|
var presence = _presenceUseCase.GetPresenceByGroup(user.Id);
|
|
|
|
|
|
|
|
|
|
return Ok(new
|
|
|
|
|
{
|
|
|
|
|
User = user,
|
|
|
|
|
Group = group,
|
|
|
|
|
Presence = presence
|
|
|
|
|
});
|
2024-11-25 04:33:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-02 10:24:02 +00:00
|
|
|
|
|
|
|
|
|
public class GroupWithStudents
|
|
|
|
|
{
|
|
|
|
|
public string GroupName { get; set; }
|
|
|
|
|
public List<User> Students { get; set; }
|
|
|
|
|
}
|
2024-11-25 04:33:26 +00:00
|
|
|
|
}
|