Initial commit

This commit is contained in:
Mark J Price 2022-03-13 16:17:01 +00:00
parent 1d9d051759
commit 9656378279
557 changed files with 182300 additions and 0 deletions

View file

@ -0,0 +1,59 @@
using Microsoft.EntityFrameworkCore;
namespace Packt.Shared;
public class NorthwindService : INorthwindService
{
private readonly NorthwindContext db;
public NorthwindService(NorthwindContext db)
{
this.db = db;
}
public Task<List<Customer>> GetCustomersAsync()
{
return db.Customers.ToListAsync();
}
public Task<List<Customer>> GetCustomersAsync(string country)
{
return db.Customers.Where(c => c.Country == country).ToListAsync();
}
public Task<Customer?> GetCustomerAsync(string id)
{
return db.Customers.FirstOrDefaultAsync
(c => c.CustomerId == id);
}
public Task<Customer> CreateCustomerAsync(Customer c)
{
db.Customers.Add(c);
db.SaveChangesAsync();
return Task.FromResult(c);
}
public Task<Customer> UpdateCustomerAsync(Customer c)
{
db.Entry(c).State = EntityState.Modified;
db.SaveChangesAsync();
return Task.FromResult(c);
}
public Task DeleteCustomerAsync(string id)
{
Customer? customer = db.Customers.FirstOrDefaultAsync
(c => c.CustomerId == id).Result;
if (customer == null)
{
return Task.CompletedTask;
}
else
{
db.Customers.Remove(customer);
return db.SaveChangesAsync();
}
}
}