api / admincontroller

This commit is contained in:
1billy17 2024-11-08 20:42:34 +03:00
parent 28d492421b
commit 79faa76278
24 changed files with 343 additions and 1 deletions

View File

@ -7,5 +7,6 @@ namespace Demo.Data.Repository
List<PresenceLocalEntity> GetAllPresences(); List<PresenceLocalEntity> GetAllPresences();
void IsAttedance(int firstLesson, int lastLesson, DateOnly date, Guid UserGuid); void IsAttedance(int firstLesson, int lastLesson, DateOnly date, Guid UserGuid);
List<PresenceLocalEntity> GeneratePresence(List<PresenceLocalEntity> presenceLocalEntities); List<PresenceLocalEntity> GeneratePresence(List<PresenceLocalEntity> presenceLocalEntities);
bool DeletePresence();
} }
} }

View File

@ -8,6 +8,7 @@ namespace Demo.Data.Repository
UserLocalEntity? GetUserByGuid(Guid guid); UserLocalEntity? GetUserByGuid(Guid guid);
List<UserLocalEntity> GetUsersByGroupID(int groupID); List<UserLocalEntity> GetUsersByGroupID(int groupID);
bool RemoveUserByGuid(Guid guid); bool RemoveUserByGuid(Guid guid);
public UserLocalEntity? CreateUser(string FIO, string GroupName);
UserLocalEntity? UpdateUser(UserLocalEntity updatedUser); UserLocalEntity? UpdateUser(UserLocalEntity updatedUser);
} }
} }

View File

@ -36,6 +36,13 @@ namespace Demo.Data.Repository
return presenceLocalEntities; return presenceLocalEntities;
} }
public bool DeletePresence(){
var allRecords = _remoteDatabaseContext.PresenceDaos.ToList();
_remoteDatabaseContext.PresenceDaos.RemoveRange(allRecords);
_remoteDatabaseContext.SaveChanges();
return true;
}
public void IsAttedance(int firstLesson, int lastLesson, DateOnly date, Guid UserGuid){ public void IsAttedance(int firstLesson, int lastLesson, DateOnly date, Guid UserGuid){
var presencesToUpdate = _remoteDatabaseContext.PresenceDaos var presencesToUpdate = _remoteDatabaseContext.PresenceDaos
.Where(x => x.LessonNumber >= firstLesson .Where(x => x.LessonNumber >= firstLesson

View File

@ -1,6 +1,8 @@
using Demo.Domain.Models; using Demo.Domain.Models;
using Demo.Data.LocalData; using Demo.Data.LocalData;
using Demo.Data.RemoteData.RemoteDataBase; using Demo.Data.RemoteData.RemoteDataBase;
using Demo.Data.RemoteData.RemoteDataBase.DAO;
namespace Demo.Data.Repository namespace Demo.Data.Repository
{ {
@ -45,6 +47,14 @@ namespace Demo.Data.Repository
return true; return true;
} }
public UserLocalEntity? CreateUser(string FIO, string GroupName){
var groupDAO = _remoteDatabaseContext.Groups.FirstOrDefault(x => x.Name == GroupName);
UserDAO userDAO = new UserDAO{FIO = FIO, Guid = Guid.NewGuid(), GroupID = groupDAO.ID};
var result = _remoteDatabaseContext.Users.Add(userDAO);
_remoteDatabaseContext.SaveChanges();
return new UserLocalEntity{FIO = FIO, Guid = Guid.NewGuid(), GroupID = groupDAO.ID};
}
public UserLocalEntity? UpdateUser(UserLocalEntity updatedUser){ public UserLocalEntity? UpdateUser(UserLocalEntity updatedUser){
var user = _remoteDatabaseContext.Users.FirstOrDefault(x => x.Guid == updatedUser.Guid); var user = _remoteDatabaseContext.Users.FirstOrDefault(x => x.Guid == updatedUser.Guid);
if (user == null){ if (user == null){

View File

@ -10,4 +10,21 @@ namespace Demo.Domain.Models
return new Group{ID = Convert.ToInt32(words[0]), Name = words[1]}; return new Group{ID = Convert.ToInt32(words[0]), Name = words[1]};
} }
} }
public class GroupU{
public required int ID{get; set; }
public required string Name{get; set; }
public List<User> Users { get; set; } = new List<User>();
}
public class GroupWithUsers
{
public required string GroupName { get; set; }
public List<UserRequest> Users { get; set; }
}
public class DeleteGroupsRequest
{
public List<int> GroupIDs { get; set; }
}
} }

View File

@ -8,4 +8,12 @@ namespace Demo.Domain.Models
public required int LessonNumber { get; set; } public required int LessonNumber { get; set; }
} }
public class PresencePost
{
public required int firstLesson {get; set; }
public required int lastLesson {get; set; }
public required int groupID {get; set; }
public required DateOnly date {get; set; }
}
} }

