Currency creation and minting with basis of transactions
This commit is contained in:
55
src/IO.Swagger/Repositories/CurrencyRepository.cs
Normal file
55
src/IO.Swagger/Repositories/CurrencyRepository.cs
Normal file
@ -0,0 +1,55 @@
|
||||
using IO.Swagger.Models.db;
|
||||
using IO.Swagger.Models.dto;
|
||||
using IO.Swagger.Services;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IO.Swagger.Repositories
|
||||
{
|
||||
public class CurrencyRepository : ICurrencyRepository
|
||||
{
|
||||
BankDbContext context;
|
||||
|
||||
public CurrencyRepository(BankDbContext context)
|
||||
{
|
||||
this.context = context ?? throw new ArgumentNullException(nameof(context));
|
||||
}
|
||||
|
||||
public async Task<bool> CreateCurrency(CurrencyCreateBody request, int userId)
|
||||
{
|
||||
request.Symbol = request.Symbol.Trim();
|
||||
request.Name = request.Name.Trim();
|
||||
if (await context.Currencies.AnyAsync(c => c.Symbol == request.Symbol || c.Name.ToLower() == request.Name.ToLower()))
|
||||
return false;
|
||||
|
||||
await context.Currencies.AddAsync(new Currency
|
||||
{
|
||||
Name = request.Name,
|
||||
Symbol = request.Symbol,
|
||||
UserId = userId
|
||||
});
|
||||
|
||||
return await context.SaveChangesAsync() > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> MintCurrency(CurrencyMintBody request, int userId)
|
||||
{
|
||||
var existsAndIsOwner = await context.Currencies.AnyAsync(c => c.CurrencyId == request.CurrencyId && c.UserId == userId);
|
||||
if (!existsAndIsOwner)
|
||||
return false;
|
||||
|
||||
await context.Transactions.AddAsync(new Transaction
|
||||
{
|
||||
Amount = request.Amount,
|
||||
CurrencyId = request.CurrencyId,
|
||||
ToUserId = userId,
|
||||
FromUserId = userId,
|
||||
Memo = "Minting"
|
||||
});
|
||||
|
||||
return await context.SaveChangesAsync() > 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user