Skip to content

Commit 5f62369

Browse files
committed
updated endpoints so update now gives name instead of id, updated DTOs to not return id
1 parent 0a56d86 commit 5f62369

File tree

8 files changed

+34
-55
lines changed

8 files changed

+34
-55
lines changed

api.tests/PostEndpointTests/UpdatePostTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ public async Task UpdatePostPassesTest()
8585
Assert.That(updatedResult, Is.Not.Null, "Update Failed");
8686
Assert.That(patchResponse.StatusCode, Is.EqualTo(System.Net.HttpStatusCode.OK));
8787
Assert.That(updatedResult!.Data.Body, Is.EqualTo(newBody));
88-
Assert.That(updatedResult!.Data.Id, Is.EqualTo(1));
89-
Assert.That(updatedResult!.Data.AuthorId, Is.EqualTo(1));
9088
}
9189
}
9290
}

exercise.wwwapi/DTOs/Posts/GetPosts/CommentDTO.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,24 @@ namespace exercise.wwwapi.DTOs.Posts.GetPosts
44
{
55
public class CommentDTO
66
{
7-
public int Id { get; set; }
8-
public int UserId { get; set; }
9-
public string Body { get; set; } = string.Empty;
10-
public DateTime CreatedAt { get; set; }
117
public string? firstName { get; set; }
128
public string? lastName { get; set; }
13-
public CommentDTO()
14-
{
15-
}
9+
public string Body { get; set; } = string.Empty;
10+
public DateTime CreatedAt { get; set; }
11+
public DateTime? UpdatedAt { get; set; }
12+
public string? UpdatedBy { get; set; }
13+
public CommentDTO() { }
1614
public CommentDTO(Comment model)
1715
{
18-
Id = model.Id;
19-
UserId = model.UserId;
2016
Body = model.Body;
2117
CreatedAt = model.CreatedAt;
2218
if (model.User != null)
2319
{
2420
firstName = model.User.FirstName;
2521
lastName = model.User.LastName;
2622
}
23+
UpdatedAt = model.UpdatedAt;
24+
UpdatedBy = model.UpdatedBy;
2725
}
2826
}
2927
}

exercise.wwwapi/DTOs/Posts/GetPosts/PostDTO.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,26 @@ namespace exercise.wwwapi.DTOs.Posts.GetPosts
55
{
66
public class PostDTO
77
{
8-
public int Id { get; set; }
9-
public int AuthorId { get; set; }
108
public string Firstname { get; set; }
119
public string Lastname { get; set; }
1210
public string Body { get; set; } = string.Empty;
1311
public DateTime CreatedAt { get; set; }
12+
public DateTime? UpdatedAt { get; set; }
13+
public string? UpdatedBy { get; set; }
1414
public List<CommentDTO> Comments { get; set; } = new List<CommentDTO>();
1515
public List<LikeDTO> Likes { get; set; } = new List<LikeDTO>();
1616

17-
public PostDTO()
18-
{
19-
20-
}
17+
public PostDTO() { }
2118
public PostDTO(Post model)
2219
{
23-
Id = model.Id;
24-
AuthorId = model.AuthorId;
25-
Body = model.Body;
26-
CreatedAt = model.CreatedAt;
2720
Firstname = model.Author.FirstName;
2821
Lastname = model.Author.LastName;
22+
Body = model.Body;
23+
CreatedAt = model.CreatedAt;
2924
Comments = model.Comments.Select(c => new CommentDTO(c)).ToList();
3025
Likes = model.Likes.Select(l => new LikeDTO(l)).ToList();
26+
UpdatedAt = model.UpdatedAt;
27+
UpdatedBy = model.UpdatedBy;
3128
}
3229
}
3330
}

exercise.wwwapi/DTOs/Posts/UpdatePost/UpdatePostSuccessDTO.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ namespace exercise.wwwapi.DTOs.Posts.UpdatePost
44
{
55
public class UpdatePostSuccessDTO
66
{
7-
[JsonPropertyName("id")]
8-
public int Id { get; set; }
9-
10-
[JsonPropertyName("author_id")]
11-
public int AuthorId { get; set; }
12-
137
[JsonPropertyName("body")]
148
public string Body { get; set; }
159

@@ -19,6 +13,6 @@ public class UpdatePostSuccessDTO
1913
[JsonPropertyName("created_at")]
2014
public DateTime CreatedAt { get; set; }
2115
public DateTime? UpdatedAt { get; set; }
22-
public int? UpdatedById { get; set; }
16+
public string? UpdatedBy { get; set; }
2317
}
2418
}

