Client Service hinzugefügt
This commit is contained in:
74
DaSaSo.EntityFramework/Services/ClientDataService.cs
Normal file
74
DaSaSo.EntityFramework/Services/ClientDataService.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
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<Client>
|
||||
{
|
||||
private readonly DaSaSoDbContextFactory _contextFactory;
|
||||
|
||||
public ClientDataService(DaSaSoDbContextFactory contextFactory)
|
||||
{
|
||||
_contextFactory = contextFactory;
|
||||
}
|
||||
|
||||
public async Task<Client> Create(Client entity)
|
||||
{
|
||||
using (DaSaSoDbContext context = _contextFactory.CreateDbContext())
|
||||
{
|
||||
EntityEntry<Client> createdResult = await context.Set<Client>().AddAsync(entity);
|
||||
await context.SaveChangesAsync();
|
||||
return createdResult.Entity;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> Delete(int id)
|
||||
{
|
||||
using (DaSaSoDbContext context = _contextFactory.CreateDbContext())
|
||||
{
|
||||
Client entity = await context.Set<Client>().FirstOrDefaultAsync((e) => e.Id == id);
|
||||
context.Set<Client>().Remove(entity);
|
||||
await context.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Client> 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<IEnumerable<Client>> GetAll()
|
||||
{
|
||||
using (DaSaSoDbContext context = _contextFactory.CreateDbContext())
|
||||
{
|
||||
IEnumerable<Client> entities = await context.Set<Client>().ToListAsync();
|
||||
|
||||
return entities;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Client> Update(int id, Client entity)
|
||||
{
|
||||
using (DaSaSoDbContext context = _contextFactory.CreateDbContext())
|
||||
{
|
||||
entity.Id = id;
|
||||
context.Set<Client>().Update(entity);
|
||||
await context.SaveChangesAsync();
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user