Database migration InitialCreate

This commit is contained in:
Charles Showalter 2022-05-09 17:07:59 -07:00
parent 1a9fd04f26
commit fad8bac9dc
20 changed files with 380 additions and 114 deletions

View File

@ -16,7 +16,7 @@
<ItemGroup>
<ProjectReference Include="..\Core\Core.csproj" />
<ProjectReference Include="..\Infrastucture\Infrastucture.csproj" />
<ProjectReference Include="..\Infrastructure\Infrastructure.csproj" />
</ItemGroup>
</Project>

View File

@ -1,7 +1,6 @@
using Infrastucture.Data;
using Core.Entities;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Core.Interfaces;
namespace API.Controllers
{
@ -9,16 +8,16 @@ namespace API.Controllers
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
private readonly StoreContext _context;
public ProductsController(StoreContext context)
private readonly iProductRepository _repo;
public ProductsController(iProductRepository repo)
{
_context = context;
_repo = repo;
}
[HttpGet]
public async Task<ActionResult<List<Product>>> GetProducts()
{
var products = await _context.Products.ToListAsync();
var products = await _repo.GetProductsAync();
return Ok(products);
}
@ -26,7 +25,7 @@ namespace API.Controllers
[HttpGet("{id}")]
public async Task<ActionResult<Product>> GetProduct(int id)
{
return await _context.Products.FindAsync(id);
return await _repo.GetProductByIdAsync(id);
}
}
}

View File

@ -1,3 +1,4 @@
using Core.Interfaces;
using Infrastucture.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
@ -19,6 +20,7 @@ namespace API
services.AddControllers();
services.AddDbContext<StoreContext>(x => x.UseSqlite(_config.GetConnectionString("DefaultConnection")));
services.AddScoped<iProductRepository, ProductRepository>();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebAPIv5", Version = "v1" });

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,7 @@
namespace Core.Entities
{
public class BaseEntity
{
public int Id { get; set; }
}
}

View File

@ -1,8 +1,20 @@
namespace Core.Entities
{
public class Product
public class Product : BaseEntity
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public string PictureUrl { get; set; }
public ProductType ProductType { get; set; }
public int ProductTypeId { get; set; }
public ProductBrand ProductBrand { get; set; }
public int ProductBrandId { get; set; }
}
}

View File

@ -0,0 +1,7 @@
namespace Core.Entities
{
public class ProductBrand : BaseEntity
{
public string Name { get; set; }
}
}

View File

@ -0,0 +1,7 @@
namespace Core.Entities
{
public class ProductType : BaseEntity
{
public string Name { get; set; }
}
}

View File

@ -0,0 +1,10 @@
using Core.Entities;
namespace Core.Interfaces
{
public interface iProductRepository
{
Task<Product> GetProductByIdAsync(int id);
Task<IReadOnlyList<Product>> GetProductsAync();
}
}

View File

