diff options
author | branislav.radivojevic <wafflemynxyt@gmail.com> | 2022-11-28 19:48:33 +0100 |
---|---|---|
committer | branislav.radivojevic <wafflemynxyt@gmail.com> | 2022-11-28 19:48:33 +0100 |
commit | 50a2fec98d34b51d94e9ff23ee27eda6f6e28589 (patch) | |
tree | 57ed2d4bd7bf1d0d4ccfe1c435f53a6579b0ae0b /Backend/Api | |
parent | c20758b0194ecd6fbf50afbd808801542a8f292d (diff) |
brisanje posta back
Diffstat (limited to 'Backend/Api')
-rw-r--r-- | Backend/Api/Api/Controllers/PostController.cs | 13 | ||||
-rw-r--r-- | Backend/Api/Api/Interfaces/IPostService.cs | 1 | ||||
-rw-r--r-- | Backend/Api/Api/Services/PostService.cs | 12 |
3 files changed, 25 insertions, 1 deletions
diff --git a/Backend/Api/Api/Controllers/PostController.cs b/Backend/Api/Api/Controllers/PostController.cs index ec5829e..3d7199c 100644 --- a/Backend/Api/Api/Controllers/PostController.cs +++ b/Backend/Api/Api/Controllers/PostController.cs @@ -56,7 +56,18 @@ namespace Api.Controllers } return BadRequest(); } - + [HttpGet("posts/delete/{id}")] + [Authorize(Roles = "User")] + public async Task<ActionResult<string>> deletePost(string id) + { + var userid = await _userService.UserIdFromJwt(); + var res = await _postService.deletePost(id, userid); + if (res) + { + return Ok(res); + } + return BadRequest("Post ne postoji ili vi niste vlasnik"); + } [HttpGet("image/{id}")] //[Authorize(Roles = "User")] public async Task<ActionResult> getImage(string id) diff --git a/Backend/Api/Api/Interfaces/IPostService.cs b/Backend/Api/Api/Interfaces/IPostService.cs index 9bf71d5..60781bb 100644 --- a/Backend/Api/Api/Interfaces/IPostService.cs +++ b/Backend/Api/Api/Interfaces/IPostService.cs @@ -5,6 +5,7 @@ namespace Api.Interfaces public interface IPostService { Task<PostSend> addPost(PostReceive post); + Task<Boolean> deletePost(string postid, string userid); Task<List<PostSend>> getAllPosts(); Task<PostSend> getPostById(string id,string userid); Task<PostSend> postToPostSend(Post post); diff --git a/Backend/Api/Api/Services/PostService.cs b/Backend/Api/Api/Services/PostService.cs index e59c735..dcfd5e0 100644 --- a/Backend/Api/Api/Services/PostService.cs +++ b/Backend/Api/Api/Services/PostService.cs @@ -104,6 +104,18 @@ namespace Api.Services return p; } + public async Task<Boolean> deletePost(string postid,string userid) + { + var p = await _posts.Find(x => x._id == postid).FirstOrDefaultAsync(); + if (p == null || p.ownerId != userid) + return false; + foreach (var image in p.images) + System.IO.File.Delete(image.path); + + await _posts.DeleteOneAsync(postid); + return true; + } + public async Task<List<PostSend>> getAllPosts() { List<Post> posts = await _posts.Find(_ => true).ToListAsync(); |