Switch to PostgreSQL and update database configuration

This commit is contained in:
2025-01-21 19:41:18 -06:00
parent 4f76a08ca3
commit 364c7d83e2
7 changed files with 13 additions and 173 deletions
@@ -1,57 +0,0 @@
// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace barkmanapi.Migrations
{
[DbContext(typeof(BarkContext))]
[Migration("20250109192017_InitialCreate")]
partial class InitialCreate
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "9.0.0");
modelBuilder.Entity("InventoryItems", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Brand")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Notes")
.HasColumnType("TEXT");
b.Property<float?>("RentalPrice")
.HasColumnType("REAL");
b.Property<float?>("ReplacementCost")
.HasColumnType("REAL");
b.Property<string>("SerialNumber")
.HasColumnType("TEXT");
b.Property<string>("Status")
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Inventory");
});
#pragma warning restore 612, 618
}
}
}
@@ -1,40 +0,0 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace barkmanapi.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Inventory",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Name = table.Column<string>(type: "TEXT", nullable: false),
Brand = table.Column<string>(type: "TEXT", nullable: false),
SerialNumber = table.Column<string>(type: "TEXT", nullable: true),
Status = table.Column<string>(type: "TEXT", nullable: true),
RentalPrice = table.Column<float>(type: "REAL", nullable: true),
ReplacementCost = table.Column<float>(type: "REAL", nullable: true),
Notes = table.Column<string>(type: "TEXT", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Inventory", x => x.Id);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Inventory");
}
}
}
@@ -1,54 +0,0 @@
// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace barkmanapi.Migrations
{
[DbContext(typeof(BarkContext))]
partial class BarkContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "9.0.0");
modelBuilder.Entity("InventoryItems", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Brand")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Notes")
.HasColumnType("TEXT");
b.Property<float?>("RentalPrice")
.HasColumnType("REAL");
b.Property<float?>("ReplacementCost")
.HasColumnType("REAL");
b.Property<string>("SerialNumber")
.HasColumnType("TEXT");
b.Property<string>("Status")
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Inventory");
});
#pragma warning restore 612, 618
}
}
}
+5 -1
View File
@@ -1,3 +1,4 @@
using barkmanapi;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Models;
@@ -20,6 +21,9 @@ builder.Services.AddCors(options =>
policy.WithOrigins("https://barkdev.ts.drewr.io", "http://localhost:5173").AllowAnyMethod().AllowAnyHeader(); policy.WithOrigins("https://barkdev.ts.drewr.io", "http://localhost:5173").AllowAnyMethod().AllowAnyHeader();
}); });
}); });
builder.Services.AddDbContext<BarkContext>(opt =>
opt.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")).UseSnakeCaseNamingConvention());
var app = builder.Build(); var app = builder.Build();
if (!app.Environment.IsProduction()) if (!app.Environment.IsProduction())
@@ -34,7 +38,7 @@ if (!app.Environment.IsProduction())
app.MapGet("/", () => "Hello World!"); app.MapGet("/", () => "Hello World!");
app.MapGet("/inventory", async (BarkContext db) => app.MapGet("/inventory", async (BarkContext db) =>
await db.Inventory.ToListAsync()); await db.Inventory.OrderBy(item=>item.Id).ToListAsync());
app.MapGet("/inventory/{id}", async (int id, BarkContext db) => app.MapGet("/inventory/{id}", async (int id, BarkContext db) =>
{ {
+4 -19
View File
@@ -1,24 +1,10 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
public class BarkContext : DbContext namespace barkmanapi;
public class BarkContext(DbContextOptions<BarkContext> options) : DbContext(options)
{ {
public DbSet<InventoryItems> Inventory { get; set; } public DbSet<InventoryItems> Inventory { get; set; }
public string DbPath { get; }
public BarkContext()
{
var folder = Environment.SpecialFolder.LocalApplicationData;
var path = Environment.GetFolderPath(folder);
DbPath = "./database/app.db";
}
// The following configures EF to create a Sqlite database file in the
// special "local" folder for your platform.
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlite($"Data Source={DbPath}");
} }
public class InventoryItems public class InventoryItems
@@ -31,5 +17,4 @@ public class InventoryItems
public float? RentalPrice { get; set; } public float? RentalPrice { get; set; }
public float? ReplacementCost { get; set; } public float? ReplacementCost { get; set; }
public string? Notes { get; set; } public string? Notes { get; set; }
} }
+3 -1
View File
@@ -5,15 +5,17 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS> <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<UserSecretsId>a965d65f-8c78-4549-9c7c-8c4d221a8a02</UserSecretsId>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="EFCore.NamingConventions" Version="9.0.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" /> <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.0"> <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.0" /> <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.1.4" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.1.4" />
</ItemGroup> </ItemGroup>
+1 -1
View File
@@ -1 +1 @@
VITE_API_URL=https://barkdev.ts.drewr.io VITE_API_URL=http://localhost:5145