DataContext

This commit is contained in:
femsci 2023-10-21 15:57:56 +02:00
parent 7a95319ed3
commit fc10a2dd30
Signed by: femsci
GPG key ID: 08F7911F0E650C67
5 changed files with 64 additions and 18 deletions

View file

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Nyanlabs.SFood.Api.Models;
namespace Nyanlabs.SFood.Api;
public class DataContext : DbContext
{
public DbSet<IncidentSubmission> Submissions => Set<IncidentSubmission>();
protected override void OnModelCreating(ModelBuilder mb)
{
base.OnModelCreating(mb);
}
}

View file

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;
namespace Nyanlabs.SFood.Api.Models;
public class IncidentSubmission
{
//TODO: Validation
[Key]
public long Id { get; set; }
public required string Name { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public DateTime Submitted { get; set; }
public required string ProductName { get; set; }
public required string Producer { get; set; }
public required string Description { get; set; }
public required string Location { get; set; }
}

View file

@ -8,6 +8,12 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.12" /> <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.12" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.12" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.12">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.12" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup> </ItemGroup>

View file

@ -1,22 +1,31 @@
using Microsoft.EntityFrameworkCore;
using Nyanlabs.SFood.Api;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers(); builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<DataContext>(o =>
{
if (builder.Environment.IsDevelopment())
{
o.UseSqlite("Data Source=/data/store.db;");
}
});
var app = builder.Build(); var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())
{ {
app.UseSwagger(); app.UseSwagger();
app.UseSwaggerUI(); app.UseSwaggerUI();
} }
else
app.UseHttpsRedirection(); {
app.UseHttpsRedirection();
}
app.UseAuthorization(); app.UseAuthorization();

View file

@ -1,27 +1,16 @@
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build(); var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment()) if (!app.Environment.IsDevelopment())
{ {
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts(); app.UseHsts();
app.UseHttpsRedirection();
} }
app.UseHttpsRedirection();
app.UseStaticFiles(); app.UseStaticFiles();
app.UseRouting(); app.UseRouting();
app.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
app.MapFallbackToFile("index.html"); app.MapFallbackToFile("index.html");
app.Run(); app.Run();