161 lines
5.2 KiB
C#
161 lines
5.2 KiB
C#
|
namespace AAIntegration.SimmonsBank.API.Services;
|
||
|
|
||
|
using AutoMapper;
|
||
|
using BCrypt.Net;
|
||
|
using AAIntegration.SimmonsBank.API.Entities;
|
||
|
using AAIntegration.SimmonsBank.API.Config;
|
||
|
using AAIntegration.SimmonsBank.API.Models.Accounts;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using System;
|
||
|
using Internal;
|
||
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
||
|
public interface IAccountService
|
||
|
{
|
||
|
IEnumerable<Account> GetAll();
|
||
|
Account GetById(int accountId);
|
||
|
void Create(AccountCreateRequest model);
|
||
|
void Update(int accountId, AccountUpdateRequest model);
|
||
|
Account RefreshAccountBalance(int accountId);
|
||
|
void Delete(int accountId);
|
||
|
AccountHistorical GetAccountHistorical(int accountId, int lookBack);
|
||
|
}
|
||
|
|
||
|
public class AccountService : IAccountService
|
||
|
{
|
||
|
private DataContext _context;
|
||
|
private readonly IMapper _mapper;
|
||
|
private IUserService _userService;
|
||
|
private ICurrencyTypeService _currencyTypeService;
|
||
|
|
||
|
public AccountService(
|
||
|
DataContext context,
|
||
|
IMapper mapper,
|
||
|
IUserService userService,
|
||
|
ICurrencyTypeService currencyTypeService)
|
||
|
{
|
||
|
_context = context;
|
||
|
_mapper = mapper;
|
||
|
_userService = userService;
|
||
|
_currencyTypeService = currencyTypeService;
|
||
|
}
|
||
|
|
||
|
public IEnumerable<Account> GetAll()
|
||
|
{
|
||
|
return _context.Accounts;
|
||
|
}
|
||
|
|
||
|
public Account GetById(int accountId)
|
||
|
{
|
||
|
return getAccount(accountId);
|
||
|
}
|
||
|
|
||
|
public void Create(AccountCreateRequest model)
|
||
|
{
|
||
|
// Check that account with same name or same external number doesn't exist
|
||
|
IEnumerable<Account> accountsWithSameName = _context.Accounts.Where(a => a.Name.ToUpper() == model.Name.ToUpper());
|
||
|
|
||
|
if (accountsWithSameName.Count() > 0)
|
||
|
throw new AppException("Account with name '" + model.Name + "' already exists");
|
||
|
|
||
|
if (!string.IsNullOrWhiteSpace(model.ExternalAccountNumber))
|
||
|
{
|
||
|
IEnumerable<Account> matches = _context.Accounts.Where(a => a.ExternalAccountNumber == model.ExternalAccountNumber);
|
||
|
|
||
|
if (matches.Count() > 0)
|
||
|
throw new AppException("Account with external account number '" + model.ExternalAccountNumber + "' already exists under account named '" + matches.First().Name + "'");
|
||
|
}
|
||
|
|
||
|
// map model to new account object
|
||
|
//var account = _mapper.Map<Account>(model);
|
||
|
|
||
|
Account account = new Account {
|
||
|
Name = model.Name,
|
||
|
InitialBalance = Convert.ToDecimal(model.InitialBalance),
|
||
|
Balance = Convert.ToDecimal(model.InitialBalance),
|
||
|
VirtualBalance = Convert.ToDecimal(model.InitialBalance),
|
||
|
LastActivity = DateTime.UtcNow,
|
||
|
CreatedOn = DateTime.UtcNow,
|
||
|
CurrencyId = model.Currency,
|
||
|
Currency = _currencyTypeService.GetById(model.Currency),
|
||
|
ExternalAccountNumber = model.ExternalAccountNumber
|
||
|
};
|
||
|
|
||
|
_context.Accounts.Add(account);
|
||
|
_context.SaveChanges();
|
||
|
}
|
||
|
|
||
|
public void Update(int accountId, AccountUpdateRequest model)
|
||
|
{
|
||
|
Account account = getAccount(accountId);
|
||
|
|
||
|
// validate
|
||
|
if (model.Name != account.Name && _context.Accounts.Any(x => x.Name == model.Name))
|
||
|
throw new AppException("Account with the name '" + model.Name + "' already exists");
|
||
|
|
||
|
// Name
|
||
|
if (!string.IsNullOrWhiteSpace(model.Name))
|
||
|
account.Name = model.Name;
|
||
|
|
||
|
// Owner
|
||
|
/*if (model.Owner.HasValue)
|
||
|
{
|
||
|
account.OwnerId = model.Owner.Value;
|
||
|
account.Owner = _userService.GetById(model.Owner.Value);
|
||
|
}*/
|
||
|
|
||
|
// Initial Balance
|
||
|
if (!string.IsNullOrWhiteSpace(model.InitialBalance))
|
||
|
account.InitialBalance = Convert.ToDecimal(model.InitialBalance);
|
||
|
|
||
|
// CurrencyType
|
||
|
if (model.Currency.HasValue)
|
||
|
{
|
||
|
account.CurrencyId = model.Currency.Value;
|
||
|
account.Currency = _context.CurrencyTypes.Find(model.Currency.Value);
|
||
|
}
|
||
|
|
||
|
// External Account Number
|
||
|
if (!string.IsNullOrWhiteSpace(model.ExternalAccountNumber))
|
||
|
account.ExternalAccountNumber = model.ExternalAccountNumber;
|
||
|
|
||
|
// copy model to account and save
|
||
|
//_mapper.Map(model, account);
|
||
|
_context.Accounts.Update(account);
|
||
|
_context.SaveChanges();
|
||
|
_context.RecalculateAccountBalance(accountId);
|
||
|
}
|
||
|
|
||
|
public Account RefreshAccountBalance(int accountId)
|
||
|
{
|
||
|
_context.RecalculateAccountBalance(accountId);
|
||
|
return _context.Accounts.Find(accountId);
|
||
|
}
|
||
|
|
||
|
public void Delete(int accountId)
|
||
|
{
|
||
|
var account = getAccount(accountId);
|
||
|
_context.Accounts.Remove(account);
|
||
|
_context.SaveChanges();
|
||
|
}
|
||
|
|
||
|
public AccountHistorical GetAccountHistorical(int accountId, int lookBack)
|
||
|
{
|
||
|
// Input validate lookBack
|
||
|
if (lookBack < 1)
|
||
|
throw new AppException($"Look Back value must be greater than 0.");
|
||
|
|
||
|
return _context.GetAccountHistorical(accountId, lookBack);
|
||
|
}
|
||
|
|
||
|
// helper methods
|
||
|
|
||
|
private Account getAccount(int id)
|
||
|
{
|
||
|
var account = _context.Accounts.Find(id);
|
||
|
if (account == null) throw new KeyNotFoundException("Account not found");
|
||
|
return account;
|
||
|
}
|
||
|
}
|