From fad8bac9dcf92cd881f7ad0c9b97065ca8bb80e4 Mon Sep 17 00:00:00 2001 From: Charles Showalter Date: Mon, 9 May 2022 17:07:59 -0700 Subject: [PATCH] Database migration InitialCreate --- API/API.csproj | 2 +- API/Controllers/ProductsController.cs | 13 +-- API/Startup.cs | 2 + API/ecommerce.db | Bin 20480 -> 0 bytes API/ecommerce.db-wal | Bin 8272 -> 0 bytes Core/Entities/BaseEntity.cs | 7 ++ Core/Entities/Product.cs | 16 ++- Core/Entities/ProductBrand.cs | 7 ++ Core/Entities/ProductType.cs | 7 ++ Core/Interfaces/iProductRepository.cs | 10 ++ E-Commerce.sln | 6 +- .../20220509234932_InitialCreate.Designer.cs | 103 ++++++++++++++++++ .../20220509234932_InitialCreate.cs | 90 +++++++++++++++ .../Migrations/StoreContextModelSnapshot.cs | 101 +++++++++++++++++ Infrastructure/Data/ProductRepository.cs | 25 +++++ .../Data/StoreContext.cs | 2 + .../Infrastructure.csproj | 0 .../20220509191604_InitialCreate.Designer.cs | 37 ------- .../20220509191604_InitialCreate.cs | 31 ------ .../Migrations/StoreContextModelSnapshot.cs | 35 ------ 20 files changed, 380 insertions(+), 114 deletions(-) delete mode 100644 API/ecommerce.db-wal create mode 100644 Core/Entities/BaseEntity.cs create mode 100644 Core/Entities/ProductBrand.cs create mode 100644 Core/Entities/ProductType.cs create mode 100644 Core/Interfaces/iProductRepository.cs create mode 100644 Infrastructure/Data/Migrations/20220509234932_InitialCreate.Designer.cs create mode 100644 Infrastructure/Data/Migrations/20220509234932_InitialCreate.cs create mode 100644 Infrastructure/Data/Migrations/StoreContextModelSnapshot.cs create mode 100644 Infrastructure/Data/ProductRepository.cs rename {Infrastucture => Infrastructure}/Data/StoreContext.cs (70%) rename Infrastucture/Infrastucture.csproj => Infrastructure/Infrastructure.csproj (100%) delete mode 100644 Infrastucture/Data/Migrations/20220509191604_InitialCreate.Designer.cs delete mode 100644 Infrastucture/Data/Migrations/20220509191604_InitialCreate.cs delete mode 100644 Infrastucture/Data/Migrations/StoreContextModelSnapshot.cs diff --git a/API/API.csproj b/API/API.csproj index 9c4628b..5bc8cfe 100644 --- a/API/API.csproj +++ b/API/API.csproj @@ -16,7 +16,7 @@ - + diff --git a/API/Controllers/ProductsController.cs b/API/Controllers/ProductsController.cs index f01f3e2..735d3e8 100644 --- a/API/Controllers/ProductsController.cs +++ b/API/Controllers/ProductsController.cs @@ -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>> 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> GetProduct(int id) { - return await _context.Products.FindAsync(id); + return await _repo.GetProductByIdAsync(id); } } } \ No newline at end of file diff --git a/API/Startup.cs b/API/Startup.cs index d3b6a29..6e92fca 100644 --- a/API/Startup.cs +++ b/API/Startup.cs @@ -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(x => x.UseSqlite(_config.GetConnectionString("DefaultConnection"))); + services.AddScoped(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebAPIv5", Version = "v1" }); diff --git a/API/ecommerce.db b/API/ecommerce.db index f47dc3838c1abbe400c80f5b309679f445882644..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 100644 GIT binary patch literal 0 HcmV?d00001 literal 20480 zcmeI&F;Ck-6bJA-CqNs?xSJ>F$?5b^nKM{3ccXhOAy0KUW*7gMa`8AOHafKmY;|fB*y_0D=E4 z(5o(+TU)d*<1a%|oDJ2xUQ~#)kw{NwhfD2%J0WMG^RC0$%&{A3d@8opnVT2ul@)Vs zjb0j)oV`37j80@x?yC>7Smm_EHuM@(VYvf~xn9WM^MHAN$h@OYhqZn0Fbo_u&#c}- zbaz%SaJx>>X9v8`oTJcpJ(a)9y>NS)&Qt5MSjdlWGwHR>i(|TCwp#SkpDZwn_$SnUpcT#(}^ts2})$upyxS-qXFQ{fgqI;#4#Z2q|B=SNnb|$SV&DEC8 z&n-%lbRf>BcPJjoY%-4KUMFhIovx?{?b-NY(^aF!HBlP^0uX=z1Rwwb2tWV=5P$## zAOL|!C-90^xY@LuP5X_#*Vt?9+B=b(CNhbK?OeoC?AGo2PEFNCjbB7<2nav`0uX=z z1Rwwb2tWV=5P$##9;v{~3V;5uj{xJ}|J9%V5fFd?1Rwwb2tWV=5P$##AOHafJYfO6 e|3BfIi_#zf0SG_<0uX=z1Rwwb2tWV=Q-MDwdduYi diff --git a/API/ecommerce.db-wal b/API/ecommerce.db-wal deleted file mode 100644 index 583b3a8158461cb236d4bc569de7d78bc3bc66ed..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 8272 zcmXr7XKP~6eI&uaAiw|u&+o5&5gWCeY44OrzbY(5{s4toki{=buUW;rwt$@%D9Frz zj)DIJ{{tX33PwX GetProductByIdAsync(int id); + Task> GetProductsAync(); + } +} \ No newline at end of file diff --git a/E-Commerce.sln b/E-Commerce.sln index 7828964..0f46fa7 100644 --- a/E-Commerce.sln +++ b/E-Commerce.sln @@ -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 diff --git a/Infrastructure/Data/Migrations/20220509234932_InitialCreate.Designer.cs b/Infrastructure/Data/Migrations/20220509234932_InitialCreate.Designer.cs new file mode 100644 index 0000000..a6366e9 --- /dev/null +++ b/Infrastructure/Data/Migrations/20220509234932_InitialCreate.Designer.cs @@ -0,0 +1,103 @@ +// +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("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Description") + .HasColumnType("TEXT"); + + b.Property("Name") + .HasColumnType("TEXT"); + + b.Property("PictureUrl") + .HasColumnType("TEXT"); + + b.Property("Price") + .HasColumnType("TEXT"); + + b.Property("ProductBrandId") + .HasColumnType("INTEGER"); + + b.Property("ProductTypeId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("ProductBrandId"); + + b.HasIndex("ProductTypeId"); + + b.ToTable("Products"); + }); + + modelBuilder.Entity("Core.Entities.ProductBrand", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Name") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("ProductBrands"); + }); + + modelBuilder.Entity("Core.Entities.ProductType", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("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 + } + } +} diff --git a/Infrastructure/Data/Migrations/20220509234932_InitialCreate.cs b/Infrastructure/Data/Migrations/20220509234932_InitialCreate.cs new file mode 100644 index 0000000..3f2c51f --- /dev/null +++ b/Infrastructure/Data/Migrations/20220509234932_InitialCreate.cs @@ -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(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + Name = table.Column(type: "TEXT", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_ProductBrands", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "ProductTypes", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + Name = table.Column(type: "TEXT", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_ProductTypes", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Products", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + Name = table.Column(type: "TEXT", nullable: true), + Description = table.Column(type: "TEXT", nullable: true), + Price = table.Column(type: "TEXT", nullable: false), + PictureUrl = table.Column(type: "TEXT", nullable: true), + ProductTypeId = table.Column(type: "INTEGER", nullable: false), + ProductBrandId = table.Column(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"); + } + } +} diff --git a/Infrastructure/Data/Migrations/StoreContextModelSnapshot.cs b/Infrastructure/Data/Migrations/StoreContextModelSnapshot.cs new file mode 100644 index 0000000..f63ab94 --- /dev/null +++ b/Infrastructure/Data/Migrations/StoreContextModelSnapshot.cs @@ -0,0 +1,101 @@ +// +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("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Description") + .HasColumnType("TEXT"); + + b.Property("Name") + .HasColumnType("TEXT"); + + b.Property("PictureUrl") + .HasColumnType("TEXT"); + + b.Property("Price") + .HasColumnType("TEXT"); + + b.Property("ProductBrandId") + .HasColumnType("INTEGER"); + + b.Property("ProductTypeId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("ProductBrandId"); + + b.HasIndex("ProductTypeId"); + + b.ToTable("Products"); + }); + + modelBuilder.Entity("Core.Entities.ProductBrand", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Name") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("ProductBrands"); + }); + + modelBuilder.Entity("Core.Entities.ProductType", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("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 + } + } +} diff --git a/Infrastructure/Data/ProductRepository.cs b/Infrastructure/Data/ProductRepository.cs new file mode 100644 index 0000000..027c143 --- /dev/null +++ b/Infrastructure/Data/ProductRepository.cs @@ -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 GetProductByIdAsync(int id) + { + return await _context.Products.FindAsync(id); + } + + public async Task> GetProductsAync() + { + return await _context.Products.ToListAsync(); + } + } +} \ No newline at end of file diff --git a/Infrastucture/Data/StoreContext.cs b/Infrastructure/Data/StoreContext.cs similarity index 70% rename from Infrastucture/Data/StoreContext.cs rename to Infrastructure/Data/StoreContext.cs index 55af113..31080c9 100644 --- a/Infrastucture/Data/StoreContext.cs +++ b/Infrastructure/Data/StoreContext.cs @@ -9,5 +9,7 @@ namespace Infrastucture.Data { } public DbSet Products { get; set; } + public DbSet ProductBrands { get; set; } + public DbSet ProductTypes { get; set; } } } \ No newline at end of file diff --git a/Infrastucture/Infrastucture.csproj b/Infrastructure/Infrastructure.csproj similarity index 100% rename from Infrastucture/Infrastucture.csproj rename to Infrastructure/Infrastructure.csproj diff --git a/Infrastucture/Data/Migrations/20220509191604_InitialCreate.Designer.cs b/Infrastucture/Data/Migrations/20220509191604_InitialCreate.Designer.cs deleted file mode 100644 index e7dfbb0..0000000 --- a/Infrastucture/Data/Migrations/20220509191604_InitialCreate.Designer.cs +++ /dev/null @@ -1,37 +0,0 @@ -// -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("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("Name") - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.ToTable("Products"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/Infrastucture/Data/Migrations/20220509191604_InitialCreate.cs b/Infrastucture/Data/Migrations/20220509191604_InitialCreate.cs deleted file mode 100644 index 44f3174..0000000 --- a/Infrastucture/Data/Migrations/20220509191604_InitialCreate.cs +++ /dev/null @@ -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(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - Name = table.Column(type: "TEXT", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_Products", x => x.Id); - }); - } - - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "Products"); - } - } -} diff --git a/Infrastucture/Data/Migrations/StoreContextModelSnapshot.cs b/Infrastucture/Data/Migrations/StoreContextModelSnapshot.cs deleted file mode 100644 index 65f2453..0000000 --- a/Infrastucture/Data/Migrations/StoreContextModelSnapshot.cs +++ /dev/null @@ -1,35 +0,0 @@ -// -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("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("Name") - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.ToTable("Products"); - }); -#pragma warning restore 612, 618 - } - } -}