From 8123a815bd0591ec0d5ecb9b874c6141d65da9c7 Mon Sep 17 00:00:00 2001 From: Ognjen Cirkovic Date: Sun, 6 Nov 2022 20:40:07 +0100 Subject: Dodat api za preuzimanje slika pomocu id-a. --- Backend/Api/Api/Controllers/PostController.cs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'Backend') diff --git a/Backend/Api/Api/Controllers/PostController.cs b/Backend/Api/Api/Controllers/PostController.cs index 31dbeef..62f77f1 100644 --- a/Backend/Api/Api/Controllers/PostController.cs +++ b/Backend/Api/Api/Controllers/PostController.cs @@ -13,9 +13,11 @@ namespace Api.Controllers public class PostController : ControllerBase { private readonly IPostService _postService; - public PostController(IPostService postService) + private readonly IFileService _fileService; + public PostController(IPostService postService, IFileService fileService) { _postService = postService; + _fileService = fileService; } [HttpPost("add")] @@ -52,5 +54,18 @@ namespace Api.Controllers return BadRequest(); } + [HttpGet("image/{id}")] + [Authorize(Roles = "User")] + public async Task getImage(string id) + { + Models.File f =await _fileService.getById(id); + if (f == null || !System.IO.File.Exists(f.path)) + return BadRequest("Slika ne postoji"); + return File(System.IO.File.ReadAllBytes(f.path), "image/*", Path.GetFileName(f.path)); + + + } + + } } -- cgit v1.2.3 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') 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