Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions exercise.wwwapi/DTOs/Posts/GetPosts/LikeDTO.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
4 changes: 2 additions & 2 deletions exercise.wwwapi/DTOs/Posts/GetPosts/PostDTOVol2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CommentDTO> Comments { get; set; } = new List<CommentDTO>();
public List<LikeDTO> Likes { get; set; } = new List<LikeDTO>();

public PostDTOVol2()
{
Expand All @@ -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();
}
}
}
3 changes: 0 additions & 3 deletions exercise.wwwapi/DTOs/Posts/PostDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
Expand Down
30 changes: 25 additions & 5 deletions exercise.wwwapi/Data/ModelSeeder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -180,39 +181,34 @@ private static void SeedPosts(ref ModelBuilder modelBuilder)
Id = 1,
AuthorId = 1,
Body = "Post 1 Body",
Likes = 5,
CreatedAt = _seedTime,
},
new Post
{
Id = 2,
AuthorId = 2,
Body = "Post 2 Body",
Likes = 3,
CreatedAt = _seedTime,
},
new Post
{
Id = 3,
AuthorId = 3,
Body = "Post 3 Body",
Likes = 10,
CreatedAt = _seedTime,
},
new Post
{
Id = 4,
AuthorId = 4,
Body = "Post 4 Body",
Likes = 7,
CreatedAt = _seedTime,
},
new Post
{
Id = 5,
AuthorId = 5,
Body = "Post 5 Body",
Likes = 9,
CreatedAt = _seedTime,
}
);
Expand Down Expand Up @@ -264,6 +260,30 @@ private static void SeedComments(ref ModelBuilder modelBuilder)
);
}

private static void SeedLikes(ref ModelBuilder modelBuilder)
{
modelBuilder.Entity<Like>().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<Course>().HasData(
Expand Down
7 changes: 2 additions & 5 deletions exercise.wwwapi/Endpoints/PostEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public static async Task<IResult> CreatePost(
{
AuthorId = userId.Value,
Body = request.Body!,
Likes = 0,
CreatedAt = DateTime.UtcNow
};

Expand All @@ -74,7 +73,6 @@ public static async Task<IResult> CreatePost(
Id = post.Id,
AuthorId = post.AuthorId,
Body = post.Body,
Likes = post.Likes,
CreatedAt = post.CreatedAt
}
}
Expand Down Expand Up @@ -116,7 +114,8 @@ private static async Task<IResult> GetAllPostsVol2(IRepository<Post> postReposit
{
var results = (await postRepository.GetAllAsync(
p => p.Author.Profile,
p => p.Comments
p => p.Comments,
p => p.Likes
)).ToList();

var postData = new PostsSuccessDTOVol2
Expand Down Expand Up @@ -194,7 +193,6 @@ public static async Task<IResult> UpdatePost(IRepository<Post> postRepository, i
Id = post.Id,
AuthorId = post.AuthorId,
Body = post.Body,
Likes = post.Likes,
CreatedAt = post.CreatedAt
}
};
Expand Down Expand Up @@ -243,7 +241,6 @@ public static async Task<IResult> DeletePost(IRepository<Post> postRepository, i
Id = post.Id,
AuthorId = post.AuthorId,
Body = post.Body,
Likes = post.Likes,
CreatedAt = post.CreatedAt
}
};
Expand Down
22 changes: 22 additions & 0 deletions exercise.wwwapi/Models/Like.cs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like missing table name

userID missing foreign key

Original file line number Diff line number Diff line change
@@ -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; }
}
}
7 changes: 2 additions & 5 deletions exercise.wwwapi/Models/Post.cs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

createdAt missing TypeName

remove jsonignore

Original file line number Diff line number Diff line change
Expand Up @@ -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<Comment> Comments { get; set; } = new List<Comment>();
public ICollection<Like> Likes { get; set; } = new List<Like>();
}