aboutsummaryrefslogtreecommitdiff
path: root/Backend
diff options
context:
space:
mode:
Diffstat (limited to 'Backend')
-rw-r--r--Backend/Api/Api/Controllers/PostController.cs13
-rw-r--r--Backend/Api/Api/Interfaces/IPostService.cs1
-rw-r--r--Backend/Api/Api/Services/PostService.cs12
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();