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 f47dc38..e69de29 100644 Binary files a/API/ecommerce.db and b/API/ecommerce.db differ diff --git a/API/ecommerce.db-wal b/API/ecommerce.db-wal deleted file mode 100644 index 583b3a8..0000000 Binary files a/API/ecommerce.db-wal and /dev/null differ diff --git a/Core/Entities/BaseEntity.cs b/Core/Entities/BaseEntity.cs new file mode 100644 index 0000000..200316b --- /dev/null +++ b/Core/Entities/BaseEntity.cs @@ -0,0 +1,7 @@ +namespace Core.Entities +{ + public class BaseEntity + { + public int Id { get; set; } + } +} \ No newline at end of file diff --git a/Core/Entities/Product.cs b/Core/Entities/Product.cs index c34544a..da98ecb 100644 --- a/Core/Entities/Product.cs +++ b/Core/Entities/Product.cs @@ -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; } } } \ No newline at end of file diff --git a/Core/Entities/ProductBrand.cs b/Core/Entities/ProductBrand.cs new file mode 100644 index 0000000..26ca920 --- /dev/null +++ b/Core/Entities/ProductBrand.cs @@ -0,0 +1,7 @@ +namespace Core.Entities +{ + public class ProductBrand : BaseEntity + { + public string Name { get; set; } + } +} \ No newline at end of file diff --git a/Core/Entities/ProductType.cs b/Core/Entities/ProductType.cs new file mode 100644 index 0000000..05d5fa2 --- /dev/null +++ b/Core/Entities/ProductType.cs @@ -0,0 +1,7 @@ +namespace Core.Entities +{ + public class ProductType : BaseEntity + { + public string Name { get; set; } + } +} \ No newline at end of file diff --git a/Core/Interfaces/iProductRepository.cs b/Core/Interfaces/iProductRepository.cs new file mode 100644 index 0000000..5e63b17 --- /dev/null +++ b/Core/Interfaces/iProductRepository.cs @@ -0,0 +1,10 @@ +using Core.Entities; + +namespace Core.Interfaces +{ + public interface iProductRepository + { + Task 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 - } - } -}