Skip to content
Merged
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
21 changes: 0 additions & 21 deletions WebApiLab.BLL/DTOs/DTO.cs

This file was deleted.

19 changes: 19 additions & 0 deletions WebApiLab.BLL/Dtos/Dtos.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using WebApiLab.Dal.Entities;

namespace WebApiLab.Bll.Dtos
{
public record Category(int Id, string Name);

public record Order(int Id, DateTime OrderDate);

public record Product
{
public int Id { get; init; }
public string Name { get; init; } = null!;
public int UnitPrice { get; init; }
public ShipmentRegion ShipmentRegion { get; init; }
public int CategoryId { get; init; }
public Category Category { get; init; } = null!;
public List<Order> Orders { get; init; } = null!;
}
}
23 changes: 11 additions & 12 deletions WebApiLab.BLL/Exceptions/EntityNotFoundException.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
using System;
namespace WebApiLab.Bll.Exceptions;

namespace WebApiLab.BLL
public class EntityNotFoundException : Exception
{
public class EntityNotFoundException : Exception
public EntityNotFoundException()
{
public EntityNotFoundException()
{
}
}

public EntityNotFoundException(string message) : base(message)
{
}
public EntityNotFoundException(string message)
: base(message)
{
}

public EntityNotFoundException(string message, Exception innerException) : base(message, innerException)
{
}
public EntityNotFoundException(string message, Exception innerException)
: base(message, innerException)
{
}
}
20 changes: 9 additions & 11 deletions WebApiLab.BLL/Interfaces/IProductService.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using System.Collections.Generic;
using WebApiLab.DAL.Entities;
using WebApiLab.Dal.Entities;

namespace WebApiLab.BLL
namespace WebApiLab.Bll.Interfaces;

public interface IProductService
{
public interface IProductService
{
Product GetProduct(int productId);
IEnumerable<Product> GetProducts();
Product InsertProduct(Product newProduct);
void UpdateProduct(int productId, Product updatedProduct);
void DeleteProduct(int productId);
}
public Product GetProduct(int productId);
public IEnumerable<Product> GetProducts();
public Product InsertProduct(Product newProduct);
public void UpdateProduct(int productId, Product updatedProduct);
public void DeleteProduct(int productId);
}
6 changes: 4 additions & 2 deletions WebApiLab.BLL/WebApiLab.BLL.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\WebApiLab.DAL\WebApiLab.DAL.csproj" />
<ProjectReference Include="..\WebApiLab.DAL\WebApiLab.Dal.csproj" />
</ItemGroup>

</Project>
87 changes: 87 additions & 0 deletions WebApiLab.DAL/AppDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Microsoft.Extensions.Logging;

using WebApiLab.Dal.Entities;

namespace WebApiLab.Dal;

public class AppDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=NEPTUN;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False")
.LogTo(Console.WriteLine, LogLevel.Debug);
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Entity<Category>()
.Property(c => c.Name)
.HasMaxLength(15)
.IsRequired();

modelBuilder.Entity<Category>().HasData(
new Category("Ital") { Id = 1 }
);

modelBuilder.Entity<Product>().HasData(
new Product("Sör") { Id = 1, UnitPrice = 50, CategoryId = 1, ShipmentRegion = ShipmentRegion.Asia },
new Product("Bor") { Id = 2, UnitPrice = 550, CategoryId = 1 },
new Product("Tej") { Id = 3, UnitPrice = 260, CategoryId = 1 },
new Product("Whiskey")
{
Id = 4,
UnitPrice = 960,
CategoryId = 1,
ShipmentRegion = ShipmentRegion.Australia
},
new Product("Rum")
{
Id = 5,
UnitPrice = 960,
CategoryId = 1,
ShipmentRegion = ShipmentRegion.Eu | ShipmentRegion.NorthAmerica
}
);

modelBuilder.Entity<Order>().HasData(
new Order { Id = 1, OrderDate = new DateTime(2019, 02, 01) }
);

modelBuilder.Entity<OrderItem>().HasData(
new OrderItem { Id = 1, OrderId = 1, ProductId = 1 },
new OrderItem { Id = 2, OrderId = 1, ProductId = 2 }
);