View File

@ -35,4 +35,15 @@ namespace Demo.Domain.Models
} }
} }
} }
public class UserRequest
{
public required string FIO{get; set; }
public required Group Group{get; set; }
}
public class DeleteUsersRequest
{
public List<Guid> UsersGuid { get; set; }
}
} }

View File

@ -23,6 +23,21 @@ namespace Demo.Domain.UseCase
return new Group{ID = groupID, Name = groupLocalEntity.Name}; return new Group{ID = groupID, Name = groupLocalEntity.Name};
} }
public List<GroupU> GetAllGroupsWithUsers(){
var groups = _repositoryGroupImpl.GetAllGroup().Select(it => new GroupU { ID = it.ID, Name = it.Name }).ToList();
var users = _repositoryUserImpl.GetAllUser().Select(it => new User{FIO = it.FIO, Guid = it.Guid, Group = new Group{ID = it.GroupID, Name = _repositoryGroupImpl.GetGroupById(it.GroupID).Name}}).ToList();
foreach (var group in groups)
{
foreach (var user in users)
{
if (user.Group.ID == group.ID){
group.Users.Add(user);
}
}
}
return groups;
}
public bool CreateGroup(string Name){ public bool CreateGroup(string Name){
_repositoryGroupImpl.CreateGroup(Name); _repositoryGroupImpl.CreateGroup(Name);
return true; return true;

View File

@ -8,6 +8,7 @@ namespace Demo.Domain.UseCase
bool RemoveGroupByID(int groupID); bool RemoveGroupByID(int groupID);
Group UpdateGroup(Group group); Group UpdateGroup(Group group);
Group GetGroupById(int groupID); Group GetGroupById(int groupID);
List<GroupU> GetAllGroupsWithUsers();
bool CreateGroup(string Name); bool CreateGroup(string Name);
} }
} }

View File

@ -12,5 +12,6 @@ namespace Demo.Domain.UseCase
bool IsAttedance(int firstLesson, int lastLesson, DateOnly date, Guid UserGuid); bool IsAttedance(int firstLesson, int lastLesson, DateOnly date, Guid UserGuid);
bool GeneratePresence(int firstLesson, int lastLesson, int groupID, DateOnly date); bool GeneratePresence(int firstLesson, int lastLesson, int groupID, DateOnly date);
bool GeneratePresenceWeek(int firstLesson, int lastLesson, int groupID, DateOnly date); bool GeneratePresenceWeek(int firstLesson, int lastLesson, int groupID, DateOnly date);
bool DeletePresence();
} }
} }

View File

