added presence api

This commit is contained in:
Dasha06 2024-11-18 18:54:36 +03:00
parent 5b98b00367
commit 8024021236
51 changed files with 307 additions and 62 deletions

View File

@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("console_ui")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+5b98b0036758c0bfbfd050b258dbaf603f2669f2")]
[assembly: System.Reflection.AssemblyProductAttribute("console_ui")]
[assembly: System.Reflection.AssemblyTitleAttribute("console_ui")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View File

@ -1 +1 @@
ba882f03fdfa9546ef7659464bc47e4a0ca2d53498595f377280140f0c41eed4
b49b12c1eb209457e0c1d0e3edaa7ba0ff70b6f92d0e9721fc4a71fd5a16073d

View File

@ -9,10 +9,15 @@ namespace presence.data.Repository
{
public interface IPresenceRepository
{
List<PresenceDao> GetPresence(int GroupId, DateOnly startData, DateOnly endData, int UserId);
List<PresenceDao> GetPresenceByGroup(int groupId);
List<PresenceDao> GetPresenceByGroupAndDate(int groupId, DateOnly date);
bool UncheckAttendence(int firstClass, int lastClass, DateOnly date, int userId);
bool AddPresence(PresenceDao presence);
bool DeletePresenceByGroup(int groupId);
bool DeletePresenceByUser(int userId);
bool DeletePresenceByDate(DateOnly startData, DateOnly endData);
}
}

View File

@ -33,6 +33,13 @@ namespace presence.data.Repository
return true;
}
public List<PresenceDao> GetPresence(int GroupId, DateOnly startData, DateOnly endData, int UserId)
{
var presenceList = _remoteDatabaseContext.Presences.Where(presence => presence.GroupId == GroupId
&& presence.UserId == UserId && presence.Date >= startData && presence.Date <= endData).ToList();
return presenceList;
}
public List<PresenceDao> GetPresenceByGroup(int groupId)
{
var listPresences = _remoteDatabaseContext.Presences
@ -59,5 +66,60 @@ namespace presence.data.Repository
_remoteDatabaseContext.SaveChanges();
return true;
}
public bool DeletePresenceByGroup(int groupId)
{
var presenceToDelete = _remoteDatabaseContext.Presences.Where(x => x.GroupId == groupId).ToList();
if (presenceToDelete.Count > 0)
{
foreach (var presence in presenceToDelete)
{
_remoteDatabaseContext.Presences.Remove(presence);
}
_remoteDatabaseContext.SaveChanges();
return true;
}
return false;
}
public bool DeletePresenceByUser(int userId)
{
var presenceToDelete = _remoteDatabaseContext.Presences.Where(x => x.UserId == userId).ToList();
if (presenceToDelete.Count > 0)
{
foreach (var presence in presenceToDelete)
{
_remoteDatabaseContext.Presences.Remove(presence);
}
_remoteDatabaseContext.SaveChanges();
return true;
}
return false;
}
public bool DeletePresenceByDate(DateOnly startData, DateOnly endData)
{
var PresenceToDelete = _remoteDatabaseContext.Presences.Where(x => x.Date == startData).ToList();
for (var i = startData; i < endData; i = i.AddDays(1))
{
PresenceToDelete.AddRange(_remoteDatabaseContext.Presences.Where(x => x.Date == i).ToList());
}
if (PresenceToDelete.Count > 0)
{
foreach (var presence in PresenceToDelete)
{
_remoteDatabaseContext.Presences.Remove(presence);
}
_remoteDatabaseContext.SaveChanges();
return true;
}
return false;
}
}
}

Binary file not shown.

Binary file not shown.

View File

