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.
 
 
 
 
 

65 lines
1.8 KiB

using RankingApp.Data.Models;
namespace RankingApp.Data.Services
{
public class RankingService
{
private ApplicationDbContext _context;
public RankingService(ApplicationDbContext context)
{
_context = context;
}
// Create
public Task<GameResult> AddGameResult(GameResult gameResult)
{
_context.GameResults.Add(gameResult);
_context.SaveChanges();
return Task.FromResult(gameResult);
}
// Read
public Task<List<GameResult>> GetGameResultsAsync()
{
List<GameResult> results = _context.GameResults
.OrderByDescending(item => item.Score)
.ToList();
return Task.FromResult(results);
}
// Update
public Task<bool> UpdateGameResult(GameResult gameResult)
{
var findResult = _context.GameResults
.Where(x => x.Id == gameResult.Id)
.FirstOrDefault();
if (findResult == null)
return Task.FromResult(false);
findResult.UserName = gameResult.UserName;
findResult.Score = gameResult.Score;
findResult.Date = DateTime.Now;
_context.SaveChanges();
return Task.FromResult(true);
}
// Delete
public Task<bool> DeleteGameResult(GameResult gameResult)
{
var findResult = _context.GameResults
.Where(x => x.Id == gameResult.Id)
.FirstOrDefault();
if (findResult == null)
return Task.FromResult(false);
_context.GameResults.Remove(findResult);
_context.SaveChanges();
return Task.FromResult(true);
}
}
}