You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.2 KiB

2 years ago
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
2 years ago
[HttpPost]
public GameResult AddGameResult([FromBody]GameResult gameResult)
{
_context.GameResults.Add(gameResult);
_context.SaveChanges();
return gameResult;
}
2 years ago
// Read
// GET
[HttpGet]
public List<GameResult> GetGameResults()
{
List<GameResult> results = _context.GameResults
.OrderByDescending(x => x.Score)
.ToList();
return results;
}
[HttpGet("{id}")]
public GameResult GetGameResults(int id)
{
2 years ago
GameResult? result = _context.GameResults
2 years ago
.Where(x => x.Id == id)
.FirstOrDefault();
return result;
}
// Update
// PUT
2 years ago
[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;
}
2 years ago
// Delete
// DELETE
2 years ago
[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;
}
2 years ago
}
}