From 250a192e53b255fbbbed1b577659e1ee035dbde9 Mon Sep 17 00:00:00 2001 From: Gintaras Date: Tue, 5 Nov 2024 16:39:50 +0200 Subject: [PATCH] added leaderboard controller and for adding/updating the score in the leaderboardendpoints for retrieving the top 10 and for --- .../.idea.SwipeWords.dir/.idea/workspace.xml | 33 ++++----- .../Controllers/LeaderboardController.cs | 73 +++++++++++++++++++ SwipeWords/Data/Leaderboard.cs | 10 +-- 3 files changed, 93 insertions(+), 23 deletions(-) create mode 100644 SwipeWords/Controllers/LeaderboardController.cs diff --git a/SwipeWords/.idea/.idea.SwipeWords.dir/.idea/workspace.xml b/SwipeWords/.idea/.idea.SwipeWords.dir/.idea/workspace.xml index 94a40ea..89d084b 100644 --- a/SwipeWords/.idea/.idea.SwipeWords.dir/.idea/workspace.xml +++ b/SwipeWords/.idea/.idea.SwipeWords.dir/.idea/workspace.xml @@ -10,22 +10,21 @@ - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + diff --git a/SwipeWords/Controllers/LeaderboardController.cs b/SwipeWords/Controllers/LeaderboardController.cs new file mode 100644 index 0000000..438a223 --- /dev/null +++ b/SwipeWords/Controllers/LeaderboardController.cs @@ -0,0 +1,73 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using SwipeWords.Data; +using System; +using System.Linq; +using System.Threading.Tasks; + +namespace SwipeWords.Controllers +{ + [ApiController] + [Route("api/[controller]")] + public class LeaderboardController : ControllerBase + { + private readonly UsersDatabaseContext _context; + + public LeaderboardController(UsersDatabaseContext context) + { + _context = context; + } + + [HttpPost("AddOrUpdateScore")] + public async Task AddOrUpdateScore(string userName, int score) + { + var leaderboardEntry = await _context.Leaderboards + .FirstOrDefaultAsync(lb => lb.UserName == userName); + + if (leaderboardEntry != null) + { + if (score > leaderboardEntry.MaxScore) + { + leaderboardEntry.MaxScore = score; + await _context.SaveChangesAsync(); + } + } + else + { + var user = await _context.Users.FirstOrDefaultAsync(u => u.Name == userName); + if (user == null) + { + return NotFound(new { message = "User not found." }); + } + + leaderboardEntry = new Leaderboard + { + UserName = userName, + UserId = user.UserId, + MaxScore = score + }; + + _context.Leaderboards.Add(leaderboardEntry); + await _context.SaveChangesAsync(); + } + + return Ok(new { message = "Score added or updated successfully." }); + } + + [HttpGet("GetLeaderboard")] + public async Task GetLeaderboard(int top = 10) + { + var leaderboard = await _context.Leaderboards + .OrderByDescending(lb => lb.MaxScore) + .Take(top) + .Select(lb => new + { + lb.UserName, + lb.MaxScore + }) + .ToListAsync(); + + return Ok(leaderboard); + } + } +} diff --git a/SwipeWords/Data/Leaderboard.cs b/SwipeWords/Data/Leaderboard.cs index 18c0a7a..e99629a 100644 --- a/SwipeWords/Data/Leaderboard.cs +++ b/SwipeWords/Data/Leaderboard.cs @@ -3,14 +3,12 @@ namespace SwipeWords.Data; -public record Leaderboard +public class Leaderboard { - [Key] public int Id { get; set; } + [Key] public string UserName { get; set; } public int MaxScore { get; set; } - public int RankPosition { get; set; } - [ForeignKey(nameof(UserId))] public int UserId { get; set; } - - public User User { get; set; } + [ForeignKey(nameof(UserId))] public Guid UserId { get; set; } + } \ No newline at end of file