diff --git a/exercise.wwwapi/DTOs/Posts/GetPosts/LikeDTO.cs b/exercise.wwwapi/DTOs/Posts/GetPosts/LikeDTO.cs new file mode 100644 index 0000000..e10f1a2 --- /dev/null +++ b/exercise.wwwapi/DTOs/Posts/GetPosts/LikeDTO.cs @@ -0,0 +1,22 @@ +using exercise.wwwapi.Models; + +namespace exercise.wwwapi.DTOs.Posts.GetPosts +{ + public class LikeDTO + { + public int Id { get; set; } + public int PostId { get; set; } + public int UserId { get; set; } + + public LikeDTO() + { + + } + public LikeDTO(Like model) + { + Id = model.Id; + PostId = model.PostId; + UserId = model.UserId; + } + } +} diff --git a/exercise.wwwapi/DTOs/Posts/GetPosts/PostDTOVol2.cs b/exercise.wwwapi/DTOs/Posts/GetPosts/PostDTOVol2.cs index a528f2d..4fd0b1f 100644 --- a/exercise.wwwapi/DTOs/Posts/GetPosts/PostDTOVol2.cs +++ b/exercise.wwwapi/DTOs/Posts/GetPosts/PostDTOVol2.cs @@ -10,9 +10,9 @@ public class PostDTOVol2 public string Firstname { get; set; } public string Lastname { get; set; } public string Body { get; set; } = string.Empty; - public int Likes { get; set; } public DateTime CreatedAt { get; set; } public List Comments { get; set; } = new List(); + public List Likes { get; set; } = new List(); public PostDTOVol2() { @@ -23,11 +23,11 @@ public PostDTOVol2(Post model) Id = model.Id; AuthorId = model.AuthorId; Body = model.Body; - Likes = model.Likes; CreatedAt = model.CreatedAt; Firstname = model.Author.Profile.FirstName; Lastname = model.Author.Profile.LastName; Comments = model.Comments.Select(c => new CommentDTO(c)).ToList(); + Likes = model.Likes.Select(l => new LikeDTO(l)).ToList(); } } } diff --git a/exercise.wwwapi/DTOs/Posts/PostDTO.cs b/exercise.wwwapi/DTOs/Posts/PostDTO.cs index a7435cf..0b3aaa3 100644 --- a/exercise.wwwapi/DTOs/Posts/PostDTO.cs +++ b/exercise.wwwapi/DTOs/Posts/PostDTO.cs @@ -13,9 +13,6 @@ public class PostDTO [JsonPropertyName("body")] public string Body { get; set; } - [JsonPropertyName("likes")] - public int Likes { get; set; } - [JsonPropertyName("created_at")] public DateTime CreatedAt { get; set; } } diff --git a/exercise.wwwapi/Data/ModelSeeder.cs b/exercise.wwwapi/Data/ModelSeeder.cs index e04192f..dc75b3f 100644 --- a/exercise.wwwapi/Data/ModelSeeder.cs +++ b/exercise.wwwapi/Data/ModelSeeder.cs @@ -26,6 +26,7 @@ public static void Seed(ModelBuilder modelBuilder) SeedProfiles(ref modelBuilder); SeedPosts(ref modelBuilder); SeedComments(ref modelBuilder); + SeedLikes(ref modelBuilder); SeedCourses(ref modelBuilder); SeedCohorts(ref modelBuilder); SeedModules(ref modelBuilder); @@ -180,7 +181,6 @@ private static void SeedPosts(ref ModelBuilder modelBuilder) Id = 1, AuthorId = 1, Body = "Post 1 Body", - Likes = 5, CreatedAt = _seedTime, }, new Post @@ -188,7 +188,6 @@ private static void SeedPosts(ref ModelBuilder modelBuilder) Id = 2, AuthorId = 2, Body = "Post 2 Body", - Likes = 3, CreatedAt = _seedTime, }, new Post @@ -196,7 +195,6 @@ private static void SeedPosts(ref ModelBuilder modelBuilder) Id = 3, AuthorId = 3, Body = "Post 3 Body", - Likes = 10, CreatedAt = _seedTime, }, new Post @@ -204,7 +202,6 @@ private static void SeedPosts(ref ModelBuilder modelBuilder) Id = 4, AuthorId = 4, Body = "Post 4 Body", - Likes = 7, CreatedAt = _seedTime, }, new Post @@ -212,7 +209,6 @@ private static void SeedPosts(ref ModelBuilder modelBuilder) Id = 5, AuthorId = 5, Body = "Post 5 Body", - Likes = 9, CreatedAt = _seedTime, } ); @@ -264,6 +260,30 @@ private static void SeedComments(ref ModelBuilder modelBuilder) ); } + private static void SeedLikes(ref ModelBuilder modelBuilder) + { + modelBuilder.Entity().HasData( + new Like + { + Id = 1, + PostId = 1, + UserId = 1 + }, + new Like + { + Id = 2, + PostId = 1, + UserId = 2 + }, + new Like + { + Id = 3, + PostId = 1, + UserId = 3 + } + ); + } + private static void SeedCourses(ref ModelBuilder modelBuilder) { modelBuilder.Entity().HasData( diff --git a/exercise.wwwapi/Endpoints/PostEndpoints.cs b/exercise.wwwapi/Endpoints/PostEndpoints.cs index 4a4d092..5e83573 100644 --- a/exercise.wwwapi/Endpoints/PostEndpoints.cs +++ b/exercise.wwwapi/Endpoints/PostEndpoints.cs @@ -57,7 +57,6 @@ public static async Task CreatePost( { AuthorId = userId.Value, Body = request.Body!, - Likes = 0, CreatedAt = DateTime.UtcNow }; @@ -74,7 +73,6 @@ public static async Task CreatePost( Id = post.Id, AuthorId = post.AuthorId, Body = post.Body, - Likes = post.Likes, CreatedAt = post.CreatedAt } } @@ -116,7 +114,8 @@ private static async Task GetAllPostsVol2(IRepository postReposit { var results = (await postRepository.GetAllAsync( p => p.Author.Profile, - p => p.Comments + p => p.Comments, + p => p.Likes )).ToList(); var postData = new PostsSuccessDTOVol2 @@ -194,7 +193,6 @@ public static async Task UpdatePost(IRepository postRepository, i Id = post.Id, AuthorId = post.AuthorId, Body = post.Body, - Likes = post.Likes, CreatedAt = post.CreatedAt } }; @@ -243,7 +241,6 @@ public static async Task DeletePost(IRepository postRepository, i Id = post.Id, AuthorId = post.AuthorId, Body = post.Body, - Likes = post.Likes, CreatedAt = post.CreatedAt } }; diff --git a/exercise.wwwapi/Models/Like.cs b/exercise.wwwapi/Models/Like.cs new file mode 100644 index 0000000..02e930e --- /dev/null +++ b/exercise.wwwapi/Models/Like.cs @@ -0,0 +1,22 @@ +using exercise.wwwapi.Models.UserInfo; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace exercise.wwwapi.Models +{ + [Table("Likes")] + public class Like + { + [Key] + [Column("id")] + public int Id { get; set; } + [Column("post_id")] + [ForeignKey(nameof(Post))] + public int PostId { get; set; } + public Post Post { get; set; } + [Column("user_id")] + [ForeignKey(nameof(User))] + public int UserId { get; set; } + public User User { get; set; } + } +} diff --git a/exercise.wwwapi/Models/Post.cs b/exercise.wwwapi/Models/Post.cs index a073b4d..0c24f85 100644 --- a/exercise.wwwapi/Models/Post.cs +++ b/exercise.wwwapi/Models/Post.cs @@ -20,13 +20,10 @@ public class Post [Column("body", TypeName = "varchar(1000)")] public string Body { get; set; } - [Column("likes")] - public int Likes { get; set; } - - [Column("created_at")] + [Column("created_at", TypeName = "date")] public DateTime CreatedAt { get; set; } - [JsonIgnore] public User Author { get; set; } public ICollection Comments { get; set; } = new List(); + public ICollection Likes { get; set; } = new List(); } \ No newline at end of file