@ -7,7 +7,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "API", "API\API.csproj", "{B
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Core", "Core\Core.csproj", "{124DD725-C323-44B3-B490-0D3613FE6AFB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Infrastucture", "Infrastucture\Infrastucture.csproj", "{D2AA653B-A7B8-4365-9D72-213F87B67175}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Infrastucture", "Infrastructure\Infrastucture.csproj", "{4DDBA348-67D9-4797-9A0F-355220BC9B19}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -30,5 +30,9 @@ Global
{D2AA653B-A7B8-4365-9D72-213F87B67175}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D2AA653B-A7B8-4365-9D72-213F87B67175}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D2AA653B-A7B8-4365-9D72-213F87B67175}.Release|Any CPU.Build.0 = Release|Any CPU
{4DDBA348-67D9-4797-9A0F-355220BC9B19}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4DDBA348-67D9-4797-9A0F-355220BC9B19}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4DDBA348-67D9-4797-9A0F-355220BC9B19}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4DDBA348-67D9-4797-9A0F-355220BC9B19}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,103 @@
// <auto-generated />
using Infrastucture.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace Infrastructure.Data.Migrations
{
[DbContext(typeof(StoreContext))]
[Migration("20220509234932_InitialCreate")]
partial class InitialCreate
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "6.0.4");
modelBuilder.Entity("Core.Entities.Product", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Description")
.HasColumnType("TEXT");
b.Property<string>("Name")
.HasColumnType("TEXT");
b.Property<string>("PictureUrl")
.HasColumnType("TEXT");
b.Property<decimal>("Price")
.HasColumnType("TEXT");
b.Property<int>("ProductBrandId")
.HasColumnType("INTEGER");
b.Property<int>("ProductTypeId")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.HasIndex("ProductBrandId");
b.HasIndex("ProductTypeId");
b.ToTable("Products");
});
modelBuilder.Entity("Core.Entities.ProductBrand", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Name")
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("ProductBrands");
});
modelBuilder.Entity("Core.Entities.ProductType", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Name")
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("ProductTypes");
});
modelBuilder.Entity("Core.Entities.Product", b =>
{
b.HasOne("Core.Entities.ProductBrand", "ProductBrand")
.WithMany()
.HasForeignKey("ProductBrandId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Core.Entities.ProductType", "ProductType")
.WithMany()
.HasForeignKey("ProductTypeId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("ProductBrand");
b.Navigation("ProductType");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,90 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Infrastructure.Data.Migrations
{
public partial class InitialCreate : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "ProductBrands",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Name = table.Column<string>(type: "TEXT", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_ProductBrands", x => x.Id);
});
migrationBuilder.CreateTable(
name: "ProductTypes",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Name = table.Column<string>(type: "TEXT", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_ProductTypes", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Products",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Name = table.Column<string>(type: "TEXT", nullable: true),
Description = table.Column<string>(type: "TEXT", nullable: true),
Price = table.Column<decimal>(type: "TEXT", nullable: false),
PictureUrl = table.Column<string>(type: "TEXT", nullable: true),
ProductTypeId = table.Column<int>(type: "INTEGER", nullable: false),
ProductBrandId = table.Column<int>(type: "INTEGER", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Products", x => x.Id);
table.ForeignKey(
name: "FK_Products_ProductBrands_ProductBrandId",
column: x => x.ProductBrandId,
principalTable: "ProductBrands",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_Products_ProductTypes_ProductTypeId",
column: x => x.ProductTypeId,
principalTable: "ProductTypes",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_Products_ProductBrandId",
table: "Products",
column: "ProductBrandId");
migrationBuilder.CreateIndex(
name: "IX_Products_ProductTypeId",
table: "Products",
column: "ProductTypeId");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Products");
migrationBuilder.DropTable(
name: "ProductBrands");
migrationBuilder.DropTable(
name: "ProductTypes");
}
}
}

View File

@ -0,0 +1,101 @@
// <auto-generated />
using Infrastucture.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace Infrastructure.Data.Migrations
{
[DbContext(typeof(StoreContext))]
partial class StoreContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "6.0.4");
modelBuilder.Entity("Core.Entities.Product", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Description")
.HasColumnType("TEXT");
b.Property<string>("Name")
.HasColumnType("TEXT");
b.Property<string>("PictureUrl")
.HasColumnType("TEXT");
b.Property<decimal>("Price")
.HasColumnType("TEXT");
b.Property<int>("ProductBrandId")
.HasColumnType("INTEGER");
b.Property<int>("ProductTypeId")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.HasIndex("ProductBrandId");
b.HasIndex("ProductTypeId");
b.ToTable("Products");
});
modelBuilder.Entity("Core.Entities.ProductBrand", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Name")
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("ProductBrands");
});
modelBuilder.Entity("Core.Entities.ProductType", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Name")
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("ProductTypes");
});
modelBuilder.Entity("Core.Entities.Product", b =>
{
b.HasOne("Core.Entities.ProductBrand", "ProductBrand")
.WithMany()
.HasForeignKey("ProductBrandId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Core.Entities.ProductType", "ProductType")
.WithMany()
.HasForeignKey("ProductTypeId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("ProductBrand");
b.Navigation("ProductType");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,25 @@
using Core.Entities;
using Core.Interfaces;
using Microsoft.EntityFrameworkCore;
namespace Infrastucture.Data
{
public class ProductRepository : iProductRepository
{
private readonly StoreContext _context;
public ProductRepository(StoreContext context)
{
_context = context;
}
public async Task<Product> GetProductByIdAsync(int id)
{
return await _context.Products.FindAsync(id);
}
public async Task<IReadOnlyList<Product>> GetProductsAync()
{
return await _context.Products.ToListAsync();
}
}
}

View File

@ -9,5 +9,7 @@ namespace Infrastucture.Data
{
}
public DbSet<Product> Products { get; set; }
public DbSet<ProductBrand> ProductBrands { get; set; }
public DbSet<ProductType> ProductTypes { get; set; }
}
}

View File

@ -1,37 +0,0 @@
// <auto-generated />
using API.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace Infrastucture.Data.Migrations
{
[DbContext(typeof(StoreContext))]
[Migration("20220509191604_InitialCreate")]
partial class InitialCreate
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "6.0.4");
modelBuilder.Entity("API.Entities.Product", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Name")
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Products");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -1,31 +0,0 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Infrastucture.Data.Migrations
{
public partial class InitialCreate : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Products",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Name = table.Column<string>(type: "TEXT", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Products", x => x.Id);
});
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Products");
}
}
}

View File

@ -1,35 +0,0 @@
// <auto-generated />
using Infrastucture.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace API.Data.Migrations
{
[DbContext(typeof(StoreContext))]
partial class StoreContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "6.0.4");
modelBuilder.Entity("API.Entities.Product", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Name")
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Products");
});
#pragma warning restore 612, 618
}
}
}