From abac84ccf26ee4bc7b3450e9f36a43dbea84294b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Ry=C5=BCek?= Date: Fri, 7 Jun 2024 19:53:29 +0200 Subject: [PATCH] =?UTF-8?q?dodanie=20endpointu=20zwracaj=C4=85cego=20produ?= =?UTF-8?q?kt=20po=20nazwie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Controllers/ProductController.cs | 11 +++++++++++ nHIbernate/Products/ProductCRUD.cs | 10 ++++++++++ 2 files changed, 21 insertions(+) diff --git a/Controllers/ProductController.cs b/Controllers/ProductController.cs index 1d5546f..5cf7885 100644 --- a/Controllers/ProductController.cs +++ b/Controllers/ProductController.cs @@ -45,6 +45,17 @@ namespace FirmTracker_Server.Controllers return Ok(product); } + [HttpGet("name/{name}")] + [ProducesResponseType(200)] + [ProducesResponseType(404)] + public IActionResult GetProductByName(string name) + { + var product = _productCrud.GetProductByName(name); + if (product ==null) + return NotFound(); + return Ok(product); + } + // PUT: api/Products/5 [HttpPut("{id}")] [ProducesResponseType(200)] // Created diff --git a/nHIbernate/Products/ProductCRUD.cs b/nHIbernate/Products/ProductCRUD.cs index e14b12b..d07df8f 100644 --- a/nHIbernate/Products/ProductCRUD.cs +++ b/nHIbernate/Products/ProductCRUD.cs @@ -74,6 +74,16 @@ namespace FirmTracker_Server.nHibernate.Products } } + public Product GetProductByName(string productName) + { + using (var session = SessionFactory.OpenSession()) + { + var query = session.CreateQuery("from Product where Name = :name"); + query.SetParameter("name", productName); + return query.UniqueResult(); + } + } + public void UpdateProduct(Product product) { using (var session = SessionFactory.OpenSession())