/
h0tnanny
/
DeliveryService
Обзор
Документация
Войти
/
h0tnanny
/
DeliveryService
Код
Запросы
0
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
DeliveryService.API/Controllers/ProductController.cs
128 строк
5 KB
Хатнянский Максим
Поправлены все связи EF Core
16 мар 2024, 12:47
16 мар 2024, 12:47
3434e4b
Код
Авторство
О чём код?
// using DeliveryService.API.Models; // using DeliveryService.Persistence; // using DeliveryService.Persistence.Models.Products; // using Microsoft.AspNetCore.Mvc; // using Microsoft.EntityFrameworkCore; // // namespace DeliveryService.API.Controllers; // // [ApiController] // [Route("[controller]")] // public sealed class ProductController(ILogger<ProductController> logger, DeliveryDbContext context) : ControllerBase // { // [HttpGet("{id:guid}")] // [ProducesResponseType<Product>(StatusCodes.Status200OK)] // public async Task<ActionResult> Get(Guid id) // { // var product = await context.Products.FirstOrDefaultAsync(x => x.Id == id); // if (product == null) return NotFound(); // // return Ok(product); // } // // [HttpGet("GetAll")] // [ProducesResponseType<IEnumerable<Product>>(StatusCodes.Status200OK)] // public async Task<ActionResult> GetAll() // { // var products = await context.Products.ToListAsync(); // return Ok(products); // } // // [HttpPost("Create")] // [ProducesResponseType<Product>(StatusCodes.Status200OK)] // public async Task<ActionResult> Create([FromBody] ProductModel product) // { // var seller = await context.Sellers.FirstOrDefaultAsync(x => x.Id == product.SellerId); // if (seller == null) return NotFound("Продавец не найден"); // // var productCategory = await context.ProductCategories.FirstOrDefaultAsync(x => x.Id == product.ProductCategoryId); // if (productCategory == null) return NotFound("Категория не найдена"); // // var newProduct = new Product() // { // Id = Guid.NewGuid(), // Name = product.Name, // SellerId = product.SellerId, // // Amount = product.Amount, // Price = product.Price, // Seller = seller, // }; // // // var newProductCategoryProduct = new ProductCategoryProduct() // // { // // ProductId = newProduct.Id, // // ProductCategoryId = productCategory.Id // // }; // // // // context.ProductCategoryProducts.Add(newProductCategoryProduct); // context.Products.Add(newProduct); // // await context.SaveChangesAsync(); // // return Ok(newProduct); // } // // [HttpPut("Update/{id:guid}")] // [ProducesResponseType<Product>(StatusCodes.Status200OK)] // public async Task<ActionResult> Update(Guid id, [FromBody] ProductModel product) // { // var productToUpdate = await context.Products.FirstOrDefaultAsync(x => x.Id == id); // if (productToUpdate == null) return NotFound(); // // productToUpdate.Name = product.Name; // // productToUpdate.Amount = product.Amount; // productToUpdate.Price = product.Price; // productToUpdate.SellerId = product.SellerId; // // context.Products.Update(productToUpdate); // await context.SaveChangesAsync(); // // return Ok(productToUpdate); // } // // [HttpPut("Update/Category/{id:guid}")] // [ProducesResponseType(StatusCodes.Status200OK)] // public async Task<ActionResult> UpdateCategory(Guid id, // [FromQuery] Guid oldCategoryId, [FromQuery] Guid newCategoryId) // { // var productToUpdate = await context.Products.FirstOrDefaultAsync(x => x.Id == id); // if (productToUpdate == null) return NotFound("Продукт не найден"); // // // var productCategoryProduct = await context.ProductCategoryProducts // // .FirstOrDefaultAsync(x => x.ProductId == productToUpdate.Id && // // x.ProductCategoryId == oldCategoryId); // // if (productCategoryProduct == null) return NotFound("Категория не найдена"); // // // productCategoryProduct.ProductCategoryId = newCategoryId; // // context.ProductCategoryProducts.Update(productCategoryProduct); // // await context.SaveChangesAsync(); // // return Ok(); // } // // [HttpPost("Add/Category/{id:guid}")] // [ProducesResponseType(StatusCodes.Status200OK)] // public async Task<ActionResult> AddCategory(Guid id, [FromQuery] Guid newCategoryId) // { // var category = await context.ProductCategories.FirstOrDefaultAsync(x => x.Id == newCategoryId); // if (category == null) return NotFound("Категория не найдена"); // // // var newCategoryProduct = new ProductCategoryProduct() { ProductId = id, ProductCategoryId = newCategoryId }; // // context.ProductCategoryProducts.Add(newCategoryProduct); // await context.SaveChangesAsync(); // // return Ok(); // } // // [HttpDelete("Delete/{id:guid}")] // public async Task<ActionResult> Delete(Guid id) // { // var product = await context.Products.FirstOrDefaultAsync(x => x.Id == id); // if (product == null) return NoContent(); // // context.Products.Remove(product); // await context.SaveChangesAsync(); // return NoContent(); // } // }