From f5b8977e911adf5caba238d624c2668f2a57ba92 Mon Sep 17 00:00:00 2001 From: Ognjen Cirkovic Date: Sun, 6 Nov 2022 21:44:54 +0100 Subject: Pretvaranje Post-a u PostSend. --- Backend/Api/Api/Controllers/PostController.cs | 2 +- Backend/Api/Api/Interfaces/IPostService.cs | 2 +- Backend/Api/Api/Services/PostService.cs | 21 ++++++++++++++++----- 3 files changed, 18 insertions(+), 7 deletions(-) (limited to 'Backend/Api') diff --git a/Backend/Api/Api/Controllers/PostController.cs b/Backend/Api/Api/Controllers/PostController.cs index 62f77f1..a61ee2e 100644 --- a/Backend/Api/Api/Controllers/PostController.cs +++ b/Backend/Api/Api/Controllers/PostController.cs @@ -42,7 +42,7 @@ namespace Api.Controllers } return BadRequest(); } - [HttpGet("posts /{id}")] + [HttpGet("posts/{id}")] [Authorize(Roles = "User")] public async Task> getPostByid(string id) { diff --git a/Backend/Api/Api/Interfaces/IPostService.cs b/Backend/Api/Api/Interfaces/IPostService.cs index 31e80cd..29a824a 100644 --- a/Backend/Api/Api/Interfaces/IPostService.cs +++ b/Backend/Api/Api/Interfaces/IPostService.cs @@ -7,6 +7,6 @@ namespace Api.Interfaces Task addPost(PostReceive post); Task> getAllPosts(); Task getPostById(string id); - PostSend postToPostSend(Post post); + Task 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 0a12f39..e9a56d2 100644 --- a/Backend/Api/Api/Services/PostService.cs +++ b/Backend/Api/Api/Services/PostService.cs @@ -11,12 +11,14 @@ namespace Api.Services private readonly IMongoCollection _posts; private readonly IHttpContextAccessor _httpContext; private readonly IFileService _fileService; - public PostService(IDatabaseConnection settings, IMongoClient mongoClient, IHttpContextAccessor httpContext, IFileService fileService) + private readonly ILocationService _locationService; + public PostService(IDatabaseConnection settings, IMongoClient mongoClient, IHttpContextAccessor httpContext, IFileService fileService,ILocationService locationService) { var database = mongoClient.GetDatabase(settings.DatabaseName); _posts = database.GetCollection(settings.PostCollectionName); _httpContext = httpContext; _fileService = fileService; + _locationService = locationService; } public async Task addPost(PostReceive post) @@ -63,14 +65,23 @@ namespace Api.Services } await _posts.InsertOneAsync(p); - return postToPostSend(p); + return await postToPostSend(p); } - public PostSend postToPostSend(Post post) + public async Task postToPostSend(Post post) { PostSend p = new PostSend(); //Convert post to post send (TODO) p._id = post._id; + p.ownerId = post.ownerId; + p.description = post.description; + p.location = await _locationService.getById(post.locationId); + p.images = post.images; + p.views = 1;//Default values todo + p.ratings = 1; + p.comments = null; + + return p; } @@ -80,7 +91,7 @@ namespace Api.Services List temp = new List(); foreach (var post in posts) { - temp.Add(postToPostSend(post)); + temp.Add(await postToPostSend(post)); } return temp; } @@ -88,7 +99,7 @@ namespace Api.Services public async Task getPostById(string id) { Post p = await _posts.Find(post => post._id == id).FirstOrDefaultAsync(); - return postToPostSend(p); + return await postToPostSend(p); } //(TODO) ADD Delete and update -- cgit v1.2.3