diff options
author | branislav.radivojevic <wafflemynxyt@gmail.com> | 2022-12-05 20:15:57 +0100 |
---|---|---|
committer | branislav.radivojevic <wafflemynxyt@gmail.com> | 2022-12-05 20:15:57 +0100 |
commit | 3b2580391137e284068c04afd0a03c27602f925d (patch) | |
tree | 15607bec141668a78a7e42a6139ea7bbc319b471 /Backend | |
parent | aae690368dc3f60d18c3564cd4c1603820defec1 (diff) |
osnovna statistika korisnika
Diffstat (limited to 'Backend')
-rw-r--r-- | Backend/Api/Api/Controllers/UserController.cs | 23 | ||||
-rw-r--r-- | Backend/Api/Api/Interfaces/IPostService.cs | 1 | ||||
-rw-r--r-- | Backend/Api/Api/Models/User.cs | 15 | ||||
-rw-r--r-- | Backend/Api/Api/Services/PostService.cs | 37 | ||||
-rw-r--r-- | Backend/Api/Api/Services/UserService.cs | 1 |
5 files changed, 77 insertions, 0 deletions
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<ActionResult<UserStats>> 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<ActionResult<UserStats>> 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<Boolean> addRemoveFavourite(string postId); Task<List<Trending>> TrendingTags(); Task<List<PostSend>> BestPostForAllLocationsInRadius(Coords coords, double radius); + Task<UserStats> 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> 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> 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<MonthlyViews>(); + + + 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; } + } } |