From f2e21e0e496a050e9b8b768cdc873668c5fffab4 Mon Sep 17 00:00:00 2001 From: Ognjen Cirkovic Date: Sat, 3 Dec 2022 19:45:50 +0100 Subject: Prosiren model za objave. Omoguceno dodavanje/brisanje favourite u post servisu i post controlleru. --- Backend/Api/Api/Controllers/PostController.cs | 6 ++++++ Backend/Api/Api/Interfaces/IPostService.cs | 1 + Backend/Api/Api/Models/Post.cs | 2 ++ Backend/Api/Api/Services/PostService.cs | 25 +++++++++++++++++++++++++ 4 files changed, 34 insertions(+) (limited to 'Backend') diff --git a/Backend/Api/Api/Controllers/PostController.cs b/Backend/Api/Api/Controllers/PostController.cs index 03c3f81..01a1c64 100644 --- a/Backend/Api/Api/Controllers/PostController.cs +++ b/Backend/Api/Api/Controllers/PostController.cs @@ -186,5 +186,11 @@ namespace Api.Controllers var userid = await _userService.UserIdFromJwt(); return Ok(await _postService.Recommended(userid)); } + [HttpGet("favourite/{id}")] + [Authorize(Roles = "User")] + public async Task> addRemoveFavourite(string id) + { + return Ok(await _postService.addRemoveFavourite(id)); + } } } diff --git a/Backend/Api/Api/Interfaces/IPostService.cs b/Backend/Api/Api/Interfaces/IPostService.cs index 96786bd..d5f489e 100644 --- a/Backend/Api/Api/Interfaces/IPostService.cs +++ b/Backend/Api/Api/Interfaces/IPostService.cs @@ -27,5 +27,6 @@ namespace Api.Interfaces Task> Get10Newest(); Task> Recommended(string userid); + Task addRemoveFavourite(string postId); } } \ No newline at end of file diff --git a/Backend/Api/Api/Models/Post.cs b/Backend/Api/Api/Models/Post.cs index dd007ec..dbe7952 100644 --- a/Backend/Api/Api/Models/Post.cs +++ b/Backend/Api/Api/Models/Post.cs @@ -19,6 +19,7 @@ namespace Api.Models public List comments { get; set; } public List images { get; set; } public List? tags { get; set; } + public List? favorites { get; set; } } public class PostReceive @@ -43,6 +44,7 @@ namespace Api.Models public List images { get; set; } public List? tags { get; set; } public DateTime? lastViewed { get; set; } + public List? favorites { get; set; } } public class Rating { diff --git a/Backend/Api/Api/Services/PostService.cs b/Backend/Api/Api/Services/PostService.cs index 855b231..17c96e3 100644 --- a/Backend/Api/Api/Services/PostService.cs +++ b/Backend/Api/Api/Services/PostService.cs @@ -530,5 +530,30 @@ namespace Api.Services taggedposts = fiveoftop5tags.Distinct().OrderByDescending(x => x.createdAt).ToList(); return taggedposts; } + public async Task addRemoveFavourite(string postId) + { + string userId = _httpContext.HttpContext.User.FindFirstValue("id"); + var result = false; + Post post = await _posts.Find(x => x._id == postId).FirstOrDefaultAsync(); + if (userId == null || post==null) + return result; + if (post.favorites == null) + post.favorites = new List(); + if (post.favorites.Contains(userId)) + { + post.favorites.Remove(userId); + result = false; + } + else + { + post.favorites.Add(userId); + result = true; + + } + await _posts.ReplaceOneAsync(x => x._id == postId, post); + return result; + + } } + } -- cgit v1.2.3 From eb024ae9f03eb9720a8745174eef53246ee02b03 Mon Sep 17 00:00:00 2001 From: Ognjen Cirkovic Date: Sat, 3 Dec 2022 20:17:27 +0100 Subject: Napravljen upit sa clienta ka backu za dodavanje favourite. Omogucen prikaz i dodavanje/brisanje favourite. --- Backend/Api/Api/Models/Post.cs | 4 +- Backend/Api/Api/Services/PostService.cs | 11 +++--- .../Activities/ActivitySinglePost.kt | 43 +++++++++++++++++++++- .../brzodolokacije/Interfaces/IBackendApi.kt | 2 + .../java/com/example/brzodolokacije/Models/Post.kt | 3 +- .../main/res/drawable/ic_baseline_favorite_24.xml | 5 +++ .../drawable/ic_baseline_favorite_border_24.xml | 5 +++ .../src/main/res/layout/activity_single_post.xml | 13 +++++++ 8 files changed, 76 insertions(+), 10 deletions(-) create mode 100644 Client/BrzoDoLokacije/app/src/main/res/drawable/ic_baseline_favorite_24.xml create mode 100644 Client/BrzoDoLokacije/app/src/main/res/drawable/ic_baseline_favorite_border_24.xml (limited to 'Backend') diff --git a/Backend/Api/Api/Models/Post.cs b/Backend/Api/Api/Models/Post.cs index dbe7952..22ed62e 100644 --- a/Backend/Api/Api/Models/Post.cs +++ b/Backend/Api/Api/Models/Post.cs @@ -19,7 +19,7 @@ namespace Api.Models public List comments { get; set; } public List images { get; set; } public List? tags { get; set; } - public List? favorites { get; set; } + public List? favourites { get; set; } } public class PostReceive @@ -44,7 +44,7 @@ namespace Api.Models public List images { get; set; } public List? tags { get; set; } public DateTime? lastViewed { get; set; } - public List? favorites { get; set; } + public List? favourites { get; set; } } public class Rating { diff --git a/Backend/Api/Api/Services/PostService.cs b/Backend/Api/Api/Services/PostService.cs index 17c96e3..fd42d08 100644 --- a/Backend/Api/Api/Services/PostService.cs +++ b/Backend/Api/Api/Services/PostService.cs @@ -89,6 +89,7 @@ namespace Api.Services p.createdAt = post.createdAt; p.tags = post.tags; p.ratingscount = post.ratings.Count(); + p.favourites = post.favourites; if (post.ratings.Count() > 0) { List ratings = new List(); @@ -537,16 +538,16 @@ namespace Api.Services Post post = await _posts.Find(x => x._id == postId).FirstOrDefaultAsync(); if (userId == null || post==null) return result; - if (post.favorites == null) - post.favorites = new List(); - if (post.favorites.Contains(userId)) + if (post.favourites == null) + post.favourites = new List(); + if (post.favourites.Contains(userId)) { - post.favorites.Remove(userId); + post.favourites.Remove(userId); result = false; } else { - post.favorites.Add(userId); + post.favourites.Add(userId); result = true; } diff --git a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ActivitySinglePost.kt b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ActivitySinglePost.kt index f3d8a63..5f99766 100644 --- a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ActivitySinglePost.kt +++ b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ActivitySinglePost.kt @@ -6,12 +6,14 @@ import android.graphics.drawable.ColorDrawable import android.os.Bundle import android.preference.PreferenceManager import android.util.Log +import android.widget.ImageView import android.widget.TextView import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView +import com.auth0.android.jwt.JWT import com.example.brzodolokacije.Adapters.CommentsAdapter import com.example.brzodolokacije.Adapters.PostImageAdapter import com.example.brzodolokacije.Models.* @@ -38,6 +40,7 @@ class ActivitySinglePost : AppCompatActivity() { private var adapterComments: RecyclerView.Adapter? = null private var recyclerViewImages: RecyclerView?=null private var recyclerViewComments: RecyclerView?=null + private var favouriteImage:ImageView?=null public lateinit var post:PostPreview private var comments:MutableList?=mutableListOf() private var starNumber:Number=0 @@ -56,7 +59,7 @@ class ActivitySinglePost : AppCompatActivity() { recyclerViewImages = binding.rvMain buildRecyclerViewComments() requestGetComments() - + favouriteImage=binding.ivFavourite // set recyclerView attributes recyclerViewImages?.setHasFixedSize(true) recyclerViewImages?.layoutManager = layoutManagerImages @@ -64,7 +67,7 @@ class ActivitySinglePost : AppCompatActivity() { loadTextComponents() setRatingListeners() translateOwnerIdToName(post.ownerId) - + loadFavourite() val alreadyrated= RatingReceive(starNumber.toInt(),post._id) requestAddRating(alreadyrated) @@ -78,6 +81,42 @@ class ActivitySinglePost : AppCompatActivity() { getMap() } + favouriteImage!!.setOnClickListener{ + addRemoveFavourite() + } + } + fun loadFavourite(){ + if(post.favourites!=null){ + var jwtString=SharedPreferencesHelper.getValue("jwt",this) + var jwt: JWT = JWT(jwtString!!) + var userId=jwt.getClaim("id").asString() + if(post.favourites!!.contains(userId)) + { + favouriteImage!!.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_baseline_favorite_24)) + }else{ + favouriteImage!!.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_baseline_favorite_border_24)); + + } + + } + } + fun addRemoveFavourite(){ + var token= SharedPreferencesHelper.getValue("jwt", this).toString() + val Api= RetrofitHelper.getInstance() + val request=Api.addRemoveFavourite("Bearer "+token,post._id) + request.enqueue(object : retrofit2.Callback { + override fun onResponse(call: Call, response: Response) { + if(response.isSuccessful && response.body() == true) + favouriteImage!!.setImageDrawable(ContextCompat.getDrawable(this@ActivitySinglePost, R.drawable.ic_baseline_favorite_24)) + else + favouriteImage!!.setImageDrawable(ContextCompat.getDrawable(this@ActivitySinglePost, R.drawable.ic_baseline_favorite_border_24)); + } + + override fun onFailure(call: Call, t: Throwable) { + + } + }) + } fun getMap(){ val mapDialogue = BottomSheetDialog(this@ActivitySinglePost, android.R.style.Theme_Black_NoTitleBar) diff --git a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Interfaces/IBackendApi.kt b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Interfaces/IBackendApi.kt index 676023f..60f243d 100644 --- a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Interfaces/IBackendApi.kt +++ b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Interfaces/IBackendApi.kt @@ -109,5 +109,7 @@ interface IBackendApi { @GET("/api/user/{id}/myFollowers") fun getMyFollowers(@Header("Authorization") authHeader:String):Call > + @GET("/api/Post/favourite/{id}") + fun addRemoveFavourite(@Header("Authorization") authHeader:String,@Path("id") id:String):Call } \ No newline at end of file diff --git a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Models/Post.kt b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Models/Post.kt index f0f67a7..8f07bca 100644 --- a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Models/Post.kt +++ b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Models/Post.kt @@ -39,7 +39,8 @@ data class PostPreview ( var ratingscount:Int, var createdAt:Date, var lastViewed: Date?, //samo za istoriju pregleda - var tags:List? + var tags:List?, + var favourites:List? //nedostaju datum i vreme kreiranja diff --git a/Client/BrzoDoLokacije/app/src/main/res/drawable/ic_baseline_favorite_24.xml b/Client/BrzoDoLokacije/app/src/main/res/drawable/ic_baseline_favorite_24.xml new file mode 100644 index 0000000..84df34b --- /dev/null +++ b/Client/BrzoDoLokacije/app/src/main/res/drawable/ic_baseline_favorite_24.xml @@ -0,0 +1,5 @@ + + + diff --git a/Client/BrzoDoLokacije/app/src/main/res/drawable/ic_baseline_favorite_border_24.xml b/Client/BrzoDoLokacije/app/src/main/res/drawable/ic_baseline_favorite_border_24.xml new file mode 100644 index 0000000..4c07189 --- /dev/null +++ b/Client/BrzoDoLokacije/app/src/main/res/drawable/ic_baseline_favorite_border_24.xml @@ -0,0 +1,5 @@ + + + diff --git a/Client/BrzoDoLokacije/app/src/main/res/layout/activity_single_post.xml b/Client/BrzoDoLokacije/app/src/main/res/layout/activity_single_post.xml index e3cabb5..e07345b 100644 --- a/Client/BrzoDoLokacije/app/src/main/res/layout/activity_single_post.xml +++ b/Client/BrzoDoLokacije/app/src/main/res/layout/activity_single_post.xml @@ -76,6 +76,19 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/tvLocationParent" /> + + Date: Mon, 5 Dec 2022 16:45:48 +0100 Subject: pretraga po tagovima i imenu lokacije, trending --- Backend/Api/Api/Controllers/PostController.cs | 7 +++ Backend/Api/Api/Interfaces/IPostService.cs | 1 + Backend/Api/Api/Models/Post.cs | 7 +++ Backend/Api/Api/Services/PostService.cs | 75 ++++++++++++++++++++++++++- 4 files changed, 88 insertions(+), 2 deletions(-) (limited to 'Backend') diff --git a/Backend/Api/Api/Controllers/PostController.cs b/Backend/Api/Api/Controllers/PostController.cs index 01a1c64..61a4f48 100644 --- a/Backend/Api/Api/Controllers/PostController.cs +++ b/Backend/Api/Api/Controllers/PostController.cs @@ -192,5 +192,12 @@ namespace Api.Controllers { return Ok(await _postService.addRemoveFavourite(id)); } + + [HttpGet("trending")] + [Authorize(Roles = "User")] + public async Task>> Trending() + { + return Ok(await _postService.TrendingTags()); + } } } diff --git a/Backend/Api/Api/Interfaces/IPostService.cs b/Backend/Api/Api/Interfaces/IPostService.cs index d5f489e..69846b0 100644 --- a/Backend/Api/Api/Interfaces/IPostService.cs +++ b/Backend/Api/Api/Interfaces/IPostService.cs @@ -28,5 +28,6 @@ namespace Api.Interfaces Task> Get10Newest(); Task> Recommended(string userid); Task addRemoveFavourite(string postId); + Task> TrendingTags(); } } \ No newline at end of file diff --git a/Backend/Api/Api/Models/Post.cs b/Backend/Api/Api/Models/Post.cs index 22ed62e..9c0c429 100644 --- a/Backend/Api/Api/Models/Post.cs +++ b/Backend/Api/Api/Models/Post.cs @@ -115,5 +115,12 @@ namespace Api.Models { public int counter { get; set; } public string tag { get; set; } + public int? views { get; set; } + } + + public class Trending + { + public TagR tagr { get; set; } + public PostSendPage page { get; set; } } } diff --git a/Backend/Api/Api/Services/PostService.cs b/Backend/Api/Api/Services/PostService.cs index fd42d08..777389b 100644 --- a/Backend/Api/Api/Services/PostService.cs +++ b/Backend/Api/Api/Services/PostService.cs @@ -15,6 +15,7 @@ namespace Api.Services private readonly IFileService _fileService; private readonly ILocationService _locationService; private readonly IMongoCollection _users; + private readonly IMongoCollection _locations; public PostService(IDatabaseConnection settings, IMongoClient mongoClient, IHttpContextAccessor httpContext, IFileService fileService,ILocationService locationService) { var database = mongoClient.GetDatabase(settings.DatabaseName); @@ -23,6 +24,7 @@ namespace Api.Services _httpContext = httpContext; _fileService = fileService; _locationService = locationService; + _locations = database.GetCollection(settings.LocationCollectionName); } public async Task addPost(PostReceive post) @@ -347,7 +349,27 @@ namespace Api.Services var lista = new List(); var ls = new List(); var xd = new List(); - lista = await _posts.Find(x => x.locationId == locid).ToListAsync(); + 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) + { + var locs = await _locations.Find(x => x.city.ToLower().Contains(locid.ToLower()) || x.name.ToLower().Contains(locid.ToLower())).ToListAsync(); + foreach(var loc in locs) + { + var posts =await _posts.Find(x => x.locationId == loc._id).ToListAsync(); + if(posts != null) + { + foreach(var p in posts) + { + lista.Add(p); + } + } + } + } + } if (lista != null) { foreach (var elem in lista) @@ -472,7 +494,7 @@ namespace Api.Services public async Task> Recommended(string userid) // momgodb bloat bleh { List posts = await UserHistory(userid); - //TODO-LIMIT RECOMMENDED FOR POSTS FROM THIS MONTH ONLY + //TODO-LIMIT RECOMMENDED FOR POSTS FROM THIS MONTH List tags = new List(); foreach (var post in posts) { @@ -555,6 +577,55 @@ namespace Api.Services return result; } + + public async Task> TrendingTags() + { + var all = await _posts.Find(_ => true).ToListAsync(); + var posts = new List(); + foreach (var elem in all) + { + if ((DateTime.Now - elem.createdAt).TotalDays < 7) + posts.Add(await postToPostSend(elem)); + } + List tags = new List(); + foreach (var post in posts) + { + if (post.tags != null) + { + foreach (var tagitem in post.tags) + { + if (!tags.Any(x => x.tag == tagitem)) + { + var newtag = new TagR(); + newtag.tag = tagitem; + newtag.counter = 1; + newtag.views = post.views; + tags.Add(newtag); + } + else + { + var replace = tags.Find(x => x.tag == tagitem); + tags.Remove(replace); + replace.counter += 1; + replace.views += post.views; + tags.Add(replace); + } + } + } + } + var top10tags = tags.OrderByDescending(x => x.views).Take(10).ToList(); + + var tosend = new List(); + foreach(var trending in top10tags) + { + var novi = new Trending(); + novi.tagr = trending; + novi.page = await SearchPosts(trending.tag, 0, 1, 5); + tosend.Add(novi); + } + + return tosend; + } } } -- cgit v1.2.3 From aae690368dc3f60d18c3564cd4c1603820defec1 Mon Sep 17 00:00:00 2001 From: "branislav.radivojevic" Date: Mon, 5 Dec 2022 17:10:49 +0100 Subject: Najpopularniji post za svaku lokaciju u radiusu --- Backend/Api/Api/Controllers/LocationController.cs | 22 +++++++++++++++++++- Backend/Api/Api/Interfaces/IPostService.cs | 1 + Backend/Api/Api/Services/LocationService.cs | 2 ++ Backend/Api/Api/Services/PostService.cs | 25 +++++++++++++++++++++++ 4 files changed, 49 insertions(+), 1 deletion(-) (limited to 'Backend') diff --git a/Backend/Api/Api/Controllers/LocationController.cs b/Backend/Api/Api/Controllers/LocationController.cs index ba9c9a8..ac093bb 100644 --- a/Backend/Api/Api/Controllers/LocationController.cs +++ b/Backend/Api/Api/Controllers/LocationController.cs @@ -12,9 +12,11 @@ namespace Api.Controllers public class LocationController : ControllerBase { private readonly ILocationService _locationService; - public LocationController(ILocationService locationService) + private readonly IPostService _postService; + public LocationController(ILocationService locationService, IPostService postService) { _locationService = locationService; + _postService = postService; } [HttpPost("add")] @@ -74,5 +76,23 @@ namespace Api.Controllers return Ok(ret); } } + + [HttpGet("searchradius")] + [Authorize(Roles = "User")] + public async Task>> bestPostsForLocationInRadius(double latitude, double longitude, double radius) + { + Coords coords = new Coords(); + if (latitude != null && longitude != null) + { + coords.latitude = (double)latitude; + coords.longitude = (double)longitude; + } + List ret = new List(); + ret = await _postService.BestPostForAllLocationsInRadius(coords, radius); + if (ret != null && ret.Count > 0) + return Ok(ret); + return BadRequest(); + + } } } diff --git a/Backend/Api/Api/Interfaces/IPostService.cs b/Backend/Api/Api/Interfaces/IPostService.cs index 69846b0..84d32ef 100644 --- a/Backend/Api/Api/Interfaces/IPostService.cs +++ b/Backend/Api/Api/Interfaces/IPostService.cs @@ -29,5 +29,6 @@ namespace Api.Interfaces Task> Recommended(string userid); Task addRemoveFavourite(string postId); Task> TrendingTags(); + Task> BestPostForAllLocationsInRadius(Coords coords, double radius); } } \ No newline at end of file diff --git a/Backend/Api/Api/Services/LocationService.cs b/Backend/Api/Api/Services/LocationService.cs index afb3b5b..55a52e7 100644 --- a/Backend/Api/Api/Services/LocationService.cs +++ b/Backend/Api/Api/Services/LocationService.cs @@ -99,5 +99,7 @@ namespace Api.Services } return tosend; } + + } } diff --git a/Backend/Api/Api/Services/PostService.cs b/Backend/Api/Api/Services/PostService.cs index 777389b..6f7c455 100644 --- a/Backend/Api/Api/Services/PostService.cs +++ b/Backend/Api/Api/Services/PostService.cs @@ -626,6 +626,31 @@ namespace Api.Services return tosend; } + + public async Task> BestPostForAllLocationsInRadius(Coords coords, double radius) + { + if (coords == null) + return null; + var lista = await _locations.Find(_ => true).ToListAsync(); + var inradius = new List(); + var tosend = new List(); + if (lista != null) + { + foreach (var elem in lista) + { + if (Math.Abs(elem.latitude - coords.latitude) < radius && Math.Abs(elem.longitude - coords.longitude) < radius) + inradius.Add(elem); + } + foreach (var elem in inradius) + { + var locposts = await SearchPosts(elem._id, 0, 1, 1); + var best = locposts.posts.Take(1).FirstOrDefault(); + if(best != null) + tosend.Add(best); + } + } + return tosend; + } } } -- cgit v1.2.3 From 3b2580391137e284068c04afd0a03c27602f925d Mon Sep 17 00:00:00 2001 From: "branislav.radivojevic" Date: Mon, 5 Dec 2022 20:15:57 +0100 Subject: osnovna statistika korisnika --- Backend/Api/Api/Controllers/UserController.cs | 23 +++++++++++++++++ Backend/Api/Api/Interfaces/IPostService.cs | 1 + Backend/Api/Api/Models/User.cs | 15 +++++++++++ Backend/Api/Api/Services/PostService.cs | 37 +++++++++++++++++++++++++++ Backend/Api/Api/Services/UserService.cs | 1 + 5 files changed, 77 insertions(+) (limited to 'Backend') diff --git a/Backend/Api/Api/Controllers/UserController.cs b/Backend/Api/Api/Controllers/UserController.cs index 4d7905a..97f2f8b 100644 --- a/Backend/Api/Api/Controllers/UserController.cs +++ b/Backend/Api/Api/Controllers/UserController.cs @@ -123,5 +123,28 @@ namespace Api.Controllers { return Ok(await _userService.GetMyFollowers()); } + + [HttpGet("profile/stats")] + [Authorize(Roles = "User")] + public async Task> SelfStats() + { + var id = await _userService.UserIdFromJwt(); + var tosend = await _postService.UserStats(id); + if (tosend != null) + return Ok(tosend); + return BadRequest(); + } + [HttpGet("{username}/profile/stats")] + [Authorize(Roles = "User")] + public async Task> GetStats(string username) + { + var rez = await _userService.GetUserData(username); + if (rez == null) + return BadRequest(); + var tosend = await _postService.UserStats(rez._id); + if (tosend != null) + return Ok(tosend); + return BadRequest(); + } } } diff --git a/Backend/Api/Api/Interfaces/IPostService.cs b/Backend/Api/Api/Interfaces/IPostService.cs index 84d32ef..c854601 100644 --- a/Backend/Api/Api/Interfaces/IPostService.cs +++ b/Backend/Api/Api/Interfaces/IPostService.cs @@ -30,5 +30,6 @@ namespace Api.Interfaces Task addRemoveFavourite(string postId); Task> TrendingTags(); Task> BestPostForAllLocationsInRadius(Coords coords, double radius); + Task UserStats(string userid); } } \ No newline at end of file diff --git a/Backend/Api/Api/Models/User.cs b/Backend/Api/Api/Models/User.cs index 6c777e7..cf16dbe 100644 --- a/Backend/Api/Api/Models/User.cs +++ b/Backend/Api/Api/Models/User.cs @@ -72,4 +72,19 @@ namespace Api.Models } + + public class UserStats + { + public int totalViews { get; set; } + public int numberOfPosts { get; set; } + public int numberOfRatingsOnPosts { get; set; } + public double averagePostRatingOnPosts {get; set; } + public List monthlyViews { get; set; } + } + + public class MonthlyViews + { + public int month { get; set; } + public int views { get; set; } + } } diff --git a/Backend/Api/Api/Services/PostService.cs b/Backend/Api/Api/Services/PostService.cs index 6f7c455..6d28206 100644 --- a/Backend/Api/Api/Services/PostService.cs +++ b/Backend/Api/Api/Services/PostService.cs @@ -651,6 +651,43 @@ namespace Api.Services } return tosend; } + + public async Task UserStats(string userid) + { + var posts = await GetUsersPosts(userid); + var stats = new UserStats(); + double ratingsum = 0; + stats.averagePostRatingOnPosts = 0; + stats.numberOfRatingsOnPosts = 0; + stats.numberOfPosts = 0; + stats.totalViews = 0; + stats.monthlyViews = new List(); + + + if(posts != null) + { + 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; + stats.monthlyViews.FirstOrDefault(x => x.month == month).views += post.views; + stats.totalViews += post.views; + stats.numberOfRatingsOnPosts += post.ratingscount; + stats.numberOfPosts++; + ratingsum += post.ratings * post.ratingscount; + } + if(stats.numberOfRatingsOnPosts > 0) //don't forget to check div by 0 jesus + stats.averagePostRatingOnPosts = ratingsum / stats.numberOfRatingsOnPosts; + } + return stats; + } } } diff --git a/Backend/Api/Api/Services/UserService.cs b/Backend/Api/Api/Services/UserService.cs index ec67729..666dd98 100644 --- a/Backend/Api/Api/Services/UserService.cs +++ b/Backend/Api/Api/Services/UserService.cs @@ -663,6 +663,7 @@ namespace Api.Services return null; } + } } -- cgit v1.2.3