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 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -157,7 +156,7 @@
-
+
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