|
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; |
@@ -71,8 +72,6 @@ public static async Task<IResult> CreatePost( |
71 | 72 | { |
72 | 73 | Posts = new PostDTO |
73 | 74 | { |
74 | | - Id = post.Id, |
75 | | - AuthorId = post.AuthorId, |
76 | 75 | Body = post.Body, |
77 | 76 | CreatedAt = post.CreatedAt |
78 | 77 | } |
@@ -132,33 +131,23 @@ public static async Task<IResult> UpdatePost(IRepository<Post> postRepository, i |
132 | 131 | { |
133 | 132 | return Results.Unauthorized(); |
134 | 133 | } |
| 134 | + var userClaimName = claimsPrincipal.Identity?.Name; |
135 | 135 |
|
136 | 136 | 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); |
139 | 139 |
|
140 | 140 | if (post == null) |
141 | 141 | { |
142 | 142 | return TypedResults.NotFound(); |
143 | 143 | } |
144 | 144 |
|
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")) |
158 | 146 | { |
159 | 147 | post.UpdatedAt = DateTime.UtcNow; |
160 | | - post.UpdatedById = userIdClaim; |
| 148 | + post.UpdatedBy = userClaimName; |
161 | 149 | } |
| 150 | + else { return Results.Unauthorized(); } |
162 | 151 |
|
163 | 152 | var validation = await validator.ValidateAsync(request); |
164 | 153 | if (!validation.IsValid) |
@@ -188,12 +177,10 @@ public static async Task<IResult> UpdatePost(IRepository<Post> postRepository, i |
188 | 177 | Status = "success", |
189 | 178 | Data = new UpdatePostSuccessDTO |
190 | 179 | { |
191 | | - Id = post.Id, |
192 | | - AuthorId = post.AuthorId, |
193 | 180 | Body = post.Body, |
194 | 181 | CreatedAt = post.CreatedAt, |
195 | 182 | UpdatedAt = post.UpdatedAt, |
196 | | - UpdatedById = post.UpdatedById |
| 183 | + UpdatedBy = post.UpdatedBy |
197 | 184 |
|
198 | 185 | } |
199 | 186 | }; |
@@ -237,8 +224,8 @@ public static async Task<IResult> DeletePost(IRepository<Post> postRepository, i |
237 | 224 | Status = "success", |
238 | 225 | Data = new PostDTO |
239 | 226 | { |
240 | | - Id = post.Id, |
241 | | - AuthorId = post.AuthorId, |
| 227 | + //Id = post.Id, |
| 228 | + //AuthorId = post.AuthorId, |
242 | 229 | Body = post.Body, |
243 | 230 | CreatedAt = post.CreatedAt |
244 | 231 | } |
|
0 commit comments