@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("data")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+4bdc422ce8c208f21ae34f3d9f1cc61a98a48a3c")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+5b98b0036758c0bfbfd050b258dbaf603f2669f2")]
[assembly: System.Reflection.AssemblyProductAttribute("data")]
[assembly: System.Reflection.AssemblyTitleAttribute("data")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View File

@ -1 +1 @@
f31721b3835e5b512a8546f19c261266dec727598bee64acedc7558f74f5abd0
6f386b203319659ed0626eb565d13c3eafd1b1ae62135254883fcc52ed19b403

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace domain.Models.ResponseModels
{
public class PresenceResponse
{
public required DateOnly Date {get; set;}
public int ClassNumber {get; set;}
public bool IsAttendence {get; set;}
public required UserResponse User {get; set;}
}
}

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using domain.Models.ResponseModels;
using presence.data.LocalData.Entity;
using presence.data.RemoteData.RemoteDataBase.DAO;
using presence.data.Repository;
@ -25,15 +26,33 @@ namespace presence.domain.UseCase
_groupRepository = groupRepositoryImpl;
}
public List<Presence> GetPresenceByGroup(int groupId) {
public List<PresenceResponse> GetPresence(int GroupId, DateOnly startData, DateOnly endData, int UserId)
{
var users = _userRepository.GetAllUser().Where(x => x.GroupId == GroupId).ToList();
var presence = _presenceRepository.GetPresence(GroupId, startData, endData, UserId)
.Where(x => users.Any(user => user.UserId == x.UserId))
.Select(presence => new PresenceResponse{
User = new UserResponse{
Id = presence.UserId,
Group = new GroupResponse{Id = GroupId, Name = _groupRepository.GetGroupById(GroupId).Name},
FIO = users.First(user => user.UserId == presence.UserId).FIO,
},
ClassNumber = presence.ClassNumber,
Date = presence.Date,
IsAttendence = presence.IsAttendence
}).ToList();
return presence;
}
public List<PresenceResponse> GetPresenceByGroup(int groupId) {
var users = _userRepository.GetAllUser().Where(x => x.GroupId == groupId).ToList();
var presenceByGroup = _presenceRepository.GetPresenceByGroup(groupId)
.Where(x => users.Any(user => user.UserId == x.UserId))
.Select(presence => new Presence{
User = new User{
.Select(presence => new PresenceResponse{
User = new UserResponse{
Id = presence.UserId,
GroupId = new Group{Id = groupId, Name = _groupRepository.GetGroupById(groupId).Name},
Group = new GroupResponse{Id = groupId, Name = _groupRepository.GetGroupById(groupId).Name},
FIO = users.First(user => user.UserId == presence.UserId).FIO,
},
ClassNumber = presence.ClassNumber,
@ -65,27 +84,45 @@ namespace presence.domain.UseCase
return _presenceRepository.UncheckAttendence(firstClass, lastClass, date, userId);
}
public void AddPresence(int firstClass, int lastClass, int groupId,DateOnly date)
public List<PresenceResponse> AddPresence(String startDate, String endDate, int groupId)
{
var users = _userRepository.GetAllUser().Where(x => x.GroupId==groupId).ToList();
List<Presence> presenceList = new List<Presence>();
for (int i = firstClass; i < lastClass; i++)
var users = _userRepository.GetAllUser().Where(x => x.GroupId == groupId).ToList();
List<PresenceResponse> presenceList = new List<PresenceResponse>();
for (var currentDate = DateOnly.Parse(startDate); currentDate <= DateOnly.Parse(endDate); currentDate = currentDate.AddDays(1))
{
foreach (var user in users)
for (int i = 1; i < 8; i++)
{
Presence pres = new Presence{ClassNumber = i, Date = date,
User = new User{Id = user.UserId,
FIO = user.FIO,
GroupId = new Group{Id = groupId,
Name = _groupRepository.GetGroupById(groupId).Name}}};
presenceList.Add(pres);
}
if (currentDate.DayOfWeek == DayOfWeek.Saturday ||
currentDate.DayOfWeek == DayOfWeek.Sunday)
{
continue;
}
foreach (var user in users)
{
var presenceDao = new PresenceDao
{
ClassNumber = i,
Date = currentDate,
UserId = user.UserId,
User = user,
GroupId = groupId
};
_presenceRepository.AddPresence(presenceDao);
}
}
}
// Здесь можно добавить код для сохранения presenceList в репозиторий, если это необходимо
return presenceList;
}
public Dictionary<string, int> GetPresenceStatsByGroup(int groupId)
{
var stats = new Dictionary<string, int>();
// Получаем всех студентов группы
var users = _userRepository.GetAllUser().Where(x => x.GroupId == groupId).ToList();
stats["Количество студентов"] = users.Count;
@ -116,6 +153,7 @@ namespace presence.domain.UseCase
return stats;
}
public void GenerateWeeklyPresence(int firstClass, int lastClass, int groupId, DateOnly startDate)
{
// Получаем всех студентов группы
@ -152,5 +190,20 @@ namespace presence.domain.UseCase
}
}
}
public bool DeletePresenceByGroup(int groupId)
{
return _presenceRepository.DeletePresenceByGroup(groupId);
}
public bool DeletePresenceByUser(int UserId)
{
return _presenceRepository.DeletePresenceByUser(UserId);
}
public bool DeletePresenceByDate(DateOnly startData, DateOnly endData)
{
return _presenceRepository.DeletePresenceByDate(startData, endData);
}
}
}

View File

@ -26,14 +26,14 @@ namespace presence.domain.UseCase
private List<Group> GetAllGroups() => _repositoryGroupImpl.GetAllGroup()
.Select(it => new Group { Id = it.Id, Name = it.Name}).ToList();
public List<UserResponse> GetAllUsers() => _repositoryUserImpl.GetAllUser()
public List<User> GetAllUsers() => _repositoryUserImpl.GetAllUser()
.Join(_repositoryGroupImpl.GetAllGroup(),
user => user.GroupId,
group => group.Id,
(user, group) =>
new UserResponse { FIO = user.FIO,
new User { FIO = user.FIO,
Id = user.UserId,
Group = new GroupResponse{Id = group.Id, Name = group.Name}}
GroupId = new Group{Id = group.Id, Name = group.Name}}
).ToList();

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -13,10 +13,10 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("domain")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+4bdc422ce8c208f21ae34f3d9f1cc61a98a48a3c")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+5b98b0036758c0bfbfd050b258dbaf603f2669f2")]
[assembly: System.Reflection.AssemblyProductAttribute("domain")]
[assembly: System.Reflection.AssemblyTitleAttribute("domain")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
// Создано классом WriteCodeFragment MSBuild.
// Generated by the MSBuild WriteCodeFragment class.

View File

@ -1 +1 @@
a093ba0bf5d64479b23b12bae8cbb0ec7eeab3342133eaa4fe9b892857454774
32c088b7c6199415eae14640ae08a92e330be47769adae5ea3e1bd938c16f111

View File

@ -1 +1 @@
a9b24df1a9b756a44c00b9ae5110f480b03594c888c3659dbb3f3845f84a2ce4
ae51241ac0f631c070df03008b50d177e40c007642b63ec680be1b4b5ad4221b

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using presence.domain.Models;
using presence.domain.UseCase;
namespace presence_api.Controllers.UserController;
[ApiController]
[Route("api/[admin]")]
public class AdminController: ControllerBase
{
private readonly UserUseCase _userUseCase;
private readonly GroupUseCase _groupUseCase;
private readonly PresenceUseCase _presenceUseCase;
public AdminController(UserUseCase userUseCase, GroupUseCase groupUseCase, PresenceUseCase presenceUseCase)
{
_userUseCase = userUseCase;
_groupUseCase = groupUseCase;
_presenceUseCase = presenceUseCase;
}
}

View File

@ -0,0 +1,93 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using domain.Models.ResponseModels;
using Microsoft.AspNetCore.Mvc;
using presence.domain.Models;
using presence.domain.UseCase;
namespace presence_api.Controllers.PresenceController;
[ApiController]
[Route("api/[controller]")]
public class PresenceController: ControllerBase
{
private readonly PresenceUseCase _presenceUseCase;
public PresenceController(PresenceUseCase presenceUseCase)
{
_presenceUseCase = presenceUseCase;
}
[HttpGet]
public ActionResult<IEnumerable<Presence>> GetPresence([FromQuery] int GroupId,
[FromQuery] string StartData, [FromQuery] string EndData,
[FromQuery] int UserId)
{
DateOnly startData;
DateOnly endData;
bool isParsed = DateOnly.TryParse(StartData, out startData);
bool isParse = DateOnly.TryParse(EndData, out endData);
if (!isParsed)
{
return BadRequest("Неверный формат даты начала.");
}
if (!isParse)
{
return BadRequest("Неверный формат даты конца.");
}
return Ok(_presenceUseCase.GetPresence(GroupId, startData, endData, UserId));
}
[HttpPost]
public ActionResult<IEnumerable<Presence>> PostPresence([FromQuery] int GroupId,
[FromQuery] string StartData, [FromQuery] string EndData, [FromQuery] int UserId)
{
return Ok(_presenceUseCase.AddPresence(StartData, EndData, GroupId));
}
[HttpDelete]
public ActionResult<IEnumerable<PresenceResponse>> DeletePresenceByGroup([FromQuery] int GroupId)
{
return Ok(_presenceUseCase.DeletePresenceByGroup(GroupId));
}
[HttpDelete]
public ActionResult<IEnumerable<PresenceResponse>> DeletePresenceByUser([FromQuery] int UserId)
{
return Ok(_presenceUseCase.DeletePresenceByUser(UserId));
}
[HttpDelete]
public ActionResult<IEnumerable<PresenceResponse>> DeletePresenceByDate([FromQuery] string StartData, [FromQuery] string EndData)
{
DateOnly startData;
DateOnly endData;
bool isParsed = DateOnly.TryParse(StartData, out startData);
bool isParse = DateOnly.TryParse(EndData, out endData);
if (!isParsed)
{
return BadRequest("Неверный формат даты начала.");
}
if (!isParse)
{
return BadRequest("Неверный формат даты конца.");
}
return Ok(_presenceUseCase.DeletePresenceByDate(startData, endData));
}
[HttpPost]
public ActionResult<PresenceResponse> UpdateAttendance([FromQuery] int FirstClass,
[FromQuery] int LastClass, [FromQuery] string Data,
[FromQuery] int UserId)
{
DateOnly data;
bool isParsed = DateOnly.TryParse(Data, out data);
if (!isParsed)
{
return BadRequest("Неверный формат даты.");
}
return Ok(_presenceUseCase.UncheckAttendence(FirstClass, LastClass, data, UserId));
}
}

View File

@ -1,27 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using presence.domain.Models;
using presence.domain.UseCase;
namespace presence_api.Controllers.UserController;
[ApiController]
[Route("api/[controller]")]
public class UserController: ControllerBase
{
private readonly UserUseCase _userUseCase;
public UserController(UserUseCase userUseCase)
{
_userUseCase = userUseCase;
}
[HttpGet]
public ActionResult<IEnumerable<User>> GetAllUser()
{
return Ok(_userUseCase.GetAllUsers());
}
}

View File

@ -9,7 +9,8 @@ builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.ConfigureGroup();
builder.Services.ConfigureUser();
builder.Services.ConfigureAdmin();
builder.Services.ConfigurePresence();
var app = builder.Build();

View File

@ -22,5 +22,23 @@ namespace presence_api.ServiceExtensions
.AddScoped<IUserRepository, SQLUserRepositoryImpl>()
.AddScoped<UserUseCase>();
}
public static void ConfigurePresence(this IServiceCollection services)
{
services
.AddScoped<IPresenceRepository, SQLPresenceRepositoryImpl>()
.AddScoped<PresenceUseCase>();
}
public static void ConfigureAdmin(this IServiceCollection services)
{
services
.AddScoped<IUserRepository, SQLUserRepositoryImpl>()
.AddScoped<IGroupRepository, SQLGroupRepositoryImpl>()
.AddScoped<IPresenceRepository, SQLPresenceRepositoryImpl>()
.AddScoped<UserUseCase>()
.AddScoped<GroupUseCase>()
.AddScoped<PresenceUseCase>();
}
}
}

