74 lines
2.4 KiB
C#
74 lines
2.4 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
|
using Microsoft.EntityFrameworkCore.Internal;
|
|
using OemanTrader.Domain.Models;
|
|
using OemanTrader.Domain.Services;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace OemanTrader.EntityFramework.Services
|
|
{
|
|
public class AccountDataService : IDataService<Account>
|
|
{
|
|
private readonly OemanTraderDbContextFactory _contextFactory;
|
|
|
|
public AccountDataService(OemanTraderDbContextFactory contextFactory)
|
|
{
|
|
_contextFactory = contextFactory;
|
|
}
|
|
public async Task<Account> Create(Account entity)
|
|
{
|
|
using (var context = _contextFactory.CreateDbContext())
|
|
{
|
|
EntityEntry<Account> createdResult = await context.Set<Account>().AddAsync(entity);
|
|
|
|
await context.SaveChangesAsync();
|
|
return createdResult.Entity;
|
|
}
|
|
}
|
|
public async Task<bool> Delete(int id)
|
|
{
|
|
using (var context = _contextFactory.CreateDbContext())
|
|
{
|
|
|
|
Account entity = await context.Set<Account>().FirstOrDefaultAsync((e) => e.Id == id);
|
|
context.Set<Account>().Remove(entity);
|
|
await context.SaveChangesAsync();
|
|
return true;
|
|
}
|
|
}
|
|
|
|
public async Task<Account> Get(int id)
|
|
{
|
|
using (var context = _contextFactory.CreateDbContext())
|
|
{
|
|
Account entity = await context.Accounts.Include(a => a.AssetTransactions).FirstOrDefaultAsync((e) => e.Id == id);
|
|
return entity;
|
|
}
|
|
}
|
|
|
|
public async Task<IEnumerable<Account>> GetAll()
|
|
{
|
|
using (var context = _contextFactory.CreateDbContext())
|
|
{
|
|
IEnumerable<Account> entities = await context.Accounts.Include(a => a.AssetTransactions).ToListAsync();
|
|
return entities;
|
|
}
|
|
}
|
|
|
|
public async Task<Account> Update(int Id, Account entity)
|
|
{
|
|
using (var context = _contextFactory.CreateDbContext())
|
|
{
|
|
entity.Id = Id;
|
|
context.Set<Account>().Update(entity);
|
|
await context.SaveChangesAsync();
|
|
return entity;
|
|
}
|
|
}
|
|
}
|
|
}
|