mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2026-04-05 06:15:24 +00:00
Initial commit
This commit is contained in:
parent
1d9d051759
commit
9656378279
557 changed files with 182300 additions and 0 deletions
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue