Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions HomeWork22/HomeWork22.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34525.116
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HomeWork22", "HomeWork22\HomeWork22.csproj", "{CFCCD84A-B798-4733-978A-B3098BB61F1F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{CFCCD84A-B798-4733-978A-B3098BB61F1F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CFCCD84A-B798-4733-978A-B3098BB61F1F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CFCCD84A-B798-4733-978A-B3098BB61F1F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CFCCD84A-B798-4733-978A-B3098BB61F1F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7D596900-7DD0-4AB6-A8B6-681469160DEE}
EndGlobalSection
EndGlobal
76 changes: 76 additions & 0 deletions HomeWork22/HomeWork22/App.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using HomeWork22.Dtos;
using HomeWork22.Models;
using HomeWork22.Services.Abstracts;

namespace HomeWork22
{
internal class App
{
private readonly IOrderService _orderService;
private readonly ICostumerService _costumerService;
private readonly IProductService _productService;
public App(IProductService productService, ICostumerService costumerService, IOrderService orderService)
{
_costumerService = costumerService;
_orderService = orderService;
_productService = productService;
}
public async Task StartAsync()
{
var idConstumer = await _costumerService.AddCostumerAsync("Max", "Babych");

var costumer = await _costumerService.GetCostumerAsync(idConstumer);

costumer = await _costumerService.UpdateCostumerAsync(idConstumer, "Oleg");

var idLimon = await _productService.AddProductAsync("Limon", 15.25);

var idOrange = await _productService.AddProductAsync("Orange", 10.50);

await _productService.UpdataProductAsync(idOrange, name: "Limon", price: 11);

var productLimon = await _productService.GetProductAsync(idLimon);

var productOrange = await _productService.GetProductAsync(idOrange);

List<OrderItem> orderItems = new List<OrderItem>()
{
new OrderItem()
{
Count = 5,
Product = productLimon,
ProductId = productLimon.Id,
},
new OrderItem()
{
Count = 10,
Product = productOrange,
ProductId = productOrange.Id,
}
};

var idOrder = await _orderService.AddOrderAsync(idConstumer, orderItems);

var order = await _orderService.GetOrderAsync(idOrder);

var request = new RequestPage()
{
Name = "Limon",
PageNamber = 1,
PageSize = 20,
PriceMax = 10,
PriceMin = 5,
};

await _productService.GetViewProductListAsync(request);

await _orderService.DeleteOrderAsync(idOrder);

await _costumerService.DeleteCostumerAsync(idConstumer);

await _productService.DeleteProduct(idLimon);

await _productService.DeleteProduct(idOrange);
}
}
}
27 changes: 27 additions & 0 deletions HomeWork22/HomeWork22/Datas/ApplicatDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using HomeWork22.Datas.Entities;
using HomeWork22.Datas.EntitiesConfigurations;
using Microsoft.EntityFrameworkCore;

namespace HomeWork22.Datas
{
internal class ApplicatDbContext : DbContext
{
public ApplicatDbContext(DbContextOptions<ApplicatDbContext> options)
: base(options)
{}

public DbSet<CostumerEntity> Costumers { get; set; } = null!;
public DbSet<OrderEntity> Orders { get; set; } = null!;
public DbSet<OrderItemEntity> OrderItems { get; set; } = null!;
public DbSet<ProductEntity> Products { get; set; } = null!;

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new CostumerEntityConfiguration());
modelBuilder.ApplyConfiguration(new OrderEntityConfiguration());
modelBuilder.ApplyConfiguration(new OrderItemEntityConguration());
modelBuilder.ApplyConfiguration(new ProductEntityConfiguration());
modelBuilder.UseHiLo();
}
}
}
31 changes: 31 additions & 0 deletions HomeWork22/HomeWork22/Datas/ApplicatDbContextFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;

