using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
namespace Dotmyself
{
///
/// It implements the operations the client can perform on the Banking Service.
/// Author : Hyacinthe MENIET
///
public class BankingService : IBankingService
{
private readonly DataManager accountManager;
private readonly DataManager operationManager;
public BankingService()
{
string ConnectStr = ConfigurationManager.ConnectionStrings["BankingConnectionString"].ConnectionString;
BankingDataContext context = new BankingDataContext(ConnectStr);
accountManager = new DataManager(context);
operationManager = new DataManager(context);
}
public void depositMoney(int aId, decimal amount)
{
Operation op = new Operation
{
amount = Math.Abs(amount),
account_id = aId,
date_insertion = DateTime.Now
};
operationManager.Create(op);
operationManager.Commit();
}
public IList GetAccounts()
{
return accountManager.List().ToList();
}
public decimal GetBalance(int aId)
{
Account ac = accountManager.Get(aId);
decimal sumOp = ac.Operations.Sum(o => o.amount);
return sumOp + ac.initial_balance;
}
public IList GetOperationsByAccount(int aId)
{
Account ac = accountManager.Get(aId);
return ac.Operations.ToList();
}
public void transferMoney(int fromId, int toId, decimal amount)
{
Operation opFrom = new Operation
{
amount = -1 * Math.Abs(amount),
account_id = fromId,
date_insertion = DateTime.Now
};
Operation opTo = new Operation
{
amount = Math.Abs(amount),
account_id = toId,
date_insertion = DateTime.Now
};
operationManager.Create(opFrom);
operationManager.Create(opTo);
operationManager.Commit();
}
public void withdrawMoney(int aId, decimal amount)
{
Operation op = new Operation
{
amount = -1 * Math.Abs(amount),
account_id = aId,
date_insertion = DateTime.Now
};
operationManager.Create(op);
operationManager.Commit();
}
}
}