FirmTracker-Server/Controllers/ProductController.cs

217 lines
7.5 KiB
C#
Raw Normal View History

/*
* This file is part of FirmTracker - Server.
*
* FirmTracker - Server is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FirmTracker - Server is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with FirmTracker - Server. If not, see <https://www.gnu.org/licenses/>.
*/
using FirmTracker_Server.nHibernate.Products;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authorization.Infrastructure;
using Microsoft.AspNetCore.Mvc;
2024-06-16 23:02:04 +02:00
using System;
2024-04-15 22:15:29 +02:00
namespace FirmTracker_Server.Controllers
{
[Route("api/[controller]")]
[ApiController]
[Authorize]
2024-04-15 22:15:29 +02:00
public class ProductsController : ControllerBase
{
private readonly ProductCRUD _productCrud;
public ProductsController()
{
_productCrud = new ProductCRUD();
}
// POST: api/Products
2024-04-26 21:24:17 +02:00
/// <summary>
/// Creates a new product.
/// </summary>
2024-04-15 22:15:29 +02:00
[HttpPost]
2024-04-26 21:24:17 +02:00
[ProducesResponseType(200)] // Created
[ProducesResponseType(400)] // Bad Request
2024-10-26 21:17:52 +02:00
[Authorize(Roles = Roles.Admin)]
2024-04-15 22:15:29 +02:00
public IActionResult CreateProduct([FromBody] Product product)
{
try
{
2024-06-17 17:22:34 +02:00
if (product.Type != 0 && product.Type != 1)
{
throw new InvalidOperationException("Kategoria produktu musi być ustawiona na 0 lub 1.");
}
if (product.Type == 0 && product.Availability != 0)
{
throw new InvalidOperationException("Dostępność usługi musi być ustawiona na 0.");
}
if (product.Type == 1 && product.Availability < 0)
{
throw new InvalidOperationException("Dostępność towaru nie może być ujemna.");
}
if (product.Price < 0)
{
throw new InvalidOperationException("Produkt nie może posiadać ujemnej ceny.");
}
2024-12-12 19:48:08 +01:00
var productByName = _productCrud.GetProductByName(product.Name);
if (productByName != null)
{
throw new InvalidOperationException("Produkt o podanej nazwie już istnieje.");
}
2024-06-17 17:22:34 +02:00
2024-04-15 22:15:29 +02:00
_productCrud.AddProduct(product);
return CreatedAtAction("GetProduct", new { id = product.Id }, product);
}
2024-06-17 17:22:34 +02:00
catch (InvalidOperationException ioe)
{
return BadRequest(ioe.Message);
}
catch (Exception ex)
2024-04-15 22:15:29 +02:00
{
return BadRequest(ex.Message);
}
}
// GET: api/Products/5
[HttpGet("{id}")]
2024-04-26 21:24:17 +02:00
[ProducesResponseType(200)] // Created
[ProducesResponseType(400)] // Bad Request
2024-10-26 21:17:52 +02:00
[Authorize(Roles=Roles.Admin+","+Roles.User)]
2024-04-15 22:15:29 +02:00
public IActionResult GetProduct(int id)
{
var product = _productCrud.GetProduct(id);
if (product == null)
return NotFound();
return Ok(product);
}
[HttpGet("name/{name}")]
[ProducesResponseType(200)]
[ProducesResponseType(404)]
2024-10-26 21:17:52 +02:00
[Authorize(Roles = Roles.Admin + "," + Roles.User)]
public IActionResult GetProductByName(string name)
{
var product = _productCrud.GetProductByName(name);
if (product ==null)
return NotFound();
return Ok(product);
}
2024-04-15 22:15:29 +02:00
// PUT: api/Products/5
[HttpPut("{id}")]
2024-04-26 21:24:17 +02:00
[ProducesResponseType(200)] // Created
[ProducesResponseType(400)] // Bad Request
2024-10-26 21:17:52 +02:00
[Authorize(Roles = Roles.Admin + "," + Roles.User)]
2024-04-15 22:15:29 +02:00
public IActionResult UpdateProduct(int id, [FromBody] Product product)
{
try
{
2024-06-17 17:22:34 +02:00
if (id != product.Id)
throw new InvalidOperationException("ID produktu nie zgadza się.");
if (product.Type != 0 && product.Type != 1)
{
throw new InvalidOperationException("Kategoria produktu musi być ustawiona na 0 lub 1.");
}
if (product.Type == 0 && product.Availability != 0)
{
throw new InvalidOperationException("Dostępność usługi musi być ustawiona na 0.");
}
if (product.Type == 1 && product.Availability < 0)
{
throw new InvalidOperationException("Dostępność towaru nie może być ujemna.");
}
if (product.Price < 0)
{
throw new InvalidOperationException("Produkt nie może posiadać ujemnej ceny.");
}
2024-04-15 22:15:29 +02:00
_productCrud.UpdateProduct(product);
return NoContent();
}
2024-06-17 17:22:34 +02:00
catch (InvalidOperationException ioe)
{
return BadRequest(ioe.Message);
}
catch (Exception ex)
2024-04-15 22:15:29 +02:00
{
return BadRequest(ex.Message);
}
}
// DELETE: api/Products/5
[HttpDelete("{id}")]
2024-04-26 21:24:17 +02:00
[ProducesResponseType(200)] // Created
[ProducesResponseType(400)] // Bad Request
2024-10-26 21:17:52 +02:00
[Authorize(Roles = Roles.Admin)]
2024-04-15 22:15:29 +02:00
public IActionResult DeleteProduct(int id)
{
try
{
_productCrud.DeleteProduct(id);
return NoContent();
}
2024-06-17 22:17:59 +02:00
catch (InvalidOperationException ioe)
{
return BadRequest($"{ioe.Message}");
}
2024-04-15 22:15:29 +02:00
catch (System.Exception ex)
{
return NotFound(ex.Message);
}
}
// GET: api/Products
[HttpGet]
2024-04-26 21:24:17 +02:00
[ProducesResponseType(200)] // Created
[ProducesResponseType(400)] // Bad Request
[Authorize(Roles = Roles.Admin + "," + Roles.User)]
2024-04-15 22:15:29 +02:00
public IActionResult GetAllProducts()
{
var products = _productCrud.GetAllProducts();
return Ok(products);
}
[HttpPost("CalculateTotalPrice")]
[ProducesResponseType(200)]
[ProducesResponseType(400)]
public IActionResult CalculateTotalPrice([FromBody] ProductOrder[] orders)
{
decimal totalPrice = 0;
decimal discount = 0;
foreach (var order in orders)
{
discount = order.Discount;
var product = _productCrud.GetProduct(order.ProductId);
if (product == null)
{
2024-06-14 13:46:20 +02:00
return BadRequest($"Nie znaleziono produktu o ID {order.ProductId}.");
}
totalPrice += product.Price * order.Quantity;
}
// Apply discount
decimal discountAmount = totalPrice * (discount / 100);
totalPrice -= discountAmount;
return Ok(new { TotalPrice = totalPrice });
}
public class ProductOrder
{
public int ProductId { get; set; }
public int Quantity { get; set; }
public decimal Discount { get; set; }
}
2024-04-15 22:15:29 +02:00
}
}