From 4857c066300255312d1e1736f2a03c256c6a9501 Mon Sep 17 00:00:00 2001 From: Serghei Cebotari Date: Tue, 14 Nov 2023 12:00:20 +0300 Subject: [PATCH] Add POST methods --- MindBoxApi/Controllers/CategoryController.cs | 20 +++++++++++++++++++- MindBoxApi/Controllers/ItemController.cs | 17 +++++++++++++++++ MindBoxApi/Controllers/ProductController.cs | 17 +++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/MindBoxApi/Controllers/CategoryController.cs b/MindBoxApi/Controllers/CategoryController.cs index 960a3cb..dd706f4 100644 --- a/MindBoxApi/Controllers/CategoryController.cs +++ b/MindBoxApi/Controllers/CategoryController.cs @@ -13,7 +13,8 @@ public class CategoryController : ControllerBase _helper = helper; } [HttpGet] - public IEnumerable GetCategories() { + public IEnumerable GetCategories() + { var command = _helper.Connection.CreateCommand(); command.CommandText = @" @@ -31,4 +32,21 @@ public class CategoryController : ControllerBase } } } + + [HttpPost] + public IActionResult PostCategory([FromBody] Category category) + { + var command = _helper.Connection.CreateCommand(); + command.CommandText = + $"INSERT INTO categories (category_id, category_name) VALUES ({category.Id}, '{category.Name}')"; + try + { + command.ExecuteNonQuery(); + return Ok(); + } + catch (Exception ex) + { + return BadRequest(ex.Message); + } + } } diff --git a/MindBoxApi/Controllers/ItemController.cs b/MindBoxApi/Controllers/ItemController.cs index 4e1f136..df51dd8 100644 --- a/MindBoxApi/Controllers/ItemController.cs +++ b/MindBoxApi/Controllers/ItemController.cs @@ -37,4 +37,21 @@ public class ItemController : ControllerBase } } } + + [HttpPost] + public IActionResult PostItem([FromQuery] int? productId, [FromQuery] int? categoryId) + { + var command = _helper.Connection.CreateCommand(); + command.CommandText = + $"INSERT INTO grocery_store (product_id, category_id) VALUES ({productId}, {categoryId})"; + try + { + command.ExecuteNonQuery(); + return Ok(); + } + catch (Exception ex) + { + return BadRequest(ex.Message); + } + } } diff --git a/MindBoxApi/Controllers/ProductController.cs b/MindBoxApi/Controllers/ProductController.cs index f4f3c3f..bcf3964 100644 --- a/MindBoxApi/Controllers/ProductController.cs +++ b/MindBoxApi/Controllers/ProductController.cs @@ -32,4 +32,21 @@ public class ProductController : ControllerBase } } } + + [HttpPost] + public IActionResult PostProduct([FromBody] Product product) + { + var command = _helper.Connection.CreateCommand(); + command.CommandText = + $"INSERT INTO products (product_id, product_name) VALUES ({product.Id}, '{product.Name}')"; + try + { + command.ExecuteNonQuery(); + return Ok(); + } + catch (Exception ex) + { + return BadRequest(ex.Message); + } + } }