using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using SharedData.Models; using WebAPI.Data; namespace WebAPI.Controllers { [Route("api/[controller]")] [ApiController] public class RankingController : ControllerBase { private ApplicationDbContext _context; public RankingController(ApplicationDbContext context) { _context = context; } // Create // POST [HttpPost] public GameResult AddGameResult([FromBody]GameResult gameResult) { _context.GameResults.Add(gameResult); _context.SaveChanges(); return gameResult; } // Read // GET [HttpGet] public List GetGameResults() { List results = _context.GameResults .OrderByDescending(x => x.Score) .ToList(); return results; } [HttpGet("{id}")] public GameResult GetGameResults(int id) { GameResult? result = _context.GameResults .Where(x => x.Id == id) .FirstOrDefault(); return result; } // Update // PUT [HttpPut] public bool UpdateGameResult([FromBody]GameResult gameResult) { var findResult = _context.GameResults .Where(x => x.Id == gameResult.Id) .FirstOrDefault(); if (findResult == null) return false; findResult.UserName = gameResult.UserName; findResult.Score = gameResult.Score; findResult.Date = DateTime.Now; _context.SaveChanges(); return true; } // Delete // DELETE [HttpDelete("{id}")] public bool DeleteGameResults(int id) { var findResult = _context.GameResults .Where(x => x.Id == id) .FirstOrDefault(); if (findResult == null) return false; _context.GameResults.Remove(findResult); _context.SaveChanges(); return true; } } }