Merge branch 'feature/CreatingProductEnpoints' into develop
This commit is contained in:
@@ -14,7 +14,7 @@ public class CreatePriceDto
|
||||
public int SupplierDeliveryDelay { get; set; }
|
||||
|
||||
public int ProductId { get; set; }
|
||||
public int ProductReferences { get; set; }
|
||||
public string? ProductReferences { get; set; }
|
||||
public string? ProductName { get; set; }
|
||||
public decimal ProductDuration {get; set;}
|
||||
public decimal ProductCaliber { get; set; }
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
public class PatchPriceSellingPriceDto
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public int ProductId { get; set; }
|
||||
public int SupplierId { get; set; }
|
||||
public decimal SellingPrice { get; set; }
|
||||
}
|
||||
83
PyroFetes/Endpoints/Price/CreatePriceEndpoint.cs
Normal file
83
PyroFetes/Endpoints/Price/CreatePriceEndpoint.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.DTO.Price.Request;
|
||||
using PyroFetes.DTO.Price.Response;
|
||||
|
||||
namespace PyroFetes.Endpoints.Price;
|
||||
|
||||
public class CreatePriceEndpoint(PyroFetesDbContext database) : Endpoint<CreatePriceDto, GetPriceDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Post("/api/prices");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CreatePriceDto req, CancellationToken ct)
|
||||
{
|
||||
var price = await database.Prices.SingleOrDefaultAsync(p => p.ProductId == req.ProductId && p.SupplierId == req.SupplierId, ct);
|
||||
if (price == null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
var supplier = await database.Suppliers.FirstOrDefaultAsync(s => s.Id == req.SupplierId, ct);
|
||||
|
||||
var product = await database.Products.SingleOrDefaultAsync(p => p.Id == req.ProductId, ct);
|
||||
if (product == null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
if (supplier == null)
|
||||
{
|
||||
supplier = new Models.Supplier()
|
||||
{
|
||||
Name = req.SupplierName,
|
||||
Email = req.SupplierEmail,
|
||||
Phone = req.SupplierPhone,
|
||||
Address = req.SupplierAddress,
|
||||
City = req.SupplierCity,
|
||||
ZipCode = req.SupplierZipCode,
|
||||
DeliveryDelay = req.SupplierDeliveryDelay
|
||||
|
||||
};
|
||||
database.Suppliers.Add(supplier);
|
||||
await database.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
var priceAdded = new Models.Price()
|
||||
{
|
||||
SellingPrice = price.SellingPrice,
|
||||
SupplierId = supplier.Id,
|
||||
ProductId = product.Id,
|
||||
};
|
||||
database.Prices.Add(priceAdded);
|
||||
await database.SaveChangesAsync(ct);
|
||||
|
||||
var productAdded = new Models.Product()
|
||||
{
|
||||
Reference = req.ProductReferences,
|
||||
Name = req.ProductName,
|
||||
Duration = req.ProductDuration,
|
||||
Caliber = req.ProductCaliber,
|
||||
ApprovalNumber = req.ProductApprovalNumber,
|
||||
Weight = req.ProductWeight,
|
||||
Nec = req.ProductNec,
|
||||
Image = req.ProductImage,
|
||||
Link = req.ProductLink,
|
||||
MinimalQuantity = req.ProductMinimalQuantity
|
||||
};
|
||||
database.Products.Add(productAdded);
|
||||
await database.SaveChangesAsync(ct);
|
||||
|
||||
var responseDto = new GetPriceDto()
|
||||
{
|
||||
|
||||
};
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
}
|
||||
}
|
||||
36
PyroFetes/Endpoints/Price/DeletePriceEndpoint.cs
Normal file
36
PyroFetes/Endpoints/Price/DeletePriceEndpoint.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace PyroFetes.Endpoints.QuotationProduct;
|
||||
|
||||
public class DeletePriceRequest
|
||||
{
|
||||
public int ProductId { get; set; }
|
||||
public int SupplierId { get; set; }
|
||||
}
|
||||
|
||||
public class DeletePriceEndpoint(PyroFetesDbContext database) : Endpoint<DeletePriceRequest>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Delete("/api/prices/{@ProductId}/{@SupplierId}", x => new {x.ProductId, x.SupplierId});
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(DeletePriceRequest req, CancellationToken ct)
|
||||
{
|
||||
var price = await database.Prices
|
||||
.SingleOrDefaultAsync(p => p.ProductId == req.ProductId && p.SupplierId == req.SupplierId, ct);
|
||||
|
||||
if (price == null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
database.Prices.Remove(price);
|
||||
await database.SaveChangesAsync(ct);
|
||||
|
||||
await Send.NoContentAsync(ct);
|
||||
}
|
||||
}
|
||||
36
PyroFetes/Endpoints/Price/PatchPriceEndpoint.cs
Normal file
36
PyroFetes/Endpoints/Price/PatchPriceEndpoint.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.DTO.Price.Request;
|
||||
using PyroFetes.DTO.Price.Response;
|
||||
|
||||
namespace PyroFetes.Endpoints.Price;
|
||||
|
||||
public class PatchPriceEndpoint(PyroFetesDbContext database) : Endpoint<PatchPriceSellingPriceDto, GetPriceDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Patch("/api/prices/{@ProductId}/{@SupplierId}/SellingPrice", x => new { x.ProductId, x.SupplierId });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(PatchPriceSellingPriceDto req, CancellationToken ct)
|
||||
{
|
||||
var price = await database.Prices.SingleOrDefaultAsync(p => p.ProductId == req.ProductId && p.SupplierId == req.SupplierId, ct);
|
||||
if (price == null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
price.SellingPrice = req.SellingPrice;
|
||||
await database.SaveChangesAsync(ct);
|
||||
|
||||
GetPriceDto responseDto = new()
|
||||
{
|
||||
ProductId = price.ProductId,
|
||||
SupplierId = price.SupplierId,
|
||||
SellingPrice = price.SellingPrice
|
||||
};
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user