Sparat innan resan
This commit is contained in:
35
OemanTrader.Domain/Exceptions/InsufficientFundsException.cs
Normal file
35
OemanTrader.Domain/Exceptions/InsufficientFundsException.cs
Normal file
@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OemanTrader.Domain.Exceptions
|
||||
{
|
||||
public class InsufficientFundsException : Exception
|
||||
{
|
||||
public double AccountBalance { get; set; }
|
||||
public double RequiredBalance { get; set; }
|
||||
|
||||
public InsufficientFundsException(double accountBalance, double requiredBalance)
|
||||
{
|
||||
AccountBalance = accountBalance;
|
||||
RequiredBalance = requiredBalance;
|
||||
}
|
||||
|
||||
public InsufficientFundsException(double accountBalance, double requiredBalance, string? message) : base(message)
|
||||
{
|
||||
AccountBalance = accountBalance;
|
||||
RequiredBalance = requiredBalance;
|
||||
}
|
||||
|
||||
public InsufficientFundsException(double accountBalance, double requiredBalance, string? message, Exception? innerException) : base(message, innerException)
|
||||
{
|
||||
AccountBalance = accountBalance;
|
||||
RequiredBalance = requiredBalance;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@ -10,6 +10,6 @@ namespace OemanTrader.Domain.Models
|
||||
{
|
||||
public User AccountHolder { get; set; }
|
||||
public double Balance { get; set; }
|
||||
public IEnumerable<AssetTransaction> AssetTransactions { get; set; }
|
||||
public ICollection<AssetTransaction> AssetTransactions { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,52 @@
|
||||
using OemanTrader.Domain.Exceptions;
|
||||
using OemanTrader.Domain.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OemanTrader.Domain.Services.TransactionServices
|
||||
{
|
||||
public class BuyStockService : IBuyStockService
|
||||
{
|
||||
private readonly IStockPriceService _stockPriceService;
|
||||
private readonly IDataService<Account> _accountService;
|
||||
|
||||
public BuyStockService(IStockPriceService stockPriceService, IDataService<Account> accountService)
|
||||
{
|
||||
_stockPriceService = stockPriceService;
|
||||
_accountService = accountService;
|
||||
}
|
||||
|
||||
public async Task<Account> BuyStock(Account buyer, string symbol, int shares)
|
||||
{
|
||||
double stockPrice = await _stockPriceService.GetPrice(symbol);
|
||||
double transactionPrice = stockPrice * shares;
|
||||
|
||||
if (transactionPrice > buyer.Balance)
|
||||
{
|
||||
throw new InsufficientFundsException(buyer.Balance,transactionPrice);
|
||||
}
|
||||
|
||||
AssetTransaction transaction = new AssetTransaction()
|
||||
{
|
||||
Account = buyer,
|
||||
Asset = new Asset() {
|
||||
PricePerShare = stockPrice,
|
||||
Symbol = symbol
|
||||
},
|
||||
DateProcessed = DateTime.Now,
|
||||
ShareAmount = shares,
|
||||
IsPurchase = true
|
||||
};
|
||||
|
||||
buyer.AssetTransactions.Add(transaction);
|
||||
buyer.Balance -= transactionPrice;
|
||||
|
||||
await _accountService.Update(buyer.Id, buyer);
|
||||
|
||||
return buyer;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
using OemanTrader.Domain.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OemanTrader.Domain.Services.TransactionServices
|
||||
{
|
||||
public interface IBuyStockService
|
||||
{
|
||||
Task<Account> BuyStock(Account buyer, string stock, int shares);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user