Created endpoints to manage groups
This commit is contained in:
@@ -27,7 +27,6 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="DTO\Groups\" />
|
||||
<Folder Include="DTO\Messages\" />
|
||||
<Folder Include="DTO\RandomChallenges\" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace BeReadyBackend.DTO.Groups;
|
||||
|
||||
public class CreateGroupDto
|
||||
{
|
||||
public string? Label { get; set; }
|
||||
public string? Title { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public int Duration { get; set; }
|
||||
public List<CreateUserGroupDto>? UserGroups { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace BeReadyBackend.DTO.Groups;
|
||||
|
||||
public class CreateUserGroupDto
|
||||
{
|
||||
public int UserId { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace BeReadyBackend.DTO.Groups;
|
||||
|
||||
public class GetUserGroupDto
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string? Username { get; set; }
|
||||
public string? Grade { get; set; }
|
||||
public int Score { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
using BeReadyBackend.DTO.Groups;
|
||||
using BeReadyBackend.Models;
|
||||
using BeReadyBackend.Repositories;
|
||||
using BeReadyBackend.Services;
|
||||
using BeReadyBackend.Specifications.Users;
|
||||
using FastEndpoints;
|
||||
using Group = BeReadyBackend.Models.Group;
|
||||
|
||||
namespace BeReadyBackend.Endpoints.Groups;
|
||||
|
||||
public class CreateGroupEndpoint(
|
||||
UserGroupsRepository userGroupsRepository,
|
||||
GroupsRepository groupsRepository,
|
||||
UsersRepository usersRepository,
|
||||
UserService userService,
|
||||
AutoMapper.IMapper mapper) : Endpoint<CreateGroupDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Post("/Groups/");
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CreateGroupDto req, CancellationToken ct)
|
||||
{
|
||||
int userId = userService.GetUserIdFromToken();
|
||||
|
||||
Group? group = new();
|
||||
mapper.Map(req, group);
|
||||
group.IsFinished = false;
|
||||
group.CreationDate = DateTime.Now;
|
||||
group.UserGroups = [];
|
||||
|
||||
await groupsRepository.AddAsync(group, ct);
|
||||
await groupsRepository.SaveChangesAsync(ct);
|
||||
|
||||
UserGroup userGroup;
|
||||
foreach (CreateUserGroupDto user in req.UserGroups!)
|
||||
{
|
||||
User? userChecking = await usersRepository.SingleOrDefaultAsync(new GetUserByIdSpec(user.UserId), ct);
|
||||
if (userChecking is null) continue;
|
||||
userGroup = new UserGroup
|
||||
{
|
||||
UserId = user.UserId,
|
||||
GroupId = group.Id,
|
||||
Grade = "Member",
|
||||
Score = 0
|
||||
};
|
||||
group.UserGroups?.Add(userGroup);
|
||||
}
|
||||
|
||||
userGroup = new UserGroup
|
||||
{
|
||||
UserId = userId,
|
||||
GroupId = group.Id,
|
||||
Grade = "Admin",
|
||||
Score = 0
|
||||
};
|
||||
group.UserGroups?.Add(userGroup);
|
||||
|
||||
await userGroupsRepository.AddRangeAsync(group.UserGroups!, ct);
|
||||
await userGroupsRepository.SaveChangesAsync(ct);
|
||||
|
||||
await Send.OkAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
using BeReadyBackend.Models;
|
||||
using BeReadyBackend.Repositories;
|
||||
using BeReadyBackend.Services;
|
||||
using BeReadyBackend.Specifications.Groups;
|
||||
using BeReadyBackend.Specifications.Users;
|
||||
using FastEndpoints;
|
||||
using Group = BeReadyBackend.Models.Group;
|
||||
|
||||
namespace BeReadyBackend.Endpoints.Groups;
|
||||
|
||||
public class GroupDeletedRequest
|
||||
{
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
public class DeleteGroupEndpoint(GroupsRepository groupsRepository, UserGroupsRepository userGroupsRepository, UserService userService) : Endpoint<GroupDeletedRequest>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Delete("/Groups/{@Id}", x => new { x.Id });
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(GroupDeletedRequest req, CancellationToken ct)
|
||||
{
|
||||
int userId = userService.GetUserIdFromToken();
|
||||
Group? group = await groupsRepository.SingleOrDefaultAsync(new GetGroupByIdSpec(req.Id), ct);
|
||||
|
||||
if (group is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
bool isAdmin = group.UserGroups?.Any(x => x.UserId == userId && x.Grade == "Admin") ?? false;
|
||||
|
||||
if (!isAdmin)
|
||||
{
|
||||
await Send.StringAsync("Vous n'avez pas les droits pour supprimer ce groupe", 400, cancellation: ct);
|
||||
return;
|
||||
}
|
||||
|
||||
if (group.UserGroups?.Count > 0)
|
||||
await userGroupsRepository.DeleteRangeAsync(group.UserGroups, ct);
|
||||
|
||||
await groupsRepository.DeleteAsync(group, ct);
|
||||
await userGroupsRepository.SaveChangesAsync(ct);
|
||||
await Send.OkAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using BeReadyBackend.DTO.Groups;
|
||||
using BeReadyBackend.Repositories;
|
||||
using BeReadyBackend.Services;
|
||||
using BeReadyBackend.Specifications.Groups;
|
||||
using FastEndpoints;
|
||||
|
||||
namespace BeReadyBackend.Endpoints.Groups;
|
||||
|
||||
public class UserGroupRequest
|
||||
{
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
public class GetAllGroupUsersEndpoint(UserGroupsRepository userGroupsRepository, UserService userService) : Endpoint<UserGroupRequest, List<GetUserGroupDto>>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/Groups/{@Id}/Users/", x => new { x.Id });
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(UserGroupRequest req, CancellationToken ct)
|
||||
{
|
||||
int userId = userService.GetUserIdFromToken();
|
||||
|
||||
List<GetUserGroupDto> usersGroup = await userGroupsRepository.ProjectToListAsync<GetUserGroupDto>(new GetUserGroupByIdSpec(req.Id, userId), ct);
|
||||
await Send.OkAsync(usersGroup, ct);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using AutoMapper;
|
||||
using BeReadyBackend.DTO.Achievements;
|
||||
using BeReadyBackend.DTO.Groups;
|
||||
using BeReadyBackend.DTO.Users;
|
||||
using BeReadyBackend.Models;
|
||||
|
||||
@@ -14,5 +15,8 @@ public class DtoToEntityMappings : Profile
|
||||
CreateMap<CreateUserDto, User>();
|
||||
CreateMap<UpdateUserDto, User>();
|
||||
CreateMap<PatchUserDesignationDto, User>();
|
||||
|
||||
CreateMap<CreateGroupDto, Group>()
|
||||
.ForMember(dest => dest.UserGroups, opt => opt.Ignore());;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
using AutoMapper;
|
||||
using BeReadyBackend.DTO.Achievements;
|
||||
using BeReadyBackend.DTO.Friends;
|
||||
using BeReadyBackend.DTO.Groups;
|
||||
using BeReadyBackend.DTO.Users;
|
||||
using BeReadyBackend.Models;
|
||||
using NSwag.Generation.Processors;
|
||||
|
||||
namespace BeReadyBackend.MappingProfiles;
|
||||
|
||||
@@ -44,5 +46,10 @@ public class EntityToDtoMappings : Profile
|
||||
CreateMap<UserFriend, GetFriendRequestDto>()
|
||||
.ForMember(dest => dest.Username, opt => opt.MapFrom(src => src.User!.Username))
|
||||
.ForMember(dest => dest.Score, opt => opt.MapFrom(src => src.User!.Score));
|
||||
|
||||
CreateMap<UserGroup, GetUserGroupDto>()
|
||||
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.UserId))
|
||||
.ForMember(dest => dest.Username, opt => opt.MapFrom(src => src.User!.Username))
|
||||
.ForMember(dest => dest.Score, opt => opt.MapFrom(src => src.User!.Score));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using Ardalis.Specification;
|
||||
using BeReadyBackend.Models;
|
||||
|
||||
namespace BeReadyBackend.Specifications.Groups;
|
||||
|
||||
public class GetGroupByIdSpec : SingleResultSpecification<Group>
|
||||
{
|
||||
public GetGroupByIdSpec(int groupId)
|
||||
{
|
||||
Query
|
||||
.Include(x => x.UserGroups)
|
||||
.Where(x => x.Id == groupId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using Ardalis.Specification;
|
||||
using BeReadyBackend.Models;
|
||||
|
||||
namespace BeReadyBackend.Specifications.Groups;
|
||||
|
||||
public class GetUserGroupByIdSpec : Specification<UserGroup>
|
||||
{
|
||||
public GetUserGroupByIdSpec(int groupId, int userId)
|
||||
{
|
||||
Query
|
||||
.Include(x => x.User!)
|
||||
.Where(x => x.GroupId == groupId && x.Group!.UserGroups!.Any(y => y.UserId == userId));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user