From 68b86d96142eb1f9e42ba2d87a4277d014759c46 Mon Sep 17 00:00:00 2001 From: "branislav.radivojevic" Date: Mon, 28 Nov 2022 22:41:44 +0100 Subject: prikaz prethodne ocene, broj ocena,prikaz --- Backend/Api/Api/Controllers/PostController.cs | 7 +++--- Backend/Api/Api/Interfaces/IPostService.cs | 2 +- Backend/Api/Api/Models/Post.cs | 6 +++++ Backend/Api/Api/Services/PostService.cs | 35 ++++++++++++++++++++++++--- 4 files changed, 42 insertions(+), 8 deletions(-) (limited to 'Backend/Api') diff --git a/Backend/Api/Api/Controllers/PostController.cs b/Backend/Api/Api/Controllers/PostController.cs index 3d7199c..88c1c99 100644 --- a/Backend/Api/Api/Controllers/PostController.cs +++ b/Backend/Api/Api/Controllers/PostController.cs @@ -92,14 +92,15 @@ namespace Api.Controllers public async Task addRating([FromBody] RatingReceive rating,string id) { var userid = await _userService.UserIdFromJwt(); - if (await _postService.AddOrReplaceRating(rating, userid)) - return Ok(); + var rez = await _postService.AddOrReplaceRating(rating, userid); + if(rez != null) + return Ok(rez); return BadRequest(); } [HttpDelete("posts/{id}/removerating")] [Authorize(Roles = "User")] - public async Task removeRating(string id) + public async Task> removeRating(string id) { var userid = await _userService.UserIdFromJwt(); if (await _postService.RemoveRating(id,userid)) diff --git a/Backend/Api/Api/Interfaces/IPostService.cs b/Backend/Api/Api/Interfaces/IPostService.cs index 60781bb..a9adb05 100644 --- a/Backend/Api/Api/Interfaces/IPostService.cs +++ b/Backend/Api/Api/Interfaces/IPostService.cs @@ -9,7 +9,7 @@ namespace Api.Interfaces Task> getAllPosts(); Task getPostById(string id,string userid); Task postToPostSend(Post post); - Task AddOrReplaceRating(RatingReceive rating, string userid); + Task AddOrReplaceRating(RatingReceive rating, string userid); Task RemoveRating(string postid, string userid); Task AddComment(CommentReceive cmnt, string userid, string postid); Task> ListComments(string postid); diff --git a/Backend/Api/Api/Models/Post.cs b/Backend/Api/Api/Models/Post.cs index 0dc8158..78b4705 100644 --- a/Backend/Api/Api/Models/Post.cs +++ b/Backend/Api/Api/Models/Post.cs @@ -49,6 +49,12 @@ namespace Api.Models public string userId { get; set; } public int rating { get; set; } } + public class RatingSend + { + public int ratingscount { get; set; } + public double ratings { get; set; } + public int myrating { get; set; } + } public class Comment { [BsonId] diff --git a/Backend/Api/Api/Services/PostService.cs b/Backend/Api/Api/Services/PostService.cs index dcfd5e0..c57cfec 100644 --- a/Backend/Api/Api/Services/PostService.cs +++ b/Backend/Api/Api/Services/PostService.cs @@ -148,13 +148,34 @@ namespace Api.Services return await postToPostSend(p); } - public async Task AddOrReplaceRating(RatingReceive rating,string userid) + public async Task AddOrReplaceRating(RatingReceive rating,string userid) //0 return existing flag , -1 rating failed flag { Post p = await _posts.Find(post => post._id == rating.postId).FirstOrDefaultAsync(); if (p != null) { + var tosend = new RatingSend(); + var ps = await postToPostSend(p); + tosend.ratings = ps.ratings; + tosend.ratingscount = ps.ratingscount; + if (p.ownerId == userid) - return false; + return null; + if(rating.rating == 0)// ako nema rating staviti 0 + { + var r = p.ratings.Find(x => x.userId == userid); + if(r != null) + { + tosend.myrating=r.rating; + return tosend; + } + else + { + tosend.myrating = 0; + return tosend; + } + } + if(rating.rating<1 || rating.rating>5) + return null; if(!p.ratings.Any(x => x.userId == userid)) { Rating r = new Rating(); @@ -162,6 +183,7 @@ namespace Api.Services r.userId = userid; p.ratings.Add(r); await _posts.ReplaceOneAsync(x => x._id == p._id, p); + tosend.myrating=rating.rating; } else { @@ -170,10 +192,15 @@ namespace Api.Services r.rating = rating.rating; p.ratings.Add(r); await _posts.ReplaceOneAsync(x => x._id == p._id, p); + tosend.myrating = rating.rating; } - return true; + p = await _posts.Find(post => post._id == rating.postId).FirstOrDefaultAsync(); + ps = await postToPostSend(p); + tosend.ratings = ps.ratings; + tosend.ratingscount = ps.ratingscount; + return tosend; } - return false; + return null; } public async Task RemoveRating(string postid, string userid) { -- cgit v1.2.3