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,87 @@
using Microsoft.AspNetCore.Mvc; // [ApiController], [Route]
using Microsoft.EntityFrameworkCore; // ToListAsync, FirstOrDefaultAsync
using Packt.Shared; // NorthwindContext, Customer
namespace Northwind.BlazorWasm.Server.Controllers;
[ApiController]
[Route("api/[controller]")]
public class CustomersController : ControllerBase
{
private readonly NorthwindContext db;
public CustomersController(NorthwindContext db)
{
this.db = db;
}
[HttpGet]
public async Task<List<Customer>> GetCustomersAsync()
{
return await db.Customers.ToListAsync();
}
[HttpGet("in/{country}")] // different path to disambiguate
public async Task<List<Customer>> GetCustomersAsync(string country)
{
return await db.Customers
.Where(c => c.Country == country).ToListAsync();
}
[HttpGet("{id}")]
public async Task<Customer?> GetCustomerAsync(string id)
{
return await db.Customers
.FirstOrDefaultAsync(c => c.CustomerId == id);
}
[HttpPost]
public async Task<Customer?> CreateCustomerAsync
(Customer customerToAdd)
{
Customer? existing = await db.Customers.FirstOrDefaultAsync
(c => c.CustomerId == customerToAdd.CustomerId);
if (existing == null)
{
db.Customers.Add(customerToAdd);
int affected = await db.SaveChangesAsync();
if (affected == 1)
{
return customerToAdd;
}
}
return existing;
}
[HttpPut]
public async Task<Customer?> UpdateCustomerAsync(Customer c)
{
db.Entry(c).State = EntityState.Modified;
int affected = await db.SaveChangesAsync();
if (affected == 1)
{
return c;
}
return null;
}
[HttpDelete("{id}")]
public async Task<int> DeleteCustomerAsync(string id)
{
Customer? c = await db.Customers.FirstOrDefaultAsync
(c => c.CustomerId == id);
if (c != null)
{
db.Customers.Remove(c);
int affected = await db.SaveChangesAsync();
return affected;
}
return 0;
}
}

View file

@ -0,0 +1,34 @@
using Microsoft.AspNetCore.Mvc;
using Northwind.BlazorWasm.Shared;
namespace Northwind.BlazorWasm.Server.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
}