aboutsummaryrefslogtreecommitdiff
path: root/Backend/Api
diff options
context:
space:
mode:
Diffstat (limited to 'Backend/Api')
-rw-r--r--Backend/Api/Api/Controllers/AuthController.cs10
-rw-r--r--Backend/Api/Api/Controllers/PostController.cs23
-rw-r--r--Backend/Api/Api/Controllers/UserController.cs6
-rw-r--r--Backend/Api/Api/Interfaces/IPostService.cs4
-rw-r--r--Backend/Api/Api/Interfaces/IUserService.cs2
-rw-r--r--Backend/Api/Api/Models/Post.cs31
-rw-r--r--Backend/Api/Api/Models/User.cs14
-rw-r--r--Backend/Api/Api/Services/MessageService.cs7
-rw-r--r--Backend/Api/Api/Services/PostService.cs258
-rw-r--r--Backend/Api/Api/Services/UserService.cs56
10 files changed, 329 insertions, 82 deletions
diff --git a/Backend/Api/Api/Controllers/AuthController.cs b/Backend/Api/Api/Controllers/AuthController.cs
index abb7adc..b63665b 100644
--- a/Backend/Api/Api/Controllers/AuthController.cs
+++ b/Backend/Api/Api/Controllers/AuthController.cs
@@ -120,5 +120,15 @@ namespace Api.Controllers
return base.Content(html, "text/html");
}
}
+ [HttpGet("jwttoid")]
+ [Authorize(Roles = "User")]
+ public async Task<ActionResult<string>> JwtToUserId()
+ {
+ var userid = await _userService.UserIdFromJwt();
+ if (userid != null)
+ return Ok(userid);
+ return BadRequest();
+ }
+
}
}
diff --git a/Backend/Api/Api/Controllers/PostController.cs b/Backend/Api/Api/Controllers/PostController.cs
index 61a4f48..97646c2 100644
--- a/Backend/Api/Api/Controllers/PostController.cs
+++ b/Backend/Api/Api/Controllers/PostController.cs
@@ -56,7 +56,7 @@ namespace Api.Controllers
}
return BadRequest();
}
- [HttpGet("posts/delete/{id}")]
+ [HttpDelete("posts/delete/{id}")]
[Authorize(Roles = "User")]
public async Task<ActionResult<string>> deletePost(string id)
{
@@ -140,9 +140,9 @@ namespace Api.Controllers
}
[HttpGet("locations/{id}/posts")]
[Authorize(Roles = "User")]
- public async Task<ActionResult<List<PostSend>>> searchPosts(string id,int page=0,int sorttype=1,int filterdate=1)
+ public async Task<ActionResult<List<PostSend>>> searchPosts(string id,bool filter,int page=0,int sorttype=1,int filterdate=1,int ratingFrom=-1, int ratingTo=-1,int viewsFrom=-1,int viewsTo=-1)
{
- var res = await _postService.SearchPosts(id,page,sorttype,filterdate);
+ var res = await _postService.SearchPosts(id,filter,page,sorttype,filterdate,ratingFrom,ratingTo,viewsFrom,viewsTo);
if (res != null)
{
return Ok(res);
@@ -199,5 +199,20 @@ namespace Api.Controllers
{
return Ok(await _postService.TrendingTags());
}
- }
+
+ [HttpGet("userFavouritePosts")]
+ [Authorize(Roles = "User")]
+ public async Task<ActionResult<List<PostSend>>> getUserFavouritePosts()
+ {
+ return Ok(await _postService.userFavouritePosts());
+ }
+
+ [HttpGet("posts/getAllPostsFilterSort")]
+ [Authorize(Roles = "User")]
+ public async Task<ActionResult<List<PostSend>>> GetAllPostsFilterSort([FromBody] FilterSort fs)
+ {
+ return Ok(await _postService.GetAllPostsFilterSort(fs));
+ }
+
+ }
}
diff --git a/Backend/Api/Api/Controllers/UserController.cs b/Backend/Api/Api/Controllers/UserController.cs
index 73d32ea..abdf685 100644
--- a/Backend/Api/Api/Controllers/UserController.cs
+++ b/Backend/Api/Api/Controllers/UserController.cs
@@ -162,6 +162,12 @@ namespace Api.Controllers
return Ok(await _userService.ChangeMyProfileName(newName));
}
+ [HttpPost("changePass")]
+ [Authorize(Roles = "User")]
+ public async Task<ActionResult<int>> ChangePass([FromBody] ChangePass cp)
+ {
+ return Ok(await _userService.ChangePass(cp.currentPass,cp.newPass));
+ }
}
diff --git a/Backend/Api/Api/Interfaces/IPostService.cs b/Backend/Api/Api/Interfaces/IPostService.cs
index c854601..f554df6 100644
--- a/Backend/Api/Api/Interfaces/IPostService.cs
+++ b/Backend/Api/Api/Interfaces/IPostService.cs
@@ -16,7 +16,7 @@ namespace Api.Interfaces
Task<List<CommentSend>> CascadeComments(string parentid, Post p);
Task<Boolean> DeleteComments(string postid, string cmntid,string userid);
Task CascadeDeleteComments(string cmntid, Post p);
- Task<PostSendPage> SearchPosts(string locid, int page = 0, int sorttype = 1, int filterdate = 1);
+ Task<PostSendPage> SearchPosts(string locid,bool filter, int page = 0, int sorttype = 1, int filterdate = 1, int ratingFrom = -1, int ratingTo = -1, int viewsFrom = -1, int viewsTo = -1);
int DateEnumToDays(int filterdate);
Task<List<PostSend>> GetUsersPosts(string id);
Task<List<PostSend>> UserHistory(string userid);
@@ -31,5 +31,7 @@ namespace Api.Interfaces
Task<List<Trending>> TrendingTags();
Task<List<PostSend>> BestPostForAllLocationsInRadius(Coords coords, double radius);
Task<UserStats> UserStats(string userid);
+ Task<List<PostSend>> userFavouritePosts();
+ Task<List<PostSend>> GetAllPostsFilterSort(FilterSort fs);
}
} \ No newline at end of file
diff --git a/Backend/Api/Api/Interfaces/IUserService.cs b/Backend/Api/Api/Interfaces/IUserService.cs
index f4954e0..855272f 100644
--- a/Backend/Api/Api/Interfaces/IUserService.cs
+++ b/Backend/Api/Api/Interfaces/IUserService.cs
@@ -40,6 +40,8 @@ namespace Api.Interfaces
Task<int> ChangeMyProfileUsername(String newUsername);
Task<bool> ChangeMyProfileName(String newUsername);
+ Task<int> ChangePass(string currentPass, string newPass);
+
}
}
diff --git a/Backend/Api/Api/Models/Post.cs b/Backend/Api/Api/Models/Post.cs
index 9c0c429..9f7e937 100644
--- a/Backend/Api/Api/Models/Post.cs
+++ b/Backend/Api/Api/Models/Post.cs
@@ -92,7 +92,9 @@ namespace Api.Models
{
VIEWS_DESC=1,
RATING_DESC=2,
- DATE =3
+ DATE =3,
+ DATE_ASC=4,
+
}
public enum FilterDate
{
@@ -123,4 +125,31 @@ namespace Api.Models
public TagR tagr { get; set; }
public PostSendPage page { get; set; }
}
+
+ public class FilterSort
+ {
+ public List<PostSend> posts { get; set; }
+ public bool sort { get; set; }
+ public bool filter { get; set; }
+ public bool filterDate { get; set; }
+ public bool filterRating { get; set; }
+ public bool filterViews{ get; set; }
+ public DateTime filterDateFrom { get; set; }
+ public DateTime filterDateTo { get; set; }
+ public int filterRatingFrom { get; set; }
+ public int filterRatingTo { get; set; }
+ public int filterViewsFrom { get; set; }
+ public int filterViewsTo { get; set; }
+
+ public bool sortLatest { get; set; }
+ public bool sortOldest { get; set; }
+ public bool sortBest{ get; set; }
+ public bool sortMostViews{ get; set; }
+
+
+
+
+
+
+ }
}
diff --git a/Backend/Api/Api/Models/User.cs b/Backend/Api/Api/Models/User.cs
index cf16dbe..f789ffe 100644
--- a/Backend/Api/Api/Models/User.cs
+++ b/Backend/Api/Api/Models/User.cs
@@ -21,6 +21,7 @@ namespace Api.Models
public List<string> following { get; set; }
public int followersCount { get; set; }
public int followingCount { get; set; }
+ public List<string>? favourites { get; set; }
}
public class Login
@@ -64,11 +65,12 @@ namespace Api.Models
public File? pfp { get; set; }
public int postcount { get; set; }
- public List<String> followers{ get; set; }
+ public List<String> followers { get; set; }
public List<String> following { get; set; }
public int followersCount { get; set; }
- public int followingCount { get; set; }
+ public int followingCount { get; set; }
+ public List<string>? favourites { get; set; }
}
@@ -78,8 +80,9 @@ namespace Api.Models
public int totalViews { get; set; }
public int numberOfPosts { get; set; }
public int numberOfRatingsOnPosts { get; set; }
- public double averagePostRatingOnPosts {get; set; }
+ public double averagePostRatingOnPosts { get; set; }
public List<MonthlyViews> monthlyViews { get; set; }
+ public int numberOfFavouritePosts { get; set; }
}
public class MonthlyViews
@@ -87,4 +90,9 @@ namespace Api.Models
public int month { get; set; }
public int views { get; set; }
}
+ public class ChangePass
+ {
+ public string currentPass { get; set; }
+ public string newPass { get; set; }
+ }
}
diff --git a/Backend/Api/Api/Services/MessageService.cs b/Backend/Api/Api/Services/MessageService.cs
index 9cc818b..71df70c 100644
--- a/Backend/Api/Api/Services/MessageService.cs
+++ b/Backend/Api/Api/Services/MessageService.cs
@@ -10,13 +10,15 @@ namespace Api.Services
{
private readonly IHttpContextAccessor _httpContext;
private readonly IMongoCollection<Message> _messages;
+ private readonly IUserService _userService;
private readonly IJwtService _jwtService;
private IConfiguration _configuration;
private readonly IHubContext<ChatHub> _chatHub;
- public MessageService(IDatabaseConnection settings, IMongoClient mongoClient, IJwtService jwtService, IHttpContextAccessor httpContextAccessor, IConfiguration configuration,IHubContext<ChatHub> chatHub)
+ public MessageService(IDatabaseConnection settings, IMongoClient mongoClient, IUserService userService, IJwtService jwtService, IHttpContextAccessor httpContextAccessor, IConfiguration configuration,IHubContext<ChatHub> chatHub)
{
var database = mongoClient.GetDatabase(settings.DatabaseName);
_messages = database.GetCollection<Message>(settings.MessageCollectionname);
+ _userService = userService;
_jwtService = jwtService;
_httpContext = httpContextAccessor;
_configuration = configuration;
@@ -29,6 +31,9 @@ namespace Api.Services
var senderId = _httpContext.HttpContext.User.FindFirstValue("id").ToString();
if (senderId == null)
return null;
+ var receiverCheck =await _userService.GetSelfUserData(msg.receiverId);
+ if (receiverCheck == null)
+ return null;
var tempMsg = new Message();
tempMsg._id = "";
tempMsg.receiverId = msg.receiverId;
diff --git a/Backend/Api/Api/Services/PostService.cs b/Backend/Api/Api/Services/PostService.cs
index 6d28206..5a285e5 100644
--- a/Backend/Api/Api/Services/PostService.cs
+++ b/Backend/Api/Api/Services/PostService.cs
@@ -4,6 +4,7 @@ using Api.Models;
using MongoDB.Driver;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson;
+using System.Diagnostics;
namespace Api.Services
{
@@ -16,7 +17,7 @@ namespace Api.Services
private readonly ILocationService _locationService;
private readonly IMongoCollection<User> _users;
private readonly IMongoCollection<Location> _locations;
- public PostService(IDatabaseConnection settings, IMongoClient mongoClient, IHttpContextAccessor httpContext, IFileService fileService,ILocationService locationService)
+ public PostService(IDatabaseConnection settings, IMongoClient mongoClient, IHttpContextAccessor httpContext, IFileService fileService, ILocationService locationService)
{
var database = mongoClient.GetDatabase(settings.DatabaseName);
_posts = database.GetCollection<Post>(settings.PostCollectionName);
@@ -43,7 +44,7 @@ namespace Api.Services
p.createdAt = DateTime.Now.ToUniversalTime();
List<String> tags;
if (post.tags != "none")
- tags = post.tags.Remove(post.tags.Length-1,1).Split("|").ToList();
+ 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);
@@ -52,28 +53,28 @@ namespace Api.Services
Directory.CreateDirectory(folderPath);
}
- foreach (var image in post.images)
+ foreach (var image in post.images)
{
var filename = image.FileName;
- var ext=Path.GetExtension(filename).ToLowerInvariant();
+ var ext = Path.GetExtension(filename).ToLowerInvariant();
var name = Path.GetFileNameWithoutExtension(filename).ToLowerInvariant();
- var fullPath=Path.Combine(folderPath, name);
+ var fullPath = Path.Combine(folderPath, name);
int i = 0;
while (System.IO.File.Exists(fullPath))
{
i++;
- fullPath=Path.Combine(folderPath, name+i.ToString()+ext);
+ fullPath = Path.Combine(folderPath, name + i.ToString() + ext);
}
- using(var stream=new FileStream(fullPath, FileMode.Create))
+ using (var stream = new FileStream(fullPath, FileMode.Create))
{
await image.CopyToAsync(stream);
}
var f = new Models.File();
f.path = fullPath;
f._id = "";
- f=await _fileService.add(f);
+ f = await _fileService.add(f);
p.images.Add(f);
-
+
}
await _posts.InsertOneAsync(p);
return await postToPostSend(p);
@@ -107,15 +108,17 @@ namespace Api.Services
return p;
}
- public async Task<Boolean> deletePost(string postid,string userid)
+ public async Task<Boolean> deletePost(string postid, string userid)
{
var p = await _posts.Find(x => x._id == postid).FirstOrDefaultAsync();
- if (p == null || p.ownerId != userid)
+ var u = await _users.Find(x => x._id == userid).FirstOrDefaultAsync();
+ if (p == null || p.ownerId != userid ||u==null)
return false;
foreach (var image in p.images)
System.IO.File.Delete(image.path);
-
- await _posts.DeleteOneAsync(postid);
+ if (u.favourites != null)
+ u.favourites.Remove(postid);
+ await _posts.FindOneAndDeleteAsync(x => x._id == postid);
return true;
}
@@ -130,12 +133,12 @@ namespace Api.Services
return temp;
}
- public async Task<PostSend> getPostById(string id,string userid)
+ public async Task<PostSend> getPostById(string id, string userid)
{
Post p = await _posts.Find(post => post._id == id).FirstOrDefaultAsync();
if (p != null)
{
- if (!p.views.Any(x => x.Split("|")[0] == userid))
+ if (!p.views.Any(x => x.Split("|")[0] == userid))
{
p.views.Add(userid + "|" + DateTime.Now.ToUniversalTime().ToString());
await _posts.ReplaceOneAsync(x => x._id == id, p);
@@ -150,8 +153,8 @@ namespace Api.Services
}
return await postToPostSend(p);
}
-
- public async Task<RatingSend> AddOrReplaceRating(RatingReceive rating,string userid) //0 return existing flag , -1 rating failed flag
+
+ 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)
@@ -162,13 +165,17 @@ namespace Api.Services
tosend.ratingscount = ps.ratingscount;
if (p.ownerId == userid)
- return null;
- if(rating.rating == 0)// ako nema rating staviti 0
+ {
+ tosend.myrating = -1;
+ return tosend;
+ }
+
+ if (rating.rating == 0)// ako nema rating staviti 0
{
var r = p.ratings.Find(x => x.userId == userid);
- if(r != null)
+ if (r != null)
{
- tosend.myrating=r.rating;
+ tosend.myrating = r.rating;
return tosend;
}
else
@@ -177,16 +184,16 @@ namespace Api.Services
return tosend;
}
}
- if(rating.rating<1 || rating.rating>5)
+ if (rating.rating < 1 || rating.rating > 5)
return null;
- if(!p.ratings.Any(x => x.userId == userid))
+ if (!p.ratings.Any(x => x.userId == userid))
{
Rating r = new Rating();
r.rating = rating.rating;
r.userId = userid;
p.ratings.Add(r);
await _posts.ReplaceOneAsync(x => x._id == p._id, p);
- tosend.myrating=rating.rating;
+ tosend.myrating = rating.rating;
}
else
{
@@ -220,13 +227,13 @@ namespace Api.Services
}
return false;
}
- public async Task<CommentSend> AddComment(CommentReceive cmnt,string userid,string postid)
+ public async Task<CommentSend> AddComment(CommentReceive cmnt, string userid, string postid)
{
Post p = await _posts.Find(post => post._id == postid).FirstOrDefaultAsync();
if (p != null)
{
Comment c = new Comment();
- CommentSend c1= new CommentSend();
+ CommentSend c1 = new CommentSend();
c.parentId = cmnt.parentId;
c1.parentId = cmnt.parentId;
c.userId = userid;
@@ -255,10 +262,10 @@ namespace Api.Services
{
List<Comment> lista = new List<Comment>();
lista = p.comments.FindAll(x => x.parentId == null || x.parentId == "");
- if (lista.Count() > 0)
+ if (lista.Count() > 0)
{
List<CommentSend> tosend = new List<CommentSend>();
- foreach(var comment in lista)
+ foreach (var comment in lista)
{
CommentSend c = new CommentSend();
c.userId = comment.userId;
@@ -281,11 +288,11 @@ namespace Api.Services
}
return null;
}
- public async Task<List<CommentSend>> CascadeComments(string parentid,Post p)
+ public async Task<List<CommentSend>> CascadeComments(string parentid, Post p)
{
List<Comment> lista = new List<Comment>();
lista = p.comments.FindAll(x => x.parentId == parentid);
- if (lista.Count()>0)
+ if (lista.Count() > 0)
{
List<CommentSend> replies = new List<CommentSend>();
foreach (var comment in lista)
@@ -297,7 +304,7 @@ namespace Api.Services
c.comment = comment.comment;
c.timestamp = comment.timestamp;
- var user= await _users.Find(x => x._id == comment.userId).FirstOrDefaultAsync();
+ var user = await _users.Find(x => x._id == comment.userId).FirstOrDefaultAsync();
if (user != null)
c.username = user.username;
else c.username = "Deleted user";
@@ -310,7 +317,7 @@ namespace Api.Services
}
return null;
}
- public async Task<Boolean> DeleteComments(string postid,string cmntid,string userid)
+ public async Task<Boolean> DeleteComments(string postid, string cmntid, string userid)
{
Post p = await _posts.Find(post => post._id == postid).FirstOrDefaultAsync();
if (p != null)
@@ -325,23 +332,23 @@ namespace Api.Services
return true;
}
}
- return false;
+ return false;
}
- public async Task CascadeDeleteComments(string cmntid,Post p)
+ public async Task CascadeDeleteComments(string cmntid, Post p)
{
List<Comment> lista = new List<Comment>();
lista = p.comments.FindAll(x => x.parentId == cmntid);
if (lista.Count() > 0)
{
- foreach (var comment in lista)
+ foreach (var comment in lista)
{
p.comments.Remove(comment);
await _posts.ReplaceOneAsync(x => x._id == p._id, p);
await CascadeDeleteComments(comment._id, p);
- }
+ }
}
}
- public async Task<PostSendPage> SearchPosts(string locid,int page = 0,int sorttype = 1 ,int filterdate = 1) // for now sorting by number of ratings , not avg rating
+ public async Task<PostSendPage> SearchPosts(string locid,bool filter, int page = 0, int sorttype = 1, int filterdate = 1, int ratingFrom = -1, int ratingTo = -1, int viewsFrom = -1, int viewsTo = -1)
{
var days = DateEnumToDays(filterdate);
var tosend = new PostSendPage();
@@ -349,25 +356,29 @@ namespace Api.Services
var lista = new List<Post>();
var ls = new List<PostSend>();
var xd = new List<PostSend>();
- if(ObjectId.TryParse(locid, out _))
+ if (ObjectId.TryParse(locid, out _))
lista = await _posts.Find(x => x.locationId == locid).ToListAsync();
else
{
- lista = await _posts.Find(x => x.tags != null && x.tags.Any(y => y.ToLower().Contains(locid.ToLower()))).ToListAsync();
- if (lista.Count==0)
+ lista = await _posts.Find(x => x.tags != null && x.tags.Any(y => y.ToLower().Contains(locid.ToLower()))).ToListAsync();
+ if (lista.Count == 0)
{
var locs = await _locations.Find(x => x.city.ToLower().Contains(locid.ToLower()) || x.name.ToLower().Contains(locid.ToLower())).ToListAsync();
- foreach(var loc in locs)
+ foreach (var loc in locs)
{
- var posts =await _posts.Find(x => x.locationId == loc._id).ToListAsync();
- if(posts != null)
+ var posts = await _posts.Find(x => x.locationId == loc._id).ToListAsync();
+ if (posts != null)
{
- foreach(var p in posts)
+ foreach (var p in posts)
{
lista.Add(p);
}
}
}
+ if(lista.Count==0 && locid=="-1")
+ {
+ lista = await _posts.Find(_ => true).ToListAsync();
+ }
}
}
if (lista != null)
@@ -377,7 +388,27 @@ namespace Api.Services
if ((DateTime.Now - elem.createdAt).TotalDays < days)
ls.Add(await postToPostSend(elem));
}
-
+
+ }
+ if (filter)
+ {
+ if (ratingFrom >= 0)
+ {
+ ls = ls.FindAll(post => Math.Floor(post.ratings) >= ratingFrom).ToList();
+ }
+ if (ratingTo >= 0)
+ {
+ ls= ls.FindAll(post => Math.Ceiling(post.ratings) <= ratingTo).ToList();
+ }
+ if (viewsFrom >= 0)
+ {
+ ls = ls.FindAll(post => post.views >=viewsFrom).ToList();
+ }
+ if (viewsTo >= 0)
+ {
+ ls = ls.FindAll(post => post.views <= viewsTo).ToList();
+ }
+
}
switch (sorttype)
{
@@ -390,11 +421,14 @@ namespace Api.Services
case 3:
xd = ls.OrderByDescending(x => x.createdAt).ToList();
break;
+ case 4:
+ xd = ls.OrderBy(x => x.createdAt).ToList();
+ break;
default:
xd = ls.OrderByDescending(x => x.views).ToList();
break;
}
- if(xd != null)
+ if (xd != null)
{
tosend.page = page;
tosend.index = page * 20;
@@ -402,7 +436,7 @@ namespace Api.Services
double pgs = xd.Count / 20;
tosend.totalpages = (int)Math.Ceiling(pgs);
var selected = ls.Skip(20 * page).Take(20);
- foreach(var post in selected)
+ foreach (var post in selected)
{
pslista.Add(post);
}
@@ -412,7 +446,7 @@ namespace Api.Services
}
public int DateEnumToDays(int filterdate)
{
- switch(filterdate)
+ switch (filterdate)
{
case 1: return 365 * 10;
case 2: return 365;
@@ -521,7 +555,7 @@ namespace Api.Services
}
}
var top5tags = tags.OrderByDescending(x => x.counter).Take(5).ToList();
-
+
var all = await _posts.Find(_ => true).ToListAsync();
var recent30 = new List<PostSend>();
var fiveoftop5tags = new List<PostSend>();
@@ -531,7 +565,7 @@ namespace Api.Services
recent30.Add(await postToPostSend(elem));
}
recent30 = recent30.OrderByDescending(x => x.createdAt).ToList();
- foreach (var tag in top5tags)
+ foreach (var tag in top5tags)
{
var five = new List<PostSend>();
foreach (var elem in recent30)
@@ -543,7 +577,7 @@ namespace Api.Services
}
}
five = five.Take(5).ToList();
- foreach(var elem in five)
+ foreach (var elem in five)
{
fiveoftop5tags.Add(elem);
}
@@ -557,22 +591,28 @@ namespace Api.Services
{
string userId = _httpContext.HttpContext.User.FindFirstValue("id");
var result = false;
+ var user = await _users.Find(x => x._id == userId).FirstOrDefaultAsync();
Post post = await _posts.Find(x => x._id == postId).FirstOrDefaultAsync();
- if (userId == null || post==null)
+ if (userId == null || post == null)
return result;
if (post.favourites == null)
post.favourites = new List<string>();
+ if (user.favourites == null)
+ user.favourites = new List<string>();
if (post.favourites.Contains(userId))
{
post.favourites.Remove(userId);
+ user.favourites.Remove(post._id);
result = false;
}
else
{
post.favourites.Add(userId);
+ user.favourites.Add(post._id);
result = true;
}
+ await _users.ReplaceOneAsync(x => x._id == user._id, user);
await _posts.ReplaceOneAsync(x => x._id == postId, post);
return result;
@@ -616,11 +656,11 @@ namespace Api.Services
var top10tags = tags.OrderByDescending(x => x.views).Take(10).ToList();
var tosend = new List<Trending>();
- foreach(var trending in top10tags)
+ foreach (var trending in top10tags)
{
var novi = new Trending();
novi.tagr = trending;
- novi.page = await SearchPosts(trending.tag, 0, 1, 5);
+ novi.page = await SearchPosts(trending.tag, false,0, 1, 5,-1,-1,-1,-1);
tosend.Add(novi);
}
@@ -643,9 +683,9 @@ namespace Api.Services
}
foreach (var elem in inradius)
{
- var locposts = await SearchPosts(elem._id, 0, 1, 1);
+ var locposts = await SearchPosts(elem._id, false,0, 1, 1,-1,-1,-1,-1);
var best = locposts.posts.Take(1).FirstOrDefault();
- if(best != null)
+ if (best != null)
tosend.Add(best);
}
}
@@ -662,18 +702,19 @@ namespace Api.Services
stats.numberOfPosts = 0;
stats.totalViews = 0;
stats.monthlyViews = new List<MonthlyViews>();
+ stats.numberOfFavouritePosts = 0;
+
-
- if(posts != null)
+ if (posts != null)
{
- for(int i = 1; i <= 12; i++)
+ for (int i = 1; i <= 12; i++)
{
var novi = new MonthlyViews();
novi.month = i;
novi.views = 0;
stats.monthlyViews.Add(novi);
}
-
+
foreach (var post in posts)
{
var month = post.createdAt.Month;
@@ -681,13 +722,106 @@ namespace Api.Services
stats.totalViews += post.views;
stats.numberOfRatingsOnPosts += post.ratingscount;
stats.numberOfPosts++;
+ if (post.favourites != null)
+ stats.numberOfFavouritePosts += post.favourites.Count;
ratingsum += post.ratings * post.ratingscount;
}
- if(stats.numberOfRatingsOnPosts > 0) //don't forget to check div by 0 jesus
+ if (stats.numberOfRatingsOnPosts > 0) //don't forget to check div by 0 jesus
stats.averagePostRatingOnPosts = ratingsum / stats.numberOfRatingsOnPosts;
}
return stats;
}
+
+ public async Task<List<PostSend>> userFavouritePosts()
+ {
+ List<PostSend> posts = new List<PostSend>();
+ string userId = _httpContext.HttpContext.User.FindFirstValue("id");
+ var user = await _users.Find(x => x._id == userId).FirstOrDefaultAsync();
+ if (user == null)
+ return null;
+ //Post post = await _posts.Find(x => x._id == postId).FirstOrDefaultAsync();
+ if (user.favourites != null)
+ foreach (var postId in user.favourites)
+ {
+ Post post = await _posts.Find(x => x._id == postId).FirstOrDefaultAsync();
+ posts.Add(await postToPostSend(post));
+
+ }
+ return posts;
+ }
+
+ public async Task<List<PostSend>> GetAllPostsFilterSort(FilterSort fs)
+ {
+ List<PostSend> allPosts = fs.posts;
+ List<PostSend> filteredPosts = allPosts;
+ List<PostSend> fsPosts=allPosts;
+
+ if (fs.filter)
+ {
+ if (fs.filterRatingFrom>=0)
+ {
+ filteredPosts =filteredPosts.FindAll(post => post.ratings >fs.filterRatingFrom);
+ }
+ if (fs.filterRatingTo >= 0)
+ {
+ filteredPosts = filteredPosts.FindAll(post => post.ratings < fs.filterRatingTo);
+ }
+ if(fs.filterViewsFrom >= 0)
+ {
+ filteredPosts = filteredPosts.FindAll(post => post.views > fs.filterViewsFrom);
+ }
+ if(fs.filterViewsTo >= 0)
+ {
+ filteredPosts = filteredPosts.FindAll(post => post.views < fs.filterViewsTo);
+ }
+ if(fs.filterDateFrom!=null)
+ {
+ filteredPosts = filteredPosts.FindAll(post => post.createdAt > fs.filterDateFrom);
+ }
+ if(fs.filterDateTo!=null)
+ {
+ filteredPosts = filteredPosts.FindAll(post => post.createdAt < fs.filterDateTo);
+ }
+ }
+
+ if (fs.sort)
+ {
+ if (fs.sortBest)
+ {
+ fsPosts = filteredPosts.OrderByDescending(o => o.ratings).ToList();
+ }
+ if (fs.sortLatest)
+ {
+ fsPosts = filteredPosts.OrderByDescending(o => o.createdAt).ToList();
+ }
+ if (fs.sortMostViews)
+ {
+ fsPosts = filteredPosts.OrderByDescending(o => o.views).ToList();
+ }
+ if (fs.sortOldest)
+ {
+ fsPosts = filteredPosts.OrderBy(p => p.createdAt).ToList();
+ }
+ }
+
+ if(!fs.filter && !fs.sort)
+ {
+ return allPosts;
+ }
+ else if(!fs.filter && fs.sort)
+ {
+ return fsPosts;
+ }
+ else if(fs.filter && !fs.sort)
+ {
+ return filteredPosts;
+ }
+ else
+ {
+ return filteredPosts;
+ }
+
+
+ }
}
-
}
diff --git a/Backend/Api/Api/Services/UserService.cs b/Backend/Api/Api/Services/UserService.cs
index 781afa8..d8ec4a2 100644
--- a/Backend/Api/Api/Services/UserService.cs
+++ b/Backend/Api/Api/Services/UserService.cs
@@ -17,9 +17,7 @@ namespace Api.Services
private readonly IMongoCollection<Post> _posts;
private readonly IJwtService _jwtService;
private IConfiguration _configuration;
- private readonly IFileService _fileService;
-
- private readonly IMongoCollection<UserSend> _usersSend;
+ private readonly IFileService _fileService;
public UserService(IDatabaseConnection settings, IMongoClient mongoClient, IJwtService jwtService, IHttpContextAccessor httpContextAccessor, IConfiguration configuration, IFileService fileService)
{
var database = mongoClient.GetDatabase(settings.DatabaseName);
@@ -364,6 +362,7 @@ namespace Api.Services
tosend.followingCount = user.followingCount;
tosend.followers = user.followers;
tosend.following = user.following;
+ tosend.favourites = user.favourites;
var userposts = await _posts.Find(x => x.ownerId == user._id).ToListAsync();
tosend.postcount = userposts.Count();
return tosend;
@@ -384,6 +383,7 @@ namespace Api.Services
tosend.followingCount = user.followingCount;
tosend.followers = user.followers;
tosend.following = user.following;
+ tosend.favourites = user.favourites;
var userposts = await _posts.Find(x => x.ownerId == user._id).ToListAsync();
tosend.postcount = userposts.Count();
return tosend;
@@ -416,7 +416,9 @@ namespace Api.Services
}
User f = await _users.Find(user => user._id == followerId).FirstOrDefaultAsync();
User u = await _users.Find(user => user._id == id).FirstOrDefaultAsync();
-
+ if(await CheckIfAlreadyFollow(followerId))
+ return false;
+
if (id != null && followerId!=null)
{
if (f.followers == null)
@@ -476,6 +478,7 @@ namespace Api.Services
follower.followers = utemp.followers;
follower.followersCount = utemp.followersCount;
follower.followingCount = utemp.followingCount;
+ follower.favourites = utemp.favourites;
follower._id = utemp._id;
@@ -515,6 +518,7 @@ namespace Api.Services
follower._id = utemp._id;
follower.followersCount = utemp.followersCount;
follower.followingCount = utemp.followingCount;
+ follower.favourites = utemp.favourites;
following.Add((UserSend)follower);
}
@@ -558,6 +562,7 @@ namespace Api.Services
following._id = utemp._id;
following.followersCount = utemp.followersCount;
following.followingCount = utemp.followingCount;
+ following.favourites=utemp.favourites;
myFollowings.Add((UserSend)following);
}
@@ -614,27 +619,33 @@ namespace Api.Services
User u = await _users.Find(user => user._id == myId).FirstOrDefaultAsync();
User f = await _users.Find(user => user._id == id).FirstOrDefaultAsync();
-
+ if(await CheckIfAlreadyFollow(id))
if (u != null)
{
- if (u.following != null && f.followers!=null)
- {
u.following.Remove(f._id);
+ if (u.following == null)
+ u.following = new List<string>();
u.followingCount=u.following.Count();
+ if (u.followers == null)
+ u.followers = new List<string>();
u.followersCount = u.followers.Count();
f.followers.Remove(u._id);
+ if (f.followers == null)
+ f.followers = new List<string>();
f.followersCount =f.followers.Count();
+ if (f.following == null)
+ f.following = new List<string>();
f.followingCount =f.following.Count();
- _users.ReplaceOne(user => user._id == myId, u);
- _users.ReplaceOne(user => user._id == id, f);
+ await _users.ReplaceOneAsync(user => user._id == myId, u);
+ await _users.ReplaceOneAsync(user => user._id == id, f);
//updateUserFollowerFollowingCount(u.followers, u.following, u._id);
//updateUserFollowerFollowingCount(f.followers, f.following, f._id);
return true;
- }
+
}
return false;
@@ -672,6 +683,7 @@ namespace Api.Services
follower.followersCount = utemp.followersCount;
follower.followingCount = utemp.followingCount;
follower._id = utemp._id;
+ follower.favourites = utemp.favourites;
myfollowers.Add((UserSend)follower);
}
return myfollowers;
@@ -729,6 +741,30 @@ namespace Api.Services
return false;
}
+ public async Task<int> ChangePass(string currentPass,string newPass)
+ {
+
+ string myId = null;
+ if (_httpContext.HttpContext.User.FindFirstValue("id") != null)
+ {
+ myId = _httpContext.HttpContext.User.FindFirstValue("id").ToString();
+ }
+
+ User u = await _users.Find(user => user._id == myId).FirstOrDefaultAsync();
+
+ if (u != null)
+ {
+ if (checkPassword(currentPass, u.password))
+ {
+ u.password = hashPassword(newPass);
+ await _users.ReplaceOneAsync(x => x._id == u._id, u);
+ return 1;
+ }
+ return -1;
+ }
+ return -2;
+ }
+
}