using DaSaSo.Domain.Model; using DaSaSo.Domain.Services; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DaSaSo.EntityFramework.Services { public class ClientDataService : IDataService { private readonly DaSaSoDbContextFactory _contextFactory; public ClientDataService(DaSaSoDbContextFactory contextFactory) { _contextFactory = contextFactory; } public async Task Create(Client entity) { using (DaSaSoDbContext context = _contextFactory.CreateDbContext()) { EntityEntry createdResult = await context.Set().AddAsync(entity); await context.SaveChangesAsync(); return createdResult.Entity; } } public async Task Delete(int id) { using (DaSaSoDbContext context = _contextFactory.CreateDbContext()) { Client entity = await context.Set().FirstOrDefaultAsync((e) => e.Id == id); context.Set().Remove(entity); await context.SaveChangesAsync(); return true; } } public async Task Get(int id) { using (DaSaSoDbContext context = _contextFactory.CreateDbContext()) { Client entity = await context.Clients.Include(a => a.Projects).FirstOrDefaultAsync((e) => e.Id == id); return entity; } } public async Task> GetAll() { using (DaSaSoDbContext context = _contextFactory.CreateDbContext()) { IEnumerable entities = await context.Set().ToListAsync(); return entities; } } public async Task Update(int id, Client entity) { using (DaSaSoDbContext context = _contextFactory.CreateDbContext()) { entity.Id = id; context.Set().Update(entity); await context.SaveChangesAsync(); return entity; } } } }