View File

@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("presence_api")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+4bdc422ce8c208f21ae34f3d9f1cc61a98a48a3c")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+5b98b0036758c0bfbfd050b258dbaf603f2669f2")]
[assembly: System.Reflection.AssemblyProductAttribute("presence_api")]
[assembly: System.Reflection.AssemblyTitleAttribute("presence_api")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View File

@ -1 +1 @@
d346aa73c391eb0ef182af766d6332e886a863e9a1d1a8b4618c0041ef8db1f6
b64a32c4afcdd923cba00e274a519ae6a5efc835494f91bacc7cf08bf4992583

View File

@ -1 +1 @@
6c572403d26fc1fb9b48c0e23b3616f42e8d5076d149940e6b0f5e2bd64da14b
14cfac9208e7f1e1bd6f6867da3895abc7d7237c391a4adda871de15fc41ad95

View File

@ -26,7 +26,7 @@ namespace presence.ui
StringBuilder userOutput = new StringBuilder();
foreach (var user in _userUseCase.GetAllUsers())
{
userOutput.AppendLine($"{user.Id}\t{user.FIO}\t{user.Group}");
userOutput.AppendLine($"{user.Id}\t{user.FIO}\t{user.GroupId}");
}
Console.WriteLine(userOutput);
}

View File

@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("ui")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+5b98b0036758c0bfbfd050b258dbaf603f2669f2")]
[assembly: System.Reflection.AssemblyProductAttribute("ui")]
[assembly: System.Reflection.AssemblyTitleAttribute("ui")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View File

@ -1 +1 @@
20ed1ba88db46bd37bf95cc037aa54d55b6a60c0405520a36b7f26aec1c3e1fe
aae41bb0bff218b6d819a8588d8a08693e0dd4ef657786bcb45f29aa7e6facd9