|
4 | 4 | using exercise.wwwapi.DTOs.Posts.GetPosts; |
5 | 5 | using exercise.wwwapi.DTOs.Posts.UpdatePost; |
6 | 6 | using exercise.wwwapi.Helpers; |
| 7 | +using exercise.wwwapi.Models; |
7 | 8 | using exercise.wwwapi.Repository; |
8 | 9 | using FluentValidation; |
9 | 10 | using Microsoft.AspNetCore.Authorization; |
@@ -64,20 +65,20 @@ public static async Task<IResult> CreatePost( |
64 | 65 | postRepository.Insert(post); |
65 | 66 | await postRepository.SaveAsync(); |
66 | 67 |
|
67 | | - var response = new ResponseDTO<CreatePostSuccessDTO> |
68 | | - { |
69 | | - Status = "success", |
70 | | - Data = new CreatePostSuccessDTO |
| 68 | + var response = new ResponseDTO<CreatePostSuccessDTO> |
71 | 69 | { |
72 | | - Posts = new PostDTO |
| 70 | + Status = "success", |
| 71 | + Data = new CreatePostSuccessDTO |
73 | 72 | { |
74 | | - Id = post.Id, |
75 | | - AuthorId = post.AuthorId, |
76 | | - Body = post.Body, |
77 | | - CreatedAt = post.CreatedAt |
| 73 | + Posts = new PostDTO |
| 74 | + { |
| 75 | + Body = post.Body, |
| 76 | + CreatedAt = post.CreatedAt, |
| 77 | + Firstname = claimsPrincipal.FirstName(), |
| 78 | + Lastname = claimsPrincipal.LastName() |
| 79 | + } |
78 | 80 | } |
79 | | - } |
80 | | - }; |
| 81 | + }; |
81 | 82 |
|
82 | 83 | return Results.Created($"/posts/{post.Id}", response); |
83 | 84 | } |
@@ -132,33 +133,23 @@ public static async Task<IResult> UpdatePost(IRepository<Post> postRepository, i |
132 | 133 | { |
133 | 134 | return Results.Unauthorized(); |
134 | 135 | } |
| 136 | + var userClaimName = $"{claimsPrincipal.FirstName()} {claimsPrincipal.LastName()}"; |
135 | 137 |
|
136 | 138 | var post = await postRepository.GetByIdWithIncludes(p => p.Include(a => a.Author) |
137 | | - .Include(c => c.Comments) |
138 | | - .Include(l => l.Likes), id); |
| 139 | + .Include(c => c.Comments) |
| 140 | + .Include(l => l.Likes), id); |
139 | 141 |
|
140 | 142 | if (post == null) |
141 | 143 | { |
142 | 144 | return TypedResults.NotFound(); |
143 | 145 | } |
144 | 146 |
|
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 |
| 147 | + if (post.AuthorId == userIdClaim || claimsPrincipal.IsInRole("Teacher")) |
158 | 148 | { |
159 | 149 | post.UpdatedAt = DateTime.UtcNow; |
160 | | - post.UpdatedById = userIdClaim; |
| 150 | + post.UpdatedBy = userClaimName; |
161 | 151 | } |
| 152 | + else { return Results.Unauthorized(); } |
162 | 153 |
|
163 | 154 | var validation = await validator.ValidateAsync(request); |
164 | 155 | if (!validation.IsValid) |
@@ -188,12 +179,10 @@ public static async Task<IResult> UpdatePost(IRepository<Post> postRepository, i |
188 | 179 | Status = "success", |
189 | 180 | Data = new UpdatePostSuccessDTO |
190 | 181 | { |
191 | | - Id = post.Id, |
192 | | - AuthorId = post.AuthorId, |
193 | 182 | Body = post.Body, |
194 | 183 | CreatedAt = post.CreatedAt, |
195 | 184 | UpdatedAt = post.UpdatedAt, |
196 | | - UpdatedById = post.UpdatedById |
| 185 | + UpdatedBy = post.UpdatedBy |
197 | 186 |
|
198 | 187 | } |
199 | 188 | }; |
@@ -237,8 +226,6 @@ public static async Task<IResult> DeletePost(IRepository<Post> postRepository, i |
237 | 226 | Status = "success", |
238 | 227 | Data = new PostDTO |
239 | 228 | { |
240 | | - Id = post.Id, |
241 | | - AuthorId = post.AuthorId, |
242 | 229 | Body = post.Body, |
243 | 230 | CreatedAt = post.CreatedAt |
244 | 231 | } |
|
0 commit comments