90 lines
2.5 KiB
C#
90 lines
2.5 KiB
C#
using ReserveManagementSystemLib.Enums;
|
|
using ReserveManagementSystemLib.Models;
|
|
using ReserveManagementSystemLib.Repositories;
|
|
|
|
namespace ReserveManagementSystemApi.Services
|
|
{
|
|
public class ImportService: IImportService
|
|
{
|
|
private readonly IImportRepository _importRepository;
|
|
|
|
public ImportService(IImportRepository importRepository)
|
|
{
|
|
_importRepository = importRepository;
|
|
}
|
|
|
|
public async Task<IEnumerable<Import>> GetAllAsync()
|
|
{
|
|
try
|
|
{
|
|
return await _importRepository.GetAll().OrderByDescending(imp => imp.StartDate).ToListAsync();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "Failure retrieving imports.");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<Import> GetLastLoanImportByType(ImportType importType)
|
|
{
|
|
Import import;
|
|
try
|
|
{
|
|
import = await _importRepository.GetLastLoanDnaImportByType(importType).FirstOrDefaultAsync();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, $"Failure retrieving last import.");
|
|
throw;
|
|
}
|
|
|
|
return import;
|
|
}
|
|
|
|
public async Task<IEnumerable<Import>> GetRecentAsync(int numberOfImports)
|
|
{
|
|
try
|
|
{
|
|
return await _importRepository.GetAll().OrderByDescending(imp => imp.StartDate).Take(numberOfImports).ToListAsync();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "Failure retrieving imports.");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<Import> GetByIdAsync(int id)
|
|
{
|
|
Import import;
|
|
try
|
|
{
|
|
import = await _importRepository.FindByIdAsync(id);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, $"Failure retrieving import with id {id}.");
|
|
throw;
|
|
}
|
|
|
|
if (import == null)
|
|
{
|
|
throw new Exception($"Import with id {id} not found.");
|
|
}
|
|
|
|
return import;
|
|
}
|
|
|
|
public async Task<Import> AddImportAsync(Import newImport)
|
|
{
|
|
return await _importRepository.AddImportAsync(newImport);
|
|
}
|
|
|
|
public async Task<Import> UpdateImportAsync(Import import)
|
|
{
|
|
return await _importRepository.UpdateImportAsync(import);
|
|
}
|
|
}
|
|
}
|