114 lines
3.4 KiB
C#
114 lines
3.4 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using _0_Framework.Application;
|
|
using AccountManagement.Application.Contracts.Media;
|
|
using AccountManagement.Domain.MediaAgg;
|
|
using Company.Domain.BankAgg;
|
|
using CompanyManagment.App.Contracts.Bank;
|
|
|
|
namespace CompanyManagment.Application
|
|
{
|
|
public class BankApplication : IBankApplication
|
|
{
|
|
private readonly IBankRepository _bankRepository;
|
|
private readonly IMediaApplication _mediaApplication;
|
|
|
|
private const string _basePath = "BankLogos";
|
|
|
|
public BankApplication(IBankRepository bankRepository, IMediaApplication mediaApplication)
|
|
{
|
|
_bankRepository = bankRepository;
|
|
_mediaApplication = mediaApplication;
|
|
}
|
|
|
|
public BankViewModel GetBy(long id)
|
|
{
|
|
var entity = _bankRepository.Get(id);
|
|
if (entity == null)
|
|
return new();
|
|
var media = _mediaApplication.Get(entity.BankLogoMediaId);
|
|
return new BankViewModel()
|
|
{
|
|
Id = entity.id,
|
|
BankName = entity.BankName,
|
|
BankLogoPicturePath = media.Path
|
|
};
|
|
}
|
|
|
|
public OperationResult Remove(long id)
|
|
{
|
|
OperationResult op = new();
|
|
var entity = _bankRepository.Get(id);
|
|
if (entity == null)
|
|
return op.Failed(ApplicationMessages.RecordNotFound);
|
|
_bankRepository.Remove(entity);
|
|
_bankRepository.SaveChanges();
|
|
return op.Succcedded();
|
|
}
|
|
|
|
public OperationResult Create(CreateBank command)
|
|
{
|
|
OperationResult op = new();
|
|
if (string.IsNullOrWhiteSpace(command.BankName))
|
|
return op.Failed("فیلد نام بانک نمی تواند خالی باشد");
|
|
long mediaId = 0;
|
|
if(command.BankLogoPictureFile != null && command.BankLogoPictureFile.Length >0 )
|
|
{
|
|
var uploadResult = _mediaApplication.UploadFile(command.BankLogoPictureFile, command.BankName,
|
|
_basePath, 10, [".jpg", ".jpeg", ".png",".svg"], "Bank");
|
|
if (uploadResult.IsSuccedded == false)
|
|
return uploadResult;
|
|
mediaId = uploadResult.SendId;
|
|
}
|
|
var newEntity = new Bank(command.BankName, mediaId);
|
|
_bankRepository.Create(newEntity);
|
|
_bankRepository.SaveChanges();
|
|
|
|
return op.Succcedded();
|
|
}
|
|
|
|
public OperationResult Edit(EditBank command)
|
|
{
|
|
OperationResult op = new();
|
|
if (string.IsNullOrWhiteSpace(command.BankName))
|
|
return op.Failed("فیلد نام بانک نمی تواند خالی باشد");
|
|
|
|
var entity = _bankRepository.Get(command.Id);
|
|
if (entity == null)
|
|
return op.Failed(ApplicationMessages.RecordNotFound);
|
|
|
|
long mediaId = entity.BankLogoMediaId;
|
|
if (command.BankLogoPictureFile != null && command.BankLogoPictureFile.Length > 0)
|
|
{
|
|
var uploadResult = _mediaApplication.UploadFile(command.BankLogoPictureFile, command.BankName,
|
|
_basePath, 10, [".jpg", ".jpeg", ".png",".svg"], "Bank");
|
|
if (uploadResult.IsSuccedded == false)
|
|
return uploadResult;
|
|
_mediaApplication.DeleteFile(entity.BankLogoMediaId);
|
|
mediaId = uploadResult.SendId;
|
|
}
|
|
entity.Edit(command.BankName,mediaId);
|
|
_bankRepository.SaveChanges();
|
|
|
|
return op.Succcedded();
|
|
}
|
|
|
|
public List<BankViewModel> Search(string name)
|
|
{
|
|
var banks = _bankRepository.Search(name);
|
|
var medias = _mediaApplication.GetRange(banks.Select(x => x.BankLogoPictureMediaId));
|
|
return banks.Select(x=> new BankViewModel()
|
|
{
|
|
BankLogoPicturePath = medias.FirstOrDefault(y=>y.Id == x.BankLogoPictureMediaId)?.Path ??"",
|
|
BankName = x.BankName,
|
|
Id = x.Id
|
|
}).ToList();
|
|
}
|
|
|
|
public List<BankSelectList> GetBanksForSelectList()
|
|
{
|
|
return _bankRepository.GetBanksForSelectList();
|
|
}
|
|
}
|
|
}
|