diff options
-rw-r--r-- | Backend/Api/Api/Controllers/PostController.cs | 38 | ||||
-rw-r--r-- | Backend/Api/Api/Interfaces/IPostService.cs | 5 | ||||
-rw-r--r-- | Backend/Api/Api/Services/PostService.cs | 49 |
3 files changed, 87 insertions, 5 deletions
diff --git a/Backend/Api/Api/Controllers/PostController.cs b/Backend/Api/Api/Controllers/PostController.cs index 4bac0e5..8db68a8 100644 --- a/Backend/Api/Api/Controllers/PostController.cs +++ b/Backend/Api/Api/Controllers/PostController.cs @@ -1,4 +1,7 @@ using Api.Interfaces; +using Api.Models; +using Api.Services; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 @@ -14,5 +17,40 @@ namespace Api.Controllers { _postService = postService; } + + [HttpPost("add")] + [Authorize(Roles ="User")] + public async Task<ActionResult<PostSend>> addPost([FromBody] PostReceive post) + { + var res = await _postService.addPost(post); + if (res != null) + { + return Ok(res); + } + return BadRequest(); + } + [HttpGet] + [Authorize(Roles = "User")] + public async Task<ActionResult<List<PostSend>>> getAllPosts() + { + var res = await _postService.getAllPosts(); + if (res != null) + { + return Ok(res); + } + return BadRequest(); + } + [HttpGet("posts /{id}")] + [Authorize(Roles = "User")] + public async Task<ActionResult<PostSend>> getPostByid(string id) + { + var res = await _postService.getPostById(id); + if (res != null) + { + return Ok(res); + } + return BadRequest(); + } + } } diff --git a/Backend/Api/Api/Interfaces/IPostService.cs b/Backend/Api/Api/Interfaces/IPostService.cs index 6c34e69..31e80cd 100644 --- a/Backend/Api/Api/Interfaces/IPostService.cs +++ b/Backend/Api/Api/Interfaces/IPostService.cs @@ -4,6 +4,9 @@ namespace Api.Interfaces { public interface IPostService { - PostSend addPost(PostReceive post); + Task<PostSend> addPost(PostReceive post); + Task<List<PostSend>> getAllPosts(); + Task<PostSend> getPostById(string id); + PostSend postToPostSend(Post post); } }
\ No newline at end of file diff --git a/Backend/Api/Api/Services/PostService.cs b/Backend/Api/Api/Services/PostService.cs index 2a5e789..49c92cb 100644 --- a/Backend/Api/Api/Services/PostService.cs +++ b/Backend/Api/Api/Services/PostService.cs @@ -1,4 +1,5 @@ -using Api.Interfaces; +using System.Security.Claims; +using Api.Interfaces; using Api.Models; using MongoDB.Driver; @@ -8,15 +9,55 @@ namespace Api.Services { private readonly MongoClient _client; private readonly IMongoCollection<Post> _posts; - public PostService(IDatabaseConnection settings, IMongoClient mongoClient) + private readonly IHttpContextAccessor _httpContext; + public PostService(IDatabaseConnection settings, IMongoClient mongoClient, IHttpContextAccessor httpContext) { var database = mongoClient.GetDatabase(settings.DatabaseName); _posts = database.GetCollection<Post>(settings.PostCollectionName); + _httpContext = httpContext; } - public PostSend addPost(PostReceive post) + public async Task<PostSend> addPost(PostReceive post) { - return null; + Post p = new Post(); + p._id = ""; + p.ownerId = _httpContext.HttpContext.User.FindFirstValue("id"); + p.location = post.location; + p.description = post.description; + p.views = new List<string>(); + p.reports = new List<string>(); + p.ratings = new List<Rating>(); + p.comments = new List<Comment>(); + //add file + //add to database + return postToPostSend(p); + + } + public PostSend postToPostSend(Post post) + { + PostSend p = new PostSend(); + //Convert post to post send (TODO) + p._id = post._id; + return p; + } + + public async Task<List<PostSend>> getAllPosts() + { + List<Post> posts = await _posts.Find(_ => true).ToListAsync(); + List<PostSend> temp = new List<PostSend>(); + foreach (var post in posts) + { + temp.Add(postToPostSend(post)); + } + return temp; + } + + public async Task<PostSend> getPostById(string id) + { + Post p = await _posts.Find(post => post._id == id).FirstOrDefaultAsync(); + return postToPostSend(p); + } + //(TODO) ADD Delete and update } } |