aboutsummaryrefslogtreecommitdiff
path: root/Backend
diff options
context:
space:
mode:
Diffstat (limited to 'Backend')
-rw-r--r--Backend/Api/Api/Controllers/PostController.cs7
-rw-r--r--Backend/Api/Api/Interfaces/IPostService.cs2
-rw-r--r--Backend/Api/Api/Models/Post.cs6
-rw-r--r--Backend/Api/Api/Services/PostService.cs35
4 files changed, 42 insertions, 8 deletions
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<ActionResult> 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<ActionResult> removeRating(string id)
+ public async Task<ActionResult<int>> 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<List<PostSend>> getAllPosts();
Task<PostSend> getPostById(string id,string userid);
Task<PostSend> postToPostSend(Post post);
- Task<Boolean> AddOrReplaceRating(RatingReceive rating, string userid);
+ Task<RatingSend> AddOrReplaceRating(RatingReceive rating, string userid);
Task<Boolean> RemoveRating(string postid, string userid);
Task<CommentSend> AddComment(CommentReceive cmnt, string userid, string postid);
Task<List<CommentSend>> 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<Boolean> AddOrReplaceRating(RatingReceive rating,string userid)
+ public async Task<RatingSend> 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<Boolean> RemoveRating(string postid, string userid)
{