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(+) 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