Archived
1
0

Add POST methods

This commit is contained in:
Serghei Cebotari 2023-11-14 12:00:20 +03:00
parent d3c1749360
commit 4857c06630
3 changed files with 53 additions and 1 deletions

View File

@ -13,7 +13,8 @@ public class CategoryController : ControllerBase
_helper = helper; _helper = helper;
} }
[HttpGet] [HttpGet]
public IEnumerable<Category> GetCategories() { public IEnumerable<Category> GetCategories()
{
var command = _helper.Connection.CreateCommand(); var command = _helper.Connection.CreateCommand();
command.CommandText = 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);
}
}
} }

View File

@ -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);
}
}
} }

View File

@ -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);
}
}
} }