exercise.wwwapi/Endpoints/CommentEndpoints.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ public static async Task<IResult> UpdateComment(
119119
{
120120
return Results.Unauthorized();
121121
}
122+
var userClaimName = claimsPrincipal.Identity?.Name;
122123

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

@@ -127,12 +128,14 @@ public static async Task<IResult> UpdateComment(
127128
return TypedResults.NotFound();
128129
}
129130

130-
if (comment.UserId != userIdClaim)
131+
if (comment.UserId == userIdClaim || claimsPrincipal.IsInRole("Teacher"))
131132
{
132-
return Results.Unauthorized();
133+
comment.UpdatedAt = DateTime.UtcNow;
134+
comment.UpdatedBy = userClaimName;
133135
}
136+
else { return Results.Unauthorized(); }
134137

135-
var validation = await validator.ValidateAsync(request);
138+
var validation = await validator.ValidateAsync(request);
136139
if (!validation.IsValid)
137140
{
138141
var failureDto = new UpdateCommentFailureDTO();

exercise.wwwapi/Endpoints/PostEndpoints.cs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using exercise.wwwapi.DTOs.Posts.GetPosts;
55
using exercise.wwwapi.DTOs.Posts.UpdatePost;
66
using exercise.wwwapi.Helpers;
7+
using exercise.wwwapi.Models;
78
using exercise.wwwapi.Repository;
89
using FluentValidation;
910
using Microsoft.AspNetCore.Authorization;
@@ -71,8 +72,6 @@ public static async Task<IResult> CreatePost(
7172
{
7273
Posts = new PostDTO
7374
{
74-
Id = post.Id,
75-
AuthorId = post.AuthorId,
7675
Body = post.Body,
7776
CreatedAt = post.CreatedAt
7877
}
@@ -132,33 +131,23 @@ public static async Task<IResult> UpdatePost(IRepository<Post> postRepository, i
132131
{
133132
return Results.Unauthorized();
134133
}
134+
var userClaimName = claimsPrincipal.Identity?.Name;
135135

136136
var post = await postRepository.GetByIdWithIncludes(p => p.Include(a => a.Author)
137-
.Include(c => c.Comments)
138-
.Include(l => l.Likes), id);
137+
.Include(c => c.Comments)
138+
.Include(l => l.Likes), id);
139139

140140
if (post == null)
141141
{
142142
return TypedResults.NotFound();
143143
}
144144

145-
if (post.AuthorId != userIdClaim)
146-
{
147-
if (claimsPrincipal.IsInRole("Teacher"))
148-
{
149-
post.UpdatedAt = DateTime.UtcNow;
150-
post.UpdatedById = userIdClaim;
151-
}
152-
else
153-
{
154-
return Results.Unauthorized();
155-
}
156-
}
157-
else
145+
if (post.AuthorId == userIdClaim || claimsPrincipal.IsInRole("Teacher"))
158146
{
159147
post.UpdatedAt = DateTime.UtcNow;
160-
post.UpdatedById = userIdClaim;
148+
post.UpdatedBy = userClaimName;
161149
}
150+
else { return Results.Unauthorized(); }
162151

163152
var validation = await validator.ValidateAsync(request);
164153
if (!validation.IsValid)
@@ -188,12 +177,10 @@ public static async Task<IResult> UpdatePost(IRepository<Post> postRepository, i
188177
Status = "success",
189178
Data = new UpdatePostSuccessDTO
190179
{
191-
Id = post.Id,
192-
AuthorId = post.AuthorId,
193180
Body = post.Body,
194181
CreatedAt = post.CreatedAt,
195182
UpdatedAt = post.UpdatedAt,
196-
UpdatedById = post.UpdatedById
183+
UpdatedBy = post.UpdatedBy
197184

198185
}
199186
};
@@ -237,8 +224,8 @@ public static async Task<IResult> DeletePost(IRepository<Post> postRepository, i
237224
Status = "success",
238225
Data = new PostDTO
239226
{
240-
Id = post.Id,
241-
AuthorId = post.AuthorId,
227+
//Id = post.Id,
228+
//AuthorId = post.AuthorId,
242229
Body = post.Body,
243230
CreatedAt = post.CreatedAt
244231
}

exercise.wwwapi/Models/Comment.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public class Comment : IEntity
2626

2727
[Column("created_at")]
2828
public DateTime CreatedAt { get; set; }
29+
public DateTime? UpdatedAt { get; set; }
30+
public string? UpdatedBy { get; set; }
2931

3032
[JsonIgnore]
3133
public Post Post { get; set; }

exercise.wwwapi/Models/Post.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class Post : IEntity
2424
public DateTime CreatedAt { get; set; }
2525

2626
public DateTime? UpdatedAt { get; set; }
27-
public int? UpdatedById { get; set; }
27+
public string? UpdatedBy { get; set; }
2828

2929
public User Author { get; set; }
3030
public ICollection<Comment> Comments { get; set; } = new List<Comment>();

0 commit comments

Comments
 (0)