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>
<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" />
</ItemGroup>

View file

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

View file

@ -1,27 +1,16 @@
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build();
// Configure the HTTP request pipeline.
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.UseHttpsRedirection();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
app.MapFallbackToFile("index.html");
app.Run();