modelBuilder.Entity<Product>()
.HasMany(p => p.Orders)
.WithMany(o => o.Products)
.UsingEntity<OrderItem>(
j => j
.HasOne(oi => oi.Order)
.WithMany(o => o.OrderItems)
.HasForeignKey(oi => oi.OrderId)
.OnDelete(DeleteBehavior.Restrict),
j => j
.HasOne(oi => oi.Product)
.WithMany(p => p.ProductOrders)
.HasForeignKey(oi => oi.ProductId),
j =>
{
j.HasKey(oi => oi.Id);
});

modelBuilder
.Entity<Product>()
.Property(e => e.ShipmentRegion)
.HasConversion(new EnumToStringConverter<ShipmentRegion>());
}


public DbSet<Product> Products => Set<Product>();
public DbSet<Category> Categories => Set<Category>();
public DbSet<Order> Orders => Set<Order>();
}
22 changes: 10 additions & 12 deletions WebApiLab.DAL/Entities/Category.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WebApiLab.Dal.Entities;

namespace WebApiLab.DAL.Entities
public class Category
{
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
public int Id { get; set; }

public string Name { get; set; }

public ICollection<Product> Products { get; }
= new List<Product>();
public ICollection<Product> Products { get; } = new List<Product>();

public Category(string name)
{
Name = name;
}
}
21 changes: 6 additions & 15 deletions WebApiLab.DAL/Entities/Order.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WebApiLab.Dal.Entities;

namespace WebApiLab.DAL.Entities
public class Order
{
public class Order
{
public int Id { get; set; }
public int Id { get; set; }

public DateTime OrderDate { get; set; }
public DateTime OrderDate { get; set; }

public ICollection<OrderItem> OrderItems { get; }
= new List<OrderItem>();
public ICollection<OrderItem> OrderItems { get; } = new List<OrderItem>();

public ICollection<Product> Products { get; }
= new List<Product>();
}
public ICollection<Product> Products { get; } = new List<Product>();
}
23 changes: 8 additions & 15 deletions WebApiLab.DAL/Entities/OrderItem.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WebApiLab.Dal.Entities;

namespace WebApiLab.DAL.Entities
public class OrderItem
{
public class OrderItem
{
public int Id { get; set; }
public int Id { get; set; }

public int ProductId { get; set; }
public int ProductId { get; set; }

public Product Product { get; set; }
public Product Product { get; set; } = null!;

public int OrderId { get; set; }
public int OrderId { get; set; }

public Order Order { get; set; }
public Order Order { get; set; } = null!;

public int Quantity { get; set; }
public int Quantity { get; set; }

}
}
42 changes: 21 additions & 21 deletions WebApiLab.DAL/Entities/Product.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WebApiLab.DAL.Entities
using System.ComponentModel.DataAnnotations.Schema;

namespace WebApiLab.Dal.Entities;

public class Product
{
public class Product
{
public int Id { get; set; }
public int Id { get; set; }

[Column("ProductName")]
public string Name { get; set; }
public int UnitPrice { get; set; }
[Column("ProductName")]
public string Name { get; set; }

public int CategoryId { get; set; }
public Category Category { get; set; }
public int UnitPrice { get; set; }

public ICollection<OrderItem> ProductOrders { get; }
= new List<OrderItem>();
public int CategoryId { get; set; }

public ICollection<Order> Orders { get; } = new List<Order>();
public Category Category { get; set; } = null!;

public ShipmentRegion ShipmentRegion { get; set; }
}
public ICollection<OrderItem> ProductOrders { get; } = new List<OrderItem>();

public ICollection<Order> Orders { get; } = new List<Order>();

public ShipmentRegion ShipmentRegion { get; set; }

public Product(string name)
{
Name = name;
}
}

17 changes: 7 additions & 10 deletions WebApiLab.DAL/Entities/ShipmentRegion.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System;
namespace WebApiLab.Dal.Entities;

namespace WebApiLab.DAL.Entities
[Flags]
public enum ShipmentRegion
{
[Flags]
public enum ShipmentRegion
{
EU = 1,
NorthAmerica = 2,
Asia = 4,
Australia = 8
}
Eu = 1,
NorthAmerica = 2,
Asia = 4,
Australia = 8,
}
Loading