Added post endpoints and dto

fix error in database
This commit is contained in:
2026-04-21 22:03:24 +01:00
parent 8f6cf126be
commit 881f8559c1
17 changed files with 728 additions and 14 deletions
+1
View File
@@ -1,4 +1,5 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AEndpoint_002ESend_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fc2fa49f697e4d07c58aa3f35484a5103de685287155db8b628f2cdce16b69_003FEndpoint_002ESend_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AEndpoint_002ESetup_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E3_003Fresharper_002Dhost_003FSourcesCache_003F656240c46e44c14018dc6190e77764a3404d76b559ed75474669c553eb6227dd_003FEndpoint_002ESetup_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AIHubContext_00601_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F731dbe51d65849048244493644b992cd78910_003Fda_003Fd14ae6ee_003FIHubContext_00601_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ARepositoryBaseOfT_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E3_003Fresharper_002Dhost_003FSourcesCache_003F5023b5be698d783ffe9f42b2e944a85a7a66b61bc5e19c76c591036343fd16_003FRepositoryBaseOfT_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
+20
View File
@@ -16,6 +16,7 @@ public class BeReadyDbContext : DbContext
public DbSet<UserGroup> UserGroups { get; set; }
public DbSet<UserRandomChallenge> UserRandomChallenges { get; set; }
public DbSet<Post> Posts { get; set; }
public DbSet<UserPost> UserPosts { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
@@ -42,5 +43,24 @@ public class BeReadyDbContext : DbContext
.WithMany()
.HasForeignKey(x => x.FriendId)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Post>()
.HasOne(x => x.User)
.WithMany(x => x.Posts)
.HasForeignKey(x => x.UserId)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<UserPost>()
.HasOne(x => x.User)
.WithMany(x => x.UserPosts)
.HasForeignKey(x => x.UserId)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<UserPost>()
.HasOne(x => x.Post)
.WithMany(x => x.UserPosts)
.HasForeignKey(x => x.PostId)
.OnDelete(DeleteBehavior.Cascade);
}
}
+2 -1
View File
@@ -6,7 +6,8 @@ public class GetPostDto
public string? Libelle { get; set; }
public DateTime CreationDate { get; set; }
public int Likes { get; set; }
public bool IsLiked { get; set; }
public int UserId { get; set; }
public string? UserUsername { get; set; }
public string? Username { get; set; }
}
@@ -1,4 +1,5 @@
using BeReadyBackend.DTO.Posts;
using BeReadyBackend.Models;
using BeReadyBackend.Repositories;
using BeReadyBackend.Services;
using BeReadyBackend.Specifications.Posts;
@@ -12,10 +13,24 @@ public class GetAllPostsEndpoint(PostsRepository postsRepository, UserService us
{
Get("/Posts/");
}
public override async Task HandleAsync(CancellationToken ct)
{
int userId = userService.GetUserIdFromToken();
await Send.OkAsync(await postsRepository.ProjectToListAsync<GetPostDto>(new GetPostNotMeSpec(userId), ct), ct);
List<Post> posts = await postsRepository.ListAsync(new GetPostNotMeSpec(userId), ct);
List<GetPostDto> result = posts.Select(x => new GetPostDto
{
Id = x.Id,
Libelle = x.Libelle,
CreationDate = x.CreationDate,
Likes = x.Likes,
UserId = x.UserId,
Username = x.User?.Username,
IsLiked = x.UserPosts?.Count(y => y.UserId == userId) > 0
}).ToList();
await Send.OkAsync(result, ct);
}
}
@@ -1,6 +1,64 @@
using BeReadyBackend.Models;
using BeReadyBackend.Repositories;
using BeReadyBackend.Services;
using BeReadyBackend.Specifications.Posts;
using BeReadyBackend.Specifications.Users;
using FastEndpoints;
namespace BeReadyBackend.Endpoints.Posts;
public class PatchLikeEndpoint
public class PatchLikeRequest
{
public int PostId { get; set; }
}
public class PatchLikeEndpoint(UserPostsRepository userPostsRepository, UsersRepository usersRepository, UserService userService, PostsRepository postsRepository)
: Endpoint<PatchLikeRequest>
{
public override void Configure()
{
Patch("/Posts/{@PostId}/Like", x => new { x.PostId });
}
public override async Task HandleAsync(PatchLikeRequest req, CancellationToken ct)
{
int userId = userService.GetUserIdFromToken();
Post? post = await postsRepository.SingleOrDefaultAsync(new GetPostByIdSpec(req.PostId), ct);
if (post is null)
{
await Send.NotFoundAsync(ct);
return;
}
User? user = await usersRepository.SingleOrDefaultAsync(new GetUserByIdSpec(userId), ct);
if (user is null)
{
await Send.NotFoundAsync(ct);
return;
}
UserPost? userPost = await userPostsRepository.SingleOrDefaultAsync(new GetUserPostByIdsSpec(userId, req.PostId), ct);
if (userPost is not null)
{
post.Likes = Math.Max(0, post.Likes - 1);
post.User!.TotalLikes = Math.Max(0, post.User.TotalLikes - 1);
await userPostsRepository.DeleteAsync(userPost, ct);
await Send.NoContentAsync(ct);
return;
}
post.Likes++;
post.User!.TotalLikes++;
UserPost liked = new()
{
UserId = userId,
PostId = req.PostId
};
await userPostsRepository.AddAsync(liked, ct);
await Send.NoContentAsync(ct);
}
}
@@ -39,7 +39,7 @@ public class EntityToDtoMappings : Profile
.ForMember(dest => dest.ChallengeTitle, opt => opt.MapFrom(src => src.Label))
.ForMember(dest => dest.ChallengeDescription, opt => opt.MapFrom(src => src.Libelle))
.ForMember(dest => dest.ChallengeStartDate, opt => opt.MapFrom(src => DateOnly.FromDateTime(src.GeneratedAt!.Value)));
CreateMap<UserFriend, GetFriendDto>()
.ForMember(dest => dest.Username, opt => opt.MapFrom(src => src.Friend!.Username))
.ForMember(dest => dest.Score, opt => opt.MapFrom(src => src.Friend!.TotalLikes));
@@ -66,7 +66,5 @@ public class EntityToDtoMappings : Profile
CreateMap<RandomChallenge, GetRandomChallengeDto>();
CreateMap<Designation, GetDesignationDto>();
CreateMap<Post, GetPostDto>();
}
}
@@ -0,0 +1,491 @@
// <auto-generated />
using System;
using BeReadyBackend;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace BeReadyBackend.Migrations
{
[DbContext(typeof(BeReadyDbContext))]
[Migration("20260421205921_FixedErrorWithAuthorOfPost")]
partial class FixedErrorWithAuthorOfPost
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "8.0.20")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("BeReadyBackend.Models.Achievement", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Description")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Label")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Achievements");
});
modelBuilder.Entity("BeReadyBackend.Models.Designation", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Label")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Designations");
});
modelBuilder.Entity("BeReadyBackend.Models.Group", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<DateTime>("CreationDate")
.HasColumnType("datetime2");
b.Property<string>("Label")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Groups");
});
modelBuilder.Entity("BeReadyBackend.Models.Message", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("GroupId")
.HasColumnType("int");
b.Property<string>("Libelle")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("SendDate")
.HasColumnType("datetime2");
b.Property<int>("UserId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("GroupId");
b.HasIndex("UserId");
b.ToTable("Messages");
});
modelBuilder.Entity("BeReadyBackend.Models.Post", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<DateTime>("CreationDate")
.HasColumnType("datetime2");
b.Property<string>("Libelle")
.HasColumnType("nvarchar(max)");
b.Property<int>("Likes")
.HasColumnType("int");
b.Property<int>("UserId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("UserId");
b.ToTable("Posts");
});
modelBuilder.Entity("BeReadyBackend.Models.RandomChallenge", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<DateTime?>("GeneratedAt")
.HasColumnType("datetime2");
b.Property<bool>("IsAlreadyPast")
.HasColumnType("bit");
b.Property<string>("Label")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Libelle")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("RandomChallenges");
});
modelBuilder.Entity("BeReadyBackend.Models.User", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<DateTime>("CreationDate")
.HasColumnType("datetime2");
b.Property<int?>("DesignationId")
.HasColumnType("int");
b.Property<string>("Email")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("nvarchar(100)");
b.Property<string>("FirstName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Password")
.IsRequired()
.HasMaxLength(60)
.HasColumnType("nvarchar(60)");
b.Property<string>("Salt")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("Series")
.HasColumnType("int");
b.Property<int>("TotalChallenge")
.HasColumnType("int");
b.Property<int>("TotalLikes")
.HasColumnType("int");
b.Property<string>("Username")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.HasIndex("DesignationId");
b.ToTable("Users");
});
modelBuilder.Entity("BeReadyBackend.Models.UserAchievement", b =>
{
b.Property<int>("UserId")
.HasColumnType("int");
b.Property<int>("AchievementId")
.HasColumnType("int");
b.HasKey("UserId", "AchievementId");
b.HasIndex("AchievementId");
b.ToTable("UserAchievements");
});
modelBuilder.Entity("BeReadyBackend.Models.UserFriend", b =>
{
b.Property<int>("UserId")
.HasColumnType("int");
b.Property<int>("FriendId")
.HasColumnType("int");
b.Property<bool>("IsAccepted")
.HasColumnType("bit");
b.HasKey("UserId", "FriendId");
b.HasIndex("FriendId");
b.ToTable("UserFriends");
});
modelBuilder.Entity("BeReadyBackend.Models.UserGroup", b =>
{
b.Property<int>("UserId")
.HasColumnType("int");
b.Property<int>("GroupId")
.HasColumnType("int");
b.Property<string>("Grade")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("UserId", "GroupId");
b.HasIndex("GroupId");
b.ToTable("UserGroups");
});
modelBuilder.Entity("BeReadyBackend.Models.UserPost", b =>
{
b.Property<int>("UserId")
.HasColumnType("int");
b.Property<int>("PostId")
.HasColumnType("int");
b.HasKey("UserId", "PostId");
b.HasIndex("PostId");
b.ToTable("UserPosts");
});
modelBuilder.Entity("BeReadyBackend.Models.UserRandomChallenge", b =>
{
b.Property<int>("UserId")
.HasColumnType("int");
b.Property<int>("RandomChallengeId")
.HasColumnType("int");
b.Property<string>("Proof")
.HasColumnType("nvarchar(max)");
b.HasKey("UserId", "RandomChallengeId");
b.HasIndex("RandomChallengeId");
b.ToTable("UserRandomChallenges");
});
modelBuilder.Entity("BeReadyBackend.Models.Message", b =>
{
b.HasOne("BeReadyBackend.Models.Group", "Group")
.WithMany("Messages")
.HasForeignKey("GroupId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("BeReadyBackend.Models.User", "User")
.WithMany("Messages")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Group");
b.Navigation("User");
});
modelBuilder.Entity("BeReadyBackend.Models.Post", b =>
{
b.HasOne("BeReadyBackend.Models.User", "User")
.WithMany("Posts")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("User");
});
modelBuilder.Entity("BeReadyBackend.Models.User", b =>
{
b.HasOne("BeReadyBackend.Models.Designation", "Designation")
.WithMany("Users")
.HasForeignKey("DesignationId");
b.Navigation("Designation");
});
modelBuilder.Entity("BeReadyBackend.Models.UserAchievement", b =>
{
b.HasOne("BeReadyBackend.Models.Achievement", "Achievement")
.WithMany("UserAchievements")
.HasForeignKey("AchievementId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("BeReadyBackend.Models.User", "User")
.WithMany("UserAchievements")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Achievement");
b.Navigation("User");
});
modelBuilder.Entity("BeReadyBackend.Models.UserFriend", b =>
{
b.HasOne("BeReadyBackend.Models.User", "Friend")
.WithMany()
.HasForeignKey("FriendId")
.OnDelete(DeleteBehavior.Restrict)
.IsRequired();
b.HasOne("BeReadyBackend.Models.User", "User")
.WithMany("UserFriends")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Restrict)
.IsRequired();
b.Navigation("Friend");
b.Navigation("User");
});
modelBuilder.Entity("BeReadyBackend.Models.UserGroup", b =>
{
b.HasOne("BeReadyBackend.Models.Group", "Group")
.WithMany("UserGroups")
.HasForeignKey("GroupId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("BeReadyBackend.Models.User", "User")
.WithMany("UserGroups")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Group");
b.Navigation("User");
});
modelBuilder.Entity("BeReadyBackend.Models.UserPost", b =>
{
b.HasOne("BeReadyBackend.Models.Post", "Post")
.WithMany("UserPosts")
.HasForeignKey("PostId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("BeReadyBackend.Models.User", "User")
.WithMany("UserPosts")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.NoAction)
.IsRequired();
b.Navigation("Post");
b.Navigation("User");
});
modelBuilder.Entity("BeReadyBackend.Models.UserRandomChallenge", b =>
{
b.HasOne("BeReadyBackend.Models.RandomChallenge", "RandomChallenge")
.WithMany("UserRandomChallenges")
.HasForeignKey("RandomChallengeId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("BeReadyBackend.Models.User", "User")
.WithMany("UserRandomChallenges")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("RandomChallenge");
b.Navigation("User");
});
modelBuilder.Entity("BeReadyBackend.Models.Achievement", b =>
{
b.Navigation("UserAchievements");
});
modelBuilder.Entity("BeReadyBackend.Models.Designation", b =>
{
b.Navigation("Users");
});
modelBuilder.Entity("BeReadyBackend.Models.Group", b =>
{
b.Navigation("Messages");
b.Navigation("UserGroups");
});
modelBuilder.Entity("BeReadyBackend.Models.Post", b =>
{
b.Navigation("UserPosts");
});
modelBuilder.Entity("BeReadyBackend.Models.RandomChallenge", b =>
{
b.Navigation("UserRandomChallenges");
});
modelBuilder.Entity("BeReadyBackend.Models.User", b =>
{
b.Navigation("Messages");
b.Navigation("Posts");
b.Navigation("UserAchievements");
b.Navigation("UserFriends");
b.Navigation("UserGroups");
b.Navigation("UserPosts");
b.Navigation("UserRandomChallenges");
});
#pragma warning restore 612, 618
}
}
}
@@ -0,0 +1,49 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace BeReadyBackend.Migrations
{
/// <inheritdoc />
public partial class FixedErrorWithAuthorOfPost : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "UserPosts",
columns: table => new
{
UserId = table.Column<int>(type: "int", nullable: false),
PostId = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_UserPosts", x => new { x.UserId, x.PostId });
table.ForeignKey(
name: "FK_UserPosts_Posts_PostId",
column: x => x.PostId,
principalTable: "Posts",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_UserPosts_Users_UserId",
column: x => x.UserId,
principalTable: "Users",
principalColumn: "Id");
});
migrationBuilder.CreateIndex(
name: "IX_UserPosts_PostId",
table: "UserPosts",
column: "PostId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "UserPosts");
}
}
}
@@ -272,6 +272,21 @@ namespace BeReadyBackend.Migrations
b.ToTable("UserGroups");
});
modelBuilder.Entity("BeReadyBackend.Models.UserPost", b =>
{
b.Property<int>("UserId")
.HasColumnType("int");
b.Property<int>("PostId")
.HasColumnType("int");
b.HasKey("UserId", "PostId");
b.HasIndex("PostId");
b.ToTable("UserPosts");
});
modelBuilder.Entity("BeReadyBackend.Models.UserRandomChallenge", b =>
{
b.Property<int>("UserId")
@@ -386,6 +401,25 @@ namespace BeReadyBackend.Migrations
b.Navigation("User");
});
modelBuilder.Entity("BeReadyBackend.Models.UserPost", b =>
{
b.HasOne("BeReadyBackend.Models.Post", "Post")
.WithMany("UserPosts")
.HasForeignKey("PostId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("BeReadyBackend.Models.User", "User")
.WithMany("UserPosts")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.NoAction)
.IsRequired();
b.Navigation("Post");
b.Navigation("User");
});
modelBuilder.Entity("BeReadyBackend.Models.UserRandomChallenge", b =>
{
b.HasOne("BeReadyBackend.Models.RandomChallenge", "RandomChallenge")
@@ -422,6 +456,11 @@ namespace BeReadyBackend.Migrations
b.Navigation("UserGroups");
});
modelBuilder.Entity("BeReadyBackend.Models.Post", b =>
{
b.Navigation("UserPosts");
});
modelBuilder.Entity("BeReadyBackend.Models.RandomChallenge", b =>
{
b.Navigation("UserRandomChallenges");
@@ -439,6 +478,8 @@ namespace BeReadyBackend.Migrations
b.Navigation("UserGroups");
b.Navigation("UserPosts");
b.Navigation("UserRandomChallenges");
});
#pragma warning restore 612, 618
+3
View File
@@ -9,5 +9,8 @@ public class Post
[Required] public DateTime CreationDate { get; set; }
[Required] public int Likes { get; set; } = 0;
public User? User { get; set; }
[Required] public int UserId { get; set; }
public List<UserPost>? UserPosts { get; set; }
}
+6 -4
View File
@@ -1,12 +1,14 @@
using System.ComponentModel.DataAnnotations;
using Microsoft.EntityFrameworkCore;
namespace BeReadyBackend.Models;
[PrimaryKey(nameof(UserId), nameof(PostId))]
public class UserPost
{
public User? User { get; set; }
public int UserId { get; set; }
[Required] public int UserId { get; set; }
public Post? Post { get; set; }
public int PostId { get; set; }
public bool IsLiked { get; set; }
[Required] public int PostId { get; set; }
}
+1
View File
@@ -45,6 +45,7 @@ builder.Services.AddScoped<UserGroupsRepository>();
builder.Services.AddScoped<UserRandomChallengesRepository>();
builder.Services.AddScoped<UsersRepository>();
builder.Services.AddScoped<PostsRepository>();
builder.Services.AddScoped<UserPostsRepository>();
builder.Services.AddSignalR();
@@ -0,0 +1,5 @@
using BeReadyBackend.Models;
namespace BeReadyBackend.Repositories;
public class UserPostsRepository(BeReadyDbContext beReadyDbContext, AutoMapper.IMapper mapper) : BeReadyRepository<UserPost>(beReadyDbContext, mapper);
@@ -0,0 +1,13 @@
using Ardalis.Specification;
using BeReadyBackend.Models;
namespace BeReadyBackend.Specifications.Posts;
public class GetPostByIdSpec : SingleResultSpecification<Post>
{
public GetPostByIdSpec(int postId)
{
Query
.Where(x => x.Id == postId);
}
}
@@ -9,6 +9,9 @@ public class GetPostNotMeSpec : Specification<Post>
{
Query
.Include(x => x.User)
.Where(x => x.UserId != userId);
.Include(x => x.UserPosts!)
.ThenInclude(up => up.User)
.Where(x => x.UserId != userId &&
x.CreationDate >= DateTime.Today && x.CreationDate < DateTime.Today.AddDays(1));
}
}
@@ -0,0 +1,13 @@
using Ardalis.Specification;
using BeReadyBackend.Models;
namespace BeReadyBackend.Specifications.Posts;
public class GetUserPostByIdsSpec : SingleResultSpecification<UserPost>
{
public GetUserPostByIdsSpec(int userId, int postId)
{
Query
.Where(x => x.UserId == userId && x.PostId == postId);
}
}
@@ -36,7 +36,7 @@ public class GetPostDtoValidator : Validator<GetPostDto>
.GreaterThan(0)
.WithMessage("UserId must be greater than zero");
RuleFor(x => x.UserUsername)
RuleFor(x => x.Username)
.NotEmpty()
.WithMessage("UserUsername cannot be empty");
}