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
23 changes: 0 additions & 23 deletions exercise.wwwapi/DTOs/Comments/CommentDTO.cs

This file was deleted.

13 changes: 6 additions & 7 deletions exercise.wwwapi/DTOs/Comments/CommentsSuccessDTO.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System.Text.Json.Serialization;
using exercise.wwwapi.DTOs.Posts.GetPosts;
using System.Text.Json.Serialization;

namespace exercise.wwwapi.DTOs.Comments

public class CommentsSuccessDTO
{
public class CommentsSuccessDTO
{
[JsonPropertyName("comments")]
public List<CommentDTO> Comments { get; set; } = new();
}
[JsonPropertyName("comments")]
public List<CommentDTO> Comments { get; set; } = new();
}
2 changes: 1 addition & 1 deletion exercise.wwwapi/DTOs/GetObjects/PostsSuccessDTOVol2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace exercise.wwwapi.DTOs.GetObjects
public class PostsSuccessDTOVol2
{
[JsonPropertyName("posts")]
public List<PostDTOVol2> Posts { get; set; } = [];
public List<PostDTO> Posts { get; set; } = [];
}
}

9 changes: 9 additions & 0 deletions exercise.wwwapi/DTOs/Likes/LikeDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace exercise.wwwapi.DTOs.Likes
{
public class LikeDTO
{
public int Id { get; set; }
public int PostId { get; set; }
public int UserId { get; set; }
}
}
3 changes: 2 additions & 1 deletion exercise.wwwapi/DTOs/Posts/CreatePostSuccessDTO.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations.Schema;
using exercise.wwwapi.DTOs.Posts.GetPosts;
using System.ComponentModel.DataAnnotations.Schema;

