Début API

This commit is contained in:
MarcEricMartel
2022-10-08 10:22:12 -07:00
parent 8b4c7e5684
commit 7ce0e7a83f
7 changed files with 112 additions and 47 deletions

View File

@@ -0,0 +1,49 @@
using Microsoft.AspNetCore.Mvc;
using GrossesMitainesAPI.Models;
using System.Linq;
using GrossesMitainesAPI.Data;
using Microsoft.Extensions.Logging;
namespace GrossesMitainesAPI.Controllers;
[ApiController]
[Route("[controller]")]
public class InventoryController : Controller {
private readonly ILogger<InventoryController> _logger;
private readonly InventoryContext _context;
public InventoryController(ILogger<InventoryController> logger, InventoryContext context) {
_logger = logger;
_context = context;
}
[HttpGet(Name ="Inventory")]
public IEnumerable<Product> Get(int? last) {
if (!last.HasValue)
last = 1;
return _context.Products.Where(x => x.Id >= last && x.Id < last + 5).ToList();
}
[HttpGet(Name = "Product")]
public Product Get(int id) {
Product prod;
try {
prod = _context.Products.Where(x => x.Id == id).First();
} catch (Exception e) {
_logger.LogError(8, e.Message);
prod = new Product();
}
return prod;
}
[HttpPost(Name = "Product")]
public void Post(Product prod) {
prod.Id = _context.Products.Count();
try {
_context.Products.Add(prod);
} catch (Exception e){
_logger.LogError(8, e.Message);
}
}
}

View File

@@ -1,32 +0,0 @@
using Microsoft.AspNetCore.Mvc;
namespace GrossesMitainesAPI.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(Name = "GetWeatherForecast")]
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();
}
}