85 lines
2.6 KiB
C#
85 lines
2.6 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using PaydayBackend.Models;
|
|
|
|
namespace PaydayBackend.Services;
|
|
|
|
public interface IAdminService
|
|
{
|
|
public Task AddBank(Bank bank);
|
|
public Task<IEnumerable<Bank>> GetAllBanks();
|
|
public Task AddLoanTerm(LoanTerm loanTerm);
|
|
public Task<string> RemoveAllLoanTermsByBankId(long bankId);
|
|
public Task<IEnumerable<LoanTerm>?> GetAllLoanTermsByBankId(long bankId);
|
|
public Task AddUniversity(University university);
|
|
public Task AddUniversityDirection(UniversityDirection universityDirection);
|
|
}
|
|
|
|
public class AdminService : IAdminService
|
|
{
|
|
private DatabaseContext _databaseContext;
|
|
|
|
private async Task<bool> BankIsExist(long bankId)
|
|
{
|
|
if (await _databaseContext.Banks.Where(x => x.Id == bankId).FirstOrDefaultAsync() == null)
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
public AdminService(DatabaseContext databaseContext)
|
|
{
|
|
_databaseContext = databaseContext;
|
|
}
|
|
|
|
// -------------------------------------| Банки |-------------------------------------
|
|
|
|
public async Task AddBank(Bank bank)
|
|
{
|
|
await _databaseContext.Banks.AddAsync(bank);
|
|
await _databaseContext.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<Bank>> GetAllBanks()
|
|
{
|
|
return await _databaseContext.Banks.ToListAsync();
|
|
}
|
|
|
|
public async Task AddLoanTerm(LoanTerm loanTerm)
|
|
{
|
|
await _databaseContext.LoanTerms.AddAsync(loanTerm);
|
|
await _databaseContext.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task<string> RemoveAllLoanTermsByBankId(long bankId)
|
|
{
|
|
if (await BankIsExist(bankId))
|
|
return "Bank not found";
|
|
|
|
var result = await _databaseContext.LoanTerms.Where(x => x.Bank.Id == bankId).ToListAsync();
|
|
_databaseContext.LoanTerms.RemoveRange(result);
|
|
|
|
return "OK";
|
|
}
|
|
|
|
public async Task<IEnumerable<LoanTerm>?> GetAllLoanTermsByBankId(long bankId)
|
|
{
|
|
if (await BankIsExist(bankId))
|
|
return null;
|
|
|
|
return await _databaseContext.LoanTerms.Where(x => x.Bank.Id == bankId).ToListAsync();
|
|
}
|
|
|
|
// -------------------------------------| Университеты |-------------------------------------
|
|
|
|
public async Task AddUniversity(University university)
|
|
{
|
|
await _databaseContext.Universities.AddAsync(university);
|
|
await _databaseContext.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task AddUniversityDirection(UniversityDirection universityDirection)
|
|
{
|
|
await _databaseContext.UniversityDirections.AddAsync(universityDirection);
|
|
await _databaseContext.SaveChangesAsync();
|
|
}
|
|
} |