52 lines
1.7 KiB
C#
52 lines
1.7 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging;
|
|
using TOOHUCardAPI.Data.Enums;
|
|
using TOOHUCardAPI.Data.Models;
|
|
|
|
namespace TOOHUCardAPI.Data.Repositories
|
|
{
|
|
public class RankRepository
|
|
{
|
|
private readonly AppDbContext _appDbContext;
|
|
private readonly ILogger<RankRepository> _logger;
|
|
|
|
public RankRepository(AppDbContext appDbContext, ILogger<RankRepository> logger)
|
|
{
|
|
_appDbContext = appDbContext;
|
|
_logger = logger;
|
|
}
|
|
|
|
private IQueryable<RankEntry> AllIncluded()
|
|
{
|
|
return _appDbContext.RankEntries.Include(i => i.TowersUsed).Include(i => i.User);
|
|
}
|
|
|
|
public async Task<IEnumerable<RankEntry>> GetRankEntries(RankType rankType)
|
|
{
|
|
return await AllIncluded().Where(i => i.RankType == rankType).OrderByDescending(i => i.Wave).ThenByDescending(i => i.Damage).ToListAsync();
|
|
}
|
|
|
|
public async Task<RankEntry> GetRankEntry(long steamid, RankType rankType)
|
|
{
|
|
return await AllIncluded().Where(i => i.User.SteamId == steamid && i.RankType == rankType).OrderByDescending(i => i.Wave).FirstOrDefaultAsync();
|
|
}
|
|
|
|
public async Task<RankEntry> InsertRankEntry(RankEntry entry)
|
|
{
|
|
var newEntry = await _appDbContext.RankEntries.AddAsync(entry);
|
|
await _appDbContext.SaveChangesAsync();
|
|
return newEntry.Entity;
|
|
}
|
|
|
|
public async Task DeleteRankEntry(RankEntry entry)
|
|
{
|
|
entry.TowersUsed.Clear();
|
|
_appDbContext.RankEntries.Remove(entry);
|
|
await _appDbContext.SaveChangesAsync();
|
|
}
|
|
}
|
|
} |