namespace HomeWork22.Datas
{
internal class ApplicatDbContextFactory : IDesignTimeDbContextFactory<ApplicatDbContext>
{
public ApplicatDbContext CreateDbContext(string[] args)
{
var optionalBuilder = new DbContextOptionsBuilder<ApplicatDbContext>();

var builder = new ConfigurationBuilder();
builder.SetBasePath(Directory.GetCurrentDirectory());

var config = builder
.AddJsonFile("config.json")
.Build();

var connectionString = config.GetConnectionString("DefaultConnection");
optionalBuilder
.UseSqlServer(connectionString, option =>
option
.CommandTimeout((int)TimeSpan
.FromMinutes(10)
.TotalSeconds));

return new ApplicatDbContext(optionalBuilder.Options);
}
}
}
11 changes: 11 additions & 0 deletions HomeWork22/HomeWork22/Datas/Entities/CostumerEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace HomeWork22.Datas.Entities
{
internal class CostumerEntity
{
public int Id { get; set; }
public string? FirstName { get; set; }
public string? LastName { get; set; }

public ICollection<OrderEntity> Orders { get; set; } = new List<OrderEntity>();
}
}
11 changes: 11 additions & 0 deletions HomeWork22/HomeWork22/Datas/Entities/OrderEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace HomeWork22.Datas.Entities
{
internal class OrderEntity
{
public int Id { get; set; }
public int CostumerId { get; set; }
public CostumerEntity? Costumer { get; set; }

public ICollection<OrderItemEntity> OrderItems { get; set; } = new List<OrderItemEntity>();
}
}
12 changes: 12 additions & 0 deletions HomeWork22/HomeWork22/Datas/Entities/OrderItemEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace HomeWork22.Datas.Entities
{
internal class OrderItemEntity
{
public int Id { get; set; }
public int Count { get; set; }
public int OrderId { get; set; }
public OrderEntity? Order { get; set; }
public int ProductId { get; set; }
public ProductEntity? Product { get; set; }
}
}
11 changes: 11 additions & 0 deletions HomeWork22/HomeWork22/Datas/Entities/ProductEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace HomeWork22.Datas.Entities
{
internal class ProductEntity
{
public int Id { get; set; }
public string Name { get; set; } = null!;
public double Price { get; set; }

public ICollection<OrderItemEntity> OrderItems { get; set; } = new List<OrderItemEntity>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using HomeWork22.Datas.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace HomeWork22.Datas.EntitiesConfigurations
{
internal class CostumerEntityConfiguration : IEntityTypeConfiguration<CostumerEntity>
{
public void Configure(EntityTypeBuilder<CostumerEntity> builder)
{
builder.HasKey(x => x.Id);

builder
.Property(p => p.FirstName)
.HasMaxLength(50);

builder
.Property(p => p.LastName)
.IsRequired()
.HasMaxLength(50);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using HomeWork22.Datas.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace HomeWork22.Datas.EntitiesConfigurations
{
internal class OrderEntityConfiguration : IEntityTypeConfiguration<OrderEntity>
{
public void Configure(EntityTypeBuilder<OrderEntity> builder)
{
builder.HasKey(x => x.Id);

builder
.HasOne(o => o.Costumer)
.WithMany(m => m.Orders)
.HasForeignKey(o => o.CostumerId)
.OnDelete(DeleteBehavior.Cascade);

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using HomeWork22.Datas.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace HomeWork22.Datas.EntitiesConfigurations
{
internal class OrderItemEntityConguration : IEntityTypeConfiguration<OrderItemEntity>
{
public void Configure(EntityTypeBuilder<OrderItemEntity> builder)
{
builder.HasKey(x=>x.Id);

builder
.Property(x => x.Count)
.IsRequired();

builder
.HasOne(x => x.Order)
.WithMany(x => x.OrderItems)
.HasForeignKey(x=>x.OrderId)
.OnDelete(DeleteBehavior.Cascade);

builder
.HasOne(x=>x.Product)
.WithMany(x => x.OrderItems)
.HasForeignKey(x=>x.ProductId)
.OnDelete(DeleteBehavior.Cascade);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using HomeWork22.Datas.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace HomeWork22.Datas.EntitiesConfigurations
{
internal class ProductEntityConfiguration : IEntityTypeConfiguration<ProductEntity>
{
public void Configure(EntityTypeBuilder<ProductEntity> builder)
{
builder.HasKey(x=>x.Id);

builder
.Property(x => x.Name)
.IsRequired()
.HasMaxLength(100);

builder
.Property(x => x.Price)
.IsRequired();
}
}
}
8 changes: 8 additions & 0 deletions HomeWork22/HomeWork22/Dtos/PageDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace HomeWork22.Dtos
{
internal class PageDto
{
public int PageNamber = 1;
public int PageSize = 20;
}
}
10 changes: 10 additions & 0 deletions HomeWork22/HomeWork22/Dtos/RequestPage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace HomeWork22.Dtos
{
internal class RequestPage : PageDto
{
public string? Name { get; set; } = string.Empty;
public double PriceMax { get; set; } = 0;
public double PriceMin { get; set;} = 0;

}
}
26 changes: 26 additions & 0 deletions HomeWork22/HomeWork22/Dtos/ResponsPage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace HomeWork22.Dtos
{
internal class ResponsPage<T> : PageDto
{
public ResponsPage()
{ }

public int TotalItems => Items.Count;

public int TotalPages
{
get
{
return (int)Math.Ceiling((double)TotalItems / PageSize);
}
}

public List<T> Items { get; set; }
public ResponsPage(List<T> items, RequestPage request)
{
Items = items;
PageSize = request.PageSize;
PageNamber = request.PageNamber;
}
}
}
38 changes: 38 additions & 0 deletions HomeWork22/HomeWork22/HomeWork22.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<Compile Remove="bin\**" />
<EmbeddedResource Remove="bin\**" />
<None Remove="bin\**" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.3" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<None Update="config.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Loading