diff options
author | branislav.radivojevic <wafflemynxyt@gmail.com> | 2022-11-28 14:06:40 +0100 |
---|---|---|
committer | branislav.radivojevic <wafflemynxyt@gmail.com> | 2022-11-28 14:06:40 +0100 |
commit | 78caf6a59402f930b0d828bc6ce5faac1c5b4ed4 (patch) | |
tree | 37990459798a862ca1b22a752f30ad481a00c8b5 /Backend/Api | |
parent | 37b2dff40d7e1395e3dc77bd7bb353e0181a37a6 (diff) |
Sortiranje istorije + bugfixes
Diffstat (limited to 'Backend/Api')
-rw-r--r-- | Backend/Api/Api/Models/Post.cs | 2 | ||||
-rw-r--r-- | Backend/Api/Api/Services/PostService.cs | 24 |
2 files changed, 21 insertions, 5 deletions
diff --git a/Backend/Api/Api/Models/Post.cs b/Backend/Api/Api/Models/Post.cs index 1a4129f..0dc8158 100644 --- a/Backend/Api/Api/Models/Post.cs +++ b/Backend/Api/Api/Models/Post.cs @@ -38,9 +38,11 @@ namespace Api.Models public DateTime createdAt { get; set; } public int views { get; set; } public double ratings { get; set; } + public int ratingscount { get; set; } public List<CommentSend> comments { get; set; } public List<File> images { get; set; } public List<string>? tags { get; set; } + public DateTime? lastViewed { get; set; } } public class Rating { diff --git a/Backend/Api/Api/Services/PostService.cs b/Backend/Api/Api/Services/PostService.cs index b75656e..0676d74 100644 --- a/Backend/Api/Api/Services/PostService.cs +++ b/Backend/Api/Api/Services/PostService.cs @@ -39,7 +39,10 @@ namespace Api.Services p.comments = new List<Comment>(); p.images = new List<Models.File>(); p.createdAt = DateTime.Now.ToUniversalTime(); - var tags = post.tags.Split("|").ToList(); + List<String> tags; + if (post.tags != "none") + tags = post.tags.Remove(post.tags.Length-1,1).Split("|").ToList(); + else tags = null; p.tags = tags; var folderPath = Path.Combine(Directory.GetCurrentDirectory(), "Files", p.ownerId); if (!Directory.Exists(folderPath)) @@ -85,6 +88,7 @@ namespace Api.Services p.views = post.views.Count(); p.createdAt = post.createdAt; p.tags = post.tags; + p.ratingscount = post.ratings.Count(); if (post.ratings.Count() > 0) { List<int> ratings = new List<int>(); @@ -116,9 +120,16 @@ namespace Api.Services Post p = await _posts.Find(post => post._id == id).FirstOrDefaultAsync(); if (p != null) { - if (!p.views.Any(x => x == userid)) + if (!p.views.Any(x => x.Split("|")[0] == userid)) { - p.views.Add(userid); + p.views.Add(userid + "|" + DateTime.Now.ToUniversalTime().ToString()); + await _posts.ReplaceOneAsync(x => x._id == id, p); + } + else + { + var v = p.views.Find(x => x.Split("|")[0] == userid); + p.views.Remove(v); + p.views.Add(userid + "|" + DateTime.Now.ToUniversalTime().ToString()); await _posts.ReplaceOneAsync(x => x._id == id, p); } } @@ -308,7 +319,7 @@ namespace Api.Services xd = ls.OrderByDescending(x => x.createdAt).ToList(); break; default: - + xd = ls.OrderByDescending(x => x.views).ToList(); break; } if(xd != null) @@ -360,12 +371,15 @@ namespace Api.Services var tosend = new List<PostSend>(); foreach (var post in posts) { - if (post.views.Any(x => x.Equals(userid))) + if (post.views.Any(x => x.Split("|")[0] == userid)) { + var t = post.views.Find(x => x.Split("|")[0] == userid); var x = await postToPostSend(post); + x.lastViewed = DateTime.Parse(t.Split("|")[1]).ToUniversalTime(); tosend.Add(x); } } + tosend = tosend.OrderByDescending(x => x.lastViewed).ToList(); return tosend; } |