From 8f6cf126be20db2c408de4234981fd209f6aaa47 Mon Sep 17 00:00:00 2001 From: sanchezvem Date: Tue, 21 Apr 2026 11:04:25 +0200 Subject: [PATCH] Added new entity --- BeReadyBackend/DTO/Posts/GetPostDto.cs | 12 ++++++ .../Endpoints/Posts/GetAllPostsEndpoint.cs | 21 +++++++++ .../Endpoints/Posts/PatchLikeEndpoint.cs | 6 +++ .../Users/GetAllUserChallengesEndpoint.cs | 5 --- .../MappingProfiles/EntityToDtoMappings.cs | 3 ++ BeReadyBackend/Models/Post.cs | 3 +- BeReadyBackend/Models/User.cs | 1 + BeReadyBackend/Models/UserPost.cs | 12 ++++++ .../Specifications/Posts/GetPostNotMeSpec.cs | 14 ++++++ .../Validators/Posts/GetPostDtoValidator.cs | 43 +++++++++++++++++++ 10 files changed, 113 insertions(+), 7 deletions(-) create mode 100644 BeReadyBackend/DTO/Posts/GetPostDto.cs create mode 100644 BeReadyBackend/Endpoints/Posts/GetAllPostsEndpoint.cs create mode 100644 BeReadyBackend/Endpoints/Posts/PatchLikeEndpoint.cs create mode 100644 BeReadyBackend/Models/UserPost.cs create mode 100644 BeReadyBackend/Specifications/Posts/GetPostNotMeSpec.cs create mode 100644 BeReadyBackend/Validators/Posts/GetPostDtoValidator.cs diff --git a/BeReadyBackend/DTO/Posts/GetPostDto.cs b/BeReadyBackend/DTO/Posts/GetPostDto.cs new file mode 100644 index 0000000..1cdc150 --- /dev/null +++ b/BeReadyBackend/DTO/Posts/GetPostDto.cs @@ -0,0 +1,12 @@ +namespace BeReadyBackend.DTO.Posts; + +public class GetPostDto +{ + public int Id { get; set; } + public string? Libelle { get; set; } + public DateTime CreationDate { get; set; } + public int Likes { get; set; } + + public int UserId { get; set; } + public string? UserUsername { get; set; } +} \ No newline at end of file diff --git a/BeReadyBackend/Endpoints/Posts/GetAllPostsEndpoint.cs b/BeReadyBackend/Endpoints/Posts/GetAllPostsEndpoint.cs new file mode 100644 index 0000000..e228b24 --- /dev/null +++ b/BeReadyBackend/Endpoints/Posts/GetAllPostsEndpoint.cs @@ -0,0 +1,21 @@ +using BeReadyBackend.DTO.Posts; +using BeReadyBackend.Repositories; +using BeReadyBackend.Services; +using BeReadyBackend.Specifications.Posts; +using FastEndpoints; + +namespace BeReadyBackend.Endpoints.Posts; + +public class GetAllPostsEndpoint(PostsRepository postsRepository, UserService userService) : EndpointWithoutRequest> +{ + public override void Configure() + { + Get("/Posts/"); + } + + public override async Task HandleAsync(CancellationToken ct) + { + int userId = userService.GetUserIdFromToken(); + await Send.OkAsync(await postsRepository.ProjectToListAsync(new GetPostNotMeSpec(userId), ct), ct); + } +} \ No newline at end of file diff --git a/BeReadyBackend/Endpoints/Posts/PatchLikeEndpoint.cs b/BeReadyBackend/Endpoints/Posts/PatchLikeEndpoint.cs new file mode 100644 index 0000000..8e7255b --- /dev/null +++ b/BeReadyBackend/Endpoints/Posts/PatchLikeEndpoint.cs @@ -0,0 +1,6 @@ +namespace BeReadyBackend.Endpoints.Posts; + +public class PatchLikeEndpoint +{ + +} \ No newline at end of file diff --git a/BeReadyBackend/Endpoints/Users/GetAllUserChallengesEndpoint.cs b/BeReadyBackend/Endpoints/Users/GetAllUserChallengesEndpoint.cs index 3825148..fc39515 100644 --- a/BeReadyBackend/Endpoints/Users/GetAllUserChallengesEndpoint.cs +++ b/BeReadyBackend/Endpoints/Users/GetAllUserChallengesEndpoint.cs @@ -34,11 +34,6 @@ public class GetAllUserChallengesEndpoint(UsersRepository usersRepository, UserS .Select(x => mapper.Map(x.RandomChallenge)) ); - if (user.UserGroups is not null) - challenges.AddRange( - user.UserGroups.Select(x => mapper.Map(x.Group)) - ); - await Send.OkAsync(challenges.OrderByDescending(x => x.ChallengeStartDate).ToList(), ct); } } \ No newline at end of file diff --git a/BeReadyBackend/MappingProfiles/EntityToDtoMappings.cs b/BeReadyBackend/MappingProfiles/EntityToDtoMappings.cs index 61eefbc..edbd50d 100644 --- a/BeReadyBackend/MappingProfiles/EntityToDtoMappings.cs +++ b/BeReadyBackend/MappingProfiles/EntityToDtoMappings.cs @@ -4,6 +4,7 @@ using BeReadyBackend.DTO.Designations; using BeReadyBackend.DTO.Friends; using BeReadyBackend.DTO.Groups; using BeReadyBackend.DTO.Messages; +using BeReadyBackend.DTO.Posts; using BeReadyBackend.DTO.RandomChallenges; using BeReadyBackend.DTO.Users; using BeReadyBackend.Models; @@ -65,5 +66,7 @@ public class EntityToDtoMappings : Profile CreateMap(); CreateMap(); + + CreateMap(); } } \ No newline at end of file diff --git a/BeReadyBackend/Models/Post.cs b/BeReadyBackend/Models/Post.cs index 5fcccc3..1e82614 100644 --- a/BeReadyBackend/Models/Post.cs +++ b/BeReadyBackend/Models/Post.cs @@ -9,6 +9,5 @@ public class Post [Required] public DateTime CreationDate { get; set; } [Required] public int Likes { get; set; } = 0; - public User? User { get; set; } - public int UserId { get; set; } + public List? UserPosts { get; set; } } \ No newline at end of file diff --git a/BeReadyBackend/Models/User.cs b/BeReadyBackend/Models/User.cs index 56c33ab..7f24279 100644 --- a/BeReadyBackend/Models/User.cs +++ b/BeReadyBackend/Models/User.cs @@ -25,4 +25,5 @@ public class User public List? UserRandomChallenges { get; set; } public List? UserAchievements { get; set; } public List? UserGroups { get; set; } + public List? UserPosts { get; set; } } \ No newline at end of file diff --git a/BeReadyBackend/Models/UserPost.cs b/BeReadyBackend/Models/UserPost.cs new file mode 100644 index 0000000..4c4e83d --- /dev/null +++ b/BeReadyBackend/Models/UserPost.cs @@ -0,0 +1,12 @@ +namespace BeReadyBackend.Models; + +public class UserPost +{ + public User? User { get; set; } + public int UserId { get; set; } + + public Post? Post { get; set; } + public int PostId { get; set; } + + public bool IsLiked { get; set; } +} \ No newline at end of file diff --git a/BeReadyBackend/Specifications/Posts/GetPostNotMeSpec.cs b/BeReadyBackend/Specifications/Posts/GetPostNotMeSpec.cs new file mode 100644 index 0000000..35aff4a --- /dev/null +++ b/BeReadyBackend/Specifications/Posts/GetPostNotMeSpec.cs @@ -0,0 +1,14 @@ +using Ardalis.Specification; +using BeReadyBackend.Models; + +namespace BeReadyBackend.Specifications.Posts; + +public class GetPostNotMeSpec : Specification +{ + public GetPostNotMeSpec(int userId) + { + Query + .Include(x => x.User) + .Where(x => x.UserId != userId); + } +} \ No newline at end of file diff --git a/BeReadyBackend/Validators/Posts/GetPostDtoValidator.cs b/BeReadyBackend/Validators/Posts/GetPostDtoValidator.cs new file mode 100644 index 0000000..2139fdc --- /dev/null +++ b/BeReadyBackend/Validators/Posts/GetPostDtoValidator.cs @@ -0,0 +1,43 @@ +using BeReadyBackend.DTO.Posts; +using FastEndpoints; +using FluentValidation; + +namespace BeReadyBackend.Validators.Posts; + +public class GetPostDtoValidator : Validator +{ + public GetPostDtoValidator() + { + RuleFor(x => x.Id) + .NotEmpty() + .WithMessage("Id is required") + .GreaterThan(0) + .WithMessage("Id must be greater than zero"); + + RuleFor(x => x.Libelle) + .NotEmpty() + .WithMessage("Libelle cannot be empty") + .MaximumLength(200) + .WithMessage("Libelle cannot exceed 100 characters") + .MinimumLength(2) + .WithMessage("Libelle must exceed 2 characters"); + + RuleFor(x => x.CreationDate) + .NotEmpty() + .WithMessage("CreationDate cannot be empty"); + + RuleFor(x => x.Likes) + .NotEmpty() + .WithMessage("Likes cannot be empty"); + + RuleFor(x => x.UserId) + .NotEmpty() + .WithMessage("UserId cannot be empty") + .GreaterThan(0) + .WithMessage("UserId must be greater than zero"); + + RuleFor(x => x.UserUsername) + .NotEmpty() + .WithMessage("UserUsername cannot be empty"); + } +} \ No newline at end of file