-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from mahmmoudkinawy/master
Refactoring and Upgrading to .net 5.0
- Loading branch information
Showing
11 changed files
with
127 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Supermarket.API.Controllers | ||
{ | ||
[Route("/api/[controller]")] | ||
[Produces("application/json")] | ||
[ApiController] | ||
public class BaseApiController : ControllerBase | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,19 @@ | ||
using System.Reflection; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.InMemory.ValueGeneration.Internal; | ||
using Supermarket.API.Domain.Models; | ||
|
||
namespace Supermarket.API.Persistence.Contexts | ||
{ | ||
public class AppDbContext : DbContext | ||
{ | ||
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { } | ||
public DbSet<Category> Categories { get; set; } | ||
public DbSet<Product> Products { get; set; } | ||
|
||
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { } | ||
|
||
protected override void OnModelCreating(ModelBuilder builder) | ||
{ | ||
base.OnModelCreating(builder); | ||
|
||
builder.Entity<Category>().ToTable("Categories"); | ||
builder.Entity<Category>().HasKey(p => p.Id); | ||
builder.Entity<Category>().Property(p => p.Id).IsRequired().ValueGeneratedOnAdd();//.HasValueGenerator<InMemoryIntegerValueGenerator<int>>(); | ||
builder.Entity<Category>().Property(p => p.Name).IsRequired().HasMaxLength(30); | ||
builder.Entity<Category>().HasMany(p => p.Products).WithOne(p => p.Category).HasForeignKey(p => p.CategoryId); | ||
|
||
builder.Entity<Category>().HasData | ||
( | ||
new Category { Id = 100, Name = "Fruits and Vegetables" }, // Id set manually due to in-memory provider | ||
new Category { Id = 101, Name = "Dairy" } | ||
); | ||
|
||
builder.Entity<Product>().ToTable("Products"); | ||
builder.Entity<Product>().HasKey(p => p.Id); | ||
builder.Entity<Product>().Property(p => p.Id).IsRequired().ValueGeneratedOnAdd(); | ||
builder.Entity<Product>().Property(p => p.Name).IsRequired().HasMaxLength(50); | ||
builder.Entity<Product>().Property(p => p.QuantityInPackage).IsRequired(); | ||
builder.Entity<Product>().Property(p => p.UnitOfMeasurement).IsRequired(); | ||
|
||
builder.Entity<Product>().HasData | ||
( | ||
new Product | ||
{ | ||
Id = 100, | ||
Name = "Apple", | ||
QuantityInPackage = 1, | ||
UnitOfMeasurement = EUnitOfMeasurement.Unity, | ||
CategoryId = 100 | ||
}, | ||
new Product | ||
{ | ||
Id = 101, | ||
Name = "Milk", | ||
QuantityInPackage = 2, | ||
UnitOfMeasurement = EUnitOfMeasurement.Liter, | ||
CategoryId = 101, | ||
} | ||
); | ||
builder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly()); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Supermarket.API/Persistence/Contexts/Configurations/CategoryConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
using Supermarket.API.Domain.Models; | ||
|
||
namespace Supermarket.API.Persistence.Contexts.Configurations | ||
{ | ||
public class CategoryConfiguration : IEntityTypeConfiguration<Category> | ||
{ | ||
public void Configure(EntityTypeBuilder<Category> builder) | ||
{ | ||
builder.ToTable("Categories"); | ||
builder.HasKey(p => p.Id); | ||
builder.Property(p => p.Id).IsRequired().ValueGeneratedOnAdd();//.HasValueGenerator<InMemoryIntegerValueGenerator<int>>(); | ||
builder.Property(p => p.Name).IsRequired().HasMaxLength(30); | ||
builder.HasMany(p => p.Products).WithOne(p => p.Category).HasForeignKey(p => p.CategoryId); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/Supermarket.API/Persistence/Contexts/Configurations/ProductConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
using Supermarket.API.Domain.Models; | ||
|
||
namespace Supermarket.API.Persistence.Contexts.Configurations | ||
{ | ||
public class ProductConfiguration : IEntityTypeConfiguration<Product> | ||
{ | ||
public void Configure(EntityTypeBuilder<Product> builder) | ||
{ | ||
builder.ToTable("Products"); | ||
builder.HasKey(p => p.Id); | ||
builder.Property(p => p.Id).IsRequired().ValueGeneratedOnAdd(); | ||
builder.Property(p => p.Name).IsRequired().HasMaxLength(50); | ||
builder.Property(p => p.QuantityInPackage).IsRequired(); | ||
builder.Property(p => p.UnitOfMeasurement).IsRequired(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Supermarket.API.Domain.Models; | ||
|
||
namespace Supermarket.API.Persistence.Contexts | ||
{ | ||
public static class SeedData | ||
{ | ||
public static async Task Seed(AppDbContext context) | ||
{ | ||
var products = new List<Product> | ||
{ | ||
new Product | ||
{ | ||
Id = 100, | ||
Name = "Apple", | ||
QuantityInPackage = 1, | ||
UnitOfMeasurement = EUnitOfMeasurement.Unity, | ||
CategoryId = 100 | ||
}, | ||
new Product | ||
{ | ||
Id = 101, | ||
Name = "Milk", | ||
QuantityInPackage = 2, | ||
UnitOfMeasurement = EUnitOfMeasurement.Liter, | ||
CategoryId = 101, | ||
} | ||
}; | ||
|
||
var categories = new List<Category> | ||
{ | ||
new Category { Id = 100, Name = "Fruits and Vegetables" }, // Id set manually due to in-memory provider | ||
new Category { Id = 101, Name = "Dairy" } | ||
}; | ||
|
||
context.Products.AddRange(products); | ||
context.Categories.AddRange(categories); | ||
|
||
await context.SaveChangesAsync(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
<TargetFramework>net5.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> | ||
<DocumentationFile>bin\Debug\netcoreapp3.1\Supermarket.API.xml</DocumentationFile> | ||
<OutputPath>bin\Debug\netcoreapp3.1\</OutputPath> | ||
<DocumentationFile>bin\Debug\net5.0\Supermarket.API.xml</DocumentationFile> | ||
<OutputPath>bin\Debug\net5.0</OutputPath> | ||
<NoWarn>1701;1702;1591;</NoWarn> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AutoMapper" Version="9.0.0" /> | ||
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.0" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.0" /> | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc5" /> | ||
<PackageReference Include="AutoMapper" Version="10.1.1" /> | ||
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.4" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="5.0.4" /> | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Debug", | ||
"System": "Information", | ||
"Microsoft": "Information" | ||
"Default": "Information", | ||
"Microsoft": "Information", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
} | ||
} | ||
} |