53 lines
1.5 KiB
C#
Raw Permalink Normal View History

2024-12-19 23:55:07 +01:00
using Microsoft.AspNetCore.Mvc;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace Aufgabe19.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class CustomerController : ControllerBase
{
private List<Customer> mCustomers = new List<Customer>
{
new Customer(1, "Jens", "Mander"),
new Customer(2, "Hans", "Wurst"),
new Customer(3, "Guybrush", "Threepwod")
};
// GET: api/<CustomerController>
[HttpGet]
public IEnumerable<Customer> Get()
{
return mCustomers;
}
// GET api/<CustomerController>/5
[HttpGet("{id}")]
public Customer Get(int id)
{
return mCustomers.FirstOrDefault(customer => customer.Id == id);
}
// POST api/<CustomerController>
[HttpPost]
public void Post([FromBody] Customer value)
{
if (!mCustomers.Any(c => c.Id == value.Id
&& c.FirstName == value.FirstName
&& c.LastName == value.LastName))
{
mCustomers.Add(value);
}
}
// DELETE api/<CustomerController>/5
[HttpDelete("{id}")]
public void Delete(int id)
{
mCustomers.Remove(mCustomers.FirstOrDefault(customer => customer.Id == id));
}
}
}