@ -6,6 +6,7 @@ namespace Demo.Domain.UseCase
{ {
List<User> GetAllUsers(); List<User> GetAllUsers();
bool RemoveUserByGuid(Guid userGuid); bool RemoveUserByGuid(Guid userGuid);
bool CreateUser(User user);
User UpdateUser(User user); User UpdateUser(User user);
User GetUserByGuid(Guid userGuid); User GetUserByGuid(Guid userGuid);
List<User> GetUsersByGroupID(int groupID); List<User> GetUsersByGroupID(int groupID);

View File

@ -178,6 +178,10 @@ namespace Demo.Domain.UseCase
return true; return true;
} }
public bool DeletePresence(){
return _repositoryPresenceImpl.DeletePresence();
}
public bool GeneratePresenceWeek(int firstLesson, int lastLesson, int groupID, DateOnly date){ public bool GeneratePresenceWeek(int firstLesson, int lastLesson, int groupID, DateOnly date){
for (int i = 0; i < 8; i++){ for (int i = 0; i < 8; i++){
GeneratePresence(firstLesson, lastLesson, groupID, date.AddDays(i)); GeneratePresence(firstLesson, lastLesson, groupID, date.AddDays(i));

View File

@ -61,6 +61,11 @@ namespace Demo.Domain.UseCase
return users; return users;
} }
public bool CreateUser(User user){
_repositoryUserImpl.CreateUser(user.FIO, user.Group.Name);
return true;
}
public bool RemoveUserByGuid(Guid userGuid) { public bool RemoveUserByGuid(Guid userGuid) {
return _repositoryUserImpl.RemoveUserByGuid(userGuid); return _repositoryUserImpl.RemoveUserByGuid(userGuid);
} }

View File

@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "console_ui", "console_ui\co
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "data", "data\data.csproj", "{7FD5C1C4-6DF8-42BD-AD74-6D33CDE48894}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "data", "data\data.csproj", "{7FD5C1C4-6DF8-42BD-AD74-6D33CDE48894}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "presence_api", "presence_api\presence_api.csproj", "{F7B9C650-020B-45AE-AB31-4C6DC1C1D4CE}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -36,5 +38,9 @@ Global
{7FD5C1C4-6DF8-42BD-AD74-6D33CDE48894}.Debug|Any CPU.Build.0 = Debug|Any CPU {7FD5C1C4-6DF8-42BD-AD74-6D33CDE48894}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7FD5C1C4-6DF8-42BD-AD74-6D33CDE48894}.Release|Any CPU.ActiveCfg = Release|Any CPU {7FD5C1C4-6DF8-42BD-AD74-6D33CDE48894}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7FD5C1C4-6DF8-42BD-AD74-6D33CDE48894}.Release|Any CPU.Build.0 = Release|Any CPU {7FD5C1C4-6DF8-42BD-AD74-6D33CDE48894}.Release|Any CPU.Build.0 = Release|Any CPU
{F7B9C650-020B-45AE-AB31-4C6DC1C1D4CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F7B9C650-020B-45AE-AB31-4C6DC1C1D4CE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F7B9C650-020B-45AE-AB31-4C6DC1C1D4CE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F7B9C650-020B-45AE-AB31-4C6DC1C1D4CE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal

View File

@ -0,0 +1,113 @@
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;
public AdminController(GroupUseCase groupUseCase, UserUseCase userUseCase, PresenceUseCase presenceUseCase){
_groupUseCase = groupUseCase;
_userUseCase = userUseCase;
_presenceUseCase = presenceUseCase;
}
//post
[HttpPost]
public ActionResult<bool> CreateGroup([FromBody] GroupWithUsers request)
{
if (request == null || string.IsNullOrEmpty(request.GroupName))
{
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){
return BadRequest("Invalid request");
}
bool isDeleted = _userUseCase.RemoveUserByGuid(userGuid);
if (isDeleted == false){
return NotFound("User not found");
}
return Ok(true);
}
[HttpDelete("users")]
public ActionResult<bool> DeleteUsers([FromBody] DeleteUsersRequest request){
if (request == null){
return BadRequest("Invalid request");
}
foreach (Guid userGuid in request.UsersGuid)
{
bool isDeleted = _userUseCase.RemoveUserByGuid(userGuid);
if (isDeleted == false){
return NotFound("User not found");
}
}
return Ok(true);
}
[HttpDelete("group")]
public ActionResult<bool> DeleteGroup(int GroupID){
bool isDeleted = _groupUseCase.RemoveGroupByID(GroupID);
if (isDeleted == false){
return NotFound("Group not found");
}
return Ok(true);
}
[HttpDelete("groups")]
public ActionResult<bool> DeleteGroups([FromBody] DeleteGroupsRequest request){
if (request == null){
return BadRequest("Invalid request");
}
foreach (int GroupID in request.GroupIDs)
{
bool isDeleted = _groupUseCase.RemoveGroupByID(GroupID);
if (isDeleted == false){
return NotFound("Group not found");
}
}
return Ok(true);
}
[HttpDelete("presence")]
public ActionResult<bool> DeletePresence(){
return Ok(_presenceUseCase.DeletePresence());
}
//get
[HttpGet]
public ActionResult<IEnumerable<GroupU>> 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{
return NotFound("User not found");
}
}
}

View File

@ -0,0 +1,19 @@
using Microsoft.AspNetCore.Mvc;
using Demo.Domain.UseCase;
using Demo.Domain.Models;
namespace presence_api.Controllers;
[ApiController]
[Route("api/[controller]")]
public class GroupController: ControllerBase{
private readonly GroupUseCase _groupUseCase;
public GroupController(GroupUseCase groupUseCase){
_groupUseCase = groupUseCase;
}
[HttpGet]
public ActionResult<IEnumerable<Group>> getGroups(){
return Ok(_groupUseCase.GetAllGroups());
}
}

25
presence_api/Program.cs Normal file
View File

@ -0,0 +1,25 @@
using Demo.Data.RemoteData.RemoteDataBase;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddDbContext<RemoteDatabaseContext>();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.ConfigurateGroup();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.MapControllers();
app.Run();

View File

@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:46509",
"sslPort": 44380
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5192",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7147;http://localhost:5192",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@ -0,0 +1,15 @@
using Demo.Data.Repository;
using Demo.Domain.UseCase;
public static class ServiceExtencions
{
public static void ConfigurateGroup(this IServiceCollection services)
{
services.AddScoped<IGroupRepository, SQLGroupRepositoryImpl>()
.AddScoped<IUserRepository, SQLUserRepositoryImpl>()
.AddScoped<IPresenceRepository, SQLPresenceRepositoryImpl>()
.AddScoped<GroupUseCase>()
.AddScoped<UserUseCase>()
.AddScoped<PresenceUseCase>();
}
}

View File

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

View File

@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.8" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\domain\domain.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,6 @@
@presence_api_HostAddress = http://localhost:5192
GET {{presence_api_HostAddress}}/weatherforecast/
Accept: application/json
###