react-version #1
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
32
GrossesMitaines/GrossesMitainesAPI/Data/InventoryContext.cs
Normal file
32
GrossesMitaines/GrossesMitainesAPI/Data/InventoryContext.cs
Normal file
@ -0,0 +1,32 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using GrossesMitainesAPI.Models;
|
||||
|
||||
namespace GrossesMitainesAPI.Data;
|
||||
|
||||
public class InventoryContext : DbContext {
|
||||
public DbSet<Product> Products { get; set; }
|
||||
public InventoryContext(DbContextOptions<InventoryContext> options) : base(options) { }
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder) {
|
||||
base.OnModelCreating(modelBuilder);
|
||||
|
||||
// Pour partir la BD.
|
||||
modelBuilder.Entity<Product>().HasData(new Product {
|
||||
Id = 1,
|
||||
Title = $"",
|
||||
Description = $"",
|
||||
Price = 0.00M,
|
||||
ImageData = $""
|
||||
});
|
||||
|
||||
}
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
|
||||
var configuration = new ConfigurationBuilder()
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile("appsettings.json")
|
||||
.Build();
|
||||
|
||||
var connectionString = configuration.GetConnectionString("DefaultConnection");
|
||||
optionsBuilder.UseSqlServer(connectionString);
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
@ -7,7 +7,17 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.9" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.9">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.9" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.9">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
15
GrossesMitaines/GrossesMitainesAPI/Models/Product.cs
Normal file
15
GrossesMitaines/GrossesMitainesAPI/Models/Product.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace GrossesMitainesAPI.Models;
|
||||
|
||||
public class Product {
|
||||
[Key]
|
||||
public int Id { get; set; }
|
||||
[Required, MaxLength(255)]
|
||||
public string Title { get; set; } = "";
|
||||
[Required]
|
||||
public string Description { get; set; } = "";
|
||||
[Range(0, (double)decimal.MaxValue)] // Range qui prend pas les decimals!
|
||||
public decimal Price { get; set; } = 0;
|
||||
public string? ImageData { get; set; } // Base64 en string pour le html.
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
namespace GrossesMitainesAPI;
|
||||
|
||||
public class WeatherForecast
|
||||
{
|
||||
public DateTime Date { get; set; }
|
||||
|
||||
public int TemperatureC { get; set; }
|
||||
|
||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
||||
|
||||
public string? Summary { get; set; }
|
||||
}
|
@ -5,5 +5,8 @@
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
"AllowedHosts": "*",
|
||||
"ConnectionStrings": {
|
||||
"DefaultConnection": "Server=(localdb)\\mssqllocaldb; Database=GrosseMitainesDB; Trusted_Connection=True; MultipleActiveResultSets=true"
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user