namespace exercise.wwwapi.DTOs.Posts
{
Expand Down
11 changes: 7 additions & 4 deletions exercise.wwwapi/DTOs/Posts/GetPosts/CommentDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public class CommentDTO
public int UserId { get; set; }
public string Body { get; set; } = string.Empty;
public DateTime CreatedAt { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public string? firstName { get; set; }
public string? lastName { get; set; }
public CommentDTO()
{
}
Expand All @@ -19,8 +19,11 @@ public CommentDTO(Comment model)
UserId = model.UserId;
Body = model.Body;
CreatedAt = model.CreatedAt;
firstName = model.User.FirstName;
lastName = model.User.LastName;
if (model.User != null)
{
firstName = model.User.FirstName;
lastName = model.User.LastName;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace exercise.wwwapi.DTOs.Posts.GetPosts
{
public class PostDTOVol2
public class PostDTO
{
public int Id { get; set; }
public int AuthorId { get; set; }
Expand All @@ -14,11 +14,11 @@ public class PostDTOVol2
public List<CommentDTO> Comments { get; set; } = new List<CommentDTO>();
public List<LikeDTO> Likes { get; set; } = new List<LikeDTO>();

public PostDTOVol2()
public PostDTO()
{

}
public PostDTOVol2(Post model)
public PostDTO(Post model)
{
Id = model.Id;
AuthorId = model.AuthorId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace exercise.wwwapi.DTOs.Posts
{
public class PostDTO
public class PostDTO_old
{
[JsonPropertyName("id")]
public int Id { get; set; }
Expand Down
2 changes: 2 additions & 0 deletions exercise.wwwapi/DTOs/Posts/UpdatePost/UpdatePostSuccessDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ public class UpdatePostSuccessDTO

[JsonPropertyName("created_at")]
public DateTime CreatedAt { get; set; }
public DateTime? UpdatedAt { get; set; }
public int? UpdatedById { get; set; }
}
}
46 changes: 9 additions & 37 deletions exercise.wwwapi/Endpoints/CommentEndpoints.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using exercise.wwwapi.DTOs;
using exercise.wwwapi.DTOs.Comments;
using exercise.wwwapi.DTOs.Comments.UpdateComment;
using exercise.wwwapi.DTOs.Posts.GetPosts;
using exercise.wwwapi.Helpers;
using exercise.wwwapi.Models;
using exercise.wwwapi.Repository;
using FluentValidation;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Security.Claims;
using Post = exercise.wwwapi.Models.Post;

Expand All @@ -23,24 +25,15 @@ public static async Task ConfigureCommentEndpoints(this WebApplication app)
app.MapGet("/posts/{postId}/comments", GetCommentsPerPost).WithSummary("Get all comments for a post");
app.MapPost("/posts/{postId}/comments", CreateComment).RequireAuthorization().WithSummary("Create a comment");
}

[ProducesResponseType(StatusCodes.Status200OK)]
private static async Task<IResult> GetCommentsPerPost(IRepository<Comment> commentRepository,
ClaimsPrincipal comment, int postId)
{
var results = await commentRepository.GetAllAsync(c => c.Post);
var filtered = results.Where(c => c.PostId == postId).ToList();
var commentsForPost = await commentRepository.GetWithIncludes(c => c.Where(c => c.PostId == postId).Include(p => p.User));

var commentData = new CommentsSuccessDTO
{
Comments = filtered.Select(c => new CommentDTO
{
Id = c.Id,
PostId = postId,
UserId = c.UserId,
Body = c.Body,
CreatedAt = c.CreatedAt
}).ToList()
Comments = commentsForPost.Select(c => new CommentDTO(c)).ToList()
};

var response = new ResponseDTO<CommentsSuccessDTO>
Expand Down Expand Up @@ -98,14 +91,7 @@ public static async Task<IResult> CreateComment(
commentRepository.Insert(comment);
await commentRepository.SaveAsync();

var commentData = new CommentDTO
{
Id = comment.Id,
PostId = comment.PostId,
UserId = comment.UserId,
Body = comment.Body,
CreatedAt = comment.CreatedAt
};
var commentData = new CommentDTO(comment);

var response = new ResponseDTO<CommentDTO>
{
Expand Down Expand Up @@ -134,7 +120,7 @@ public static async Task<IResult> UpdateComment(
return Results.Unauthorized();
}

var comment = await commentRepository.GetByIdAsync(id);
var comment = await commentRepository.GetByIdWithIncludes(c => c.Include(u => u.User), id);

if (comment == null)
{
Expand Down Expand Up @@ -172,14 +158,7 @@ public static async Task<IResult> UpdateComment(
var response = new ResponseDTO<CommentDTO>
{
Status = "success",
Data = new CommentDTO
{
Id = comment.Id,
PostId = comment.PostId,
UserId = comment.UserId,
Body = comment.Body,
CreatedAt = comment.CreatedAt,
}
Data = new CommentDTO(comment)
};

return TypedResults.Ok(response);
Expand All @@ -202,7 +181,7 @@ public static async Task<IResult> DeleteComment(
return Results.Unauthorized();
}

var comment = await commentRepository.GetByIdAsync(id);
var comment = await commentRepository.GetByIdWithIncludes(c => c.Include(u => u.User), id);

if (comment == null)
{
Expand All @@ -220,14 +199,7 @@ public static async Task<IResult> DeleteComment(
var response = new ResponseDTO<CommentDTO>
{
Status = "success",
Data = new CommentDTO
{
Id = comment.Id,
PostId = comment.PostId,
UserId = comment.UserId,
Body = comment.Body,
CreatedAt = comment.CreatedAt
}
Data = new CommentDTO(comment)
};

return TypedResults.Ok(response);
Expand Down
75 changes: 75 additions & 0 deletions exercise.wwwapi/Endpoints/LikeEndpoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using exercise.wwwapi.DTOs;
using exercise.wwwapi.DTOs.Likes;
using exercise.wwwapi.Helpers;
using exercise.wwwapi.Models;
using exercise.wwwapi.Repository;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Security.Claims;

namespace exercise.wwwapi.Endpoints
{
public static class LikeEndpoint
{
public static async Task ConfigureLikeEndpoints(this WebApplication app)
{
var likes = app.MapGroup("likes");
likes.MapPost("/{postId}", toggleLike).RequireAuthorization().WithSummary("toggle between liked and unliked");

}
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public static async Task<IResult> toggleLike(
IRepository<Like> likeRepository,
IRepository<Post> postRepository,
ClaimsPrincipal claimsPrincipal,
int postId)
{
var userIdClaim = claimsPrincipal.UserRealId();
if (userIdClaim == null) return Results.Unauthorized();

var post = await postRepository.GetByIdWithIncludes(p => p.Include(l => l.Likes), postId);
if (post == null) return TypedResults.NotFound();
var isLiked = post.Likes.FirstOrDefault(l => l.UserId == userIdClaim);

if (isLiked != null)
{
var deleteResponse = new ResponseDTO<LikeDTO>
{
Status = "Success",
Data = new LikeDTO()
{
Id = isLiked.Id,
PostId = isLiked.PostId,
UserId = isLiked.UserId
}
};
likeRepository.Delete(isLiked);
await likeRepository.SaveAsync();
return Results.Ok(deleteResponse);
}

var like = new Like
{
PostId = postId,
UserId = userIdClaim.Value,
};
var response = new ResponseDTO<LikeDTO>
{
Status = "success",
Data = new LikeDTO
{
Id = like.Id,
PostId = like.PostId,
UserId = like.UserId
}
};

likeRepository.Insert(like);
await likeRepository.SaveAsync();

return Results.Created($"/likes/{postId}", response);
}
}
}
Loading
Loading