Last active
December 3, 2020 19:02
-
-
Save AbubakarSiddiq/79ccaa67c9665bad64e5651905c2615d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using AspNetCore5Microservices.Models; | |
using AspNetCore5Microservices.Repository; | |
using Microsoft.AspNetCore.Mvc; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 | |
namespace AspNetCore5Microservices.Controllers | |
{ | |
[Route("api/[controller]")] | |
[ApiController] | |
public class ProductController : ControllerBase | |
{ | |
private readonly IProductRepository _productRepository; | |
public ProductController(IProductRepository productRepository) | |
{ | |
_productRepository = productRepository; | |
} | |
[HttpGet] | |
public IActionResult GetProducts() | |
{ | |
var products = _productRepository.GetProducts(); | |
return new OkObjectResult(products); | |
} | |
[HttpGet("{id}")] | |
public IActionResult GetProductById(int id) | |
{ | |
var product = _productRepository.GetProductByID(id); | |
return new OkObjectResult(product); | |
} | |
[HttpPost] | |
public Product Add([FromBody] Product product) | |
{ | |
_productRepository.CreateProduct(product); | |
return product; | |
} | |
[HttpPut] | |
public IActionResult Edit([FromBody] Product product) | |
{ | |
if (product != null) | |
{ | |
_productRepository.EditProduct(product); | |
return new OkResult(); | |
} | |
return new NoContentResult(); | |
} | |
[HttpDelete("{id}")] | |
public IActionResult Delete(int id) | |
{ | |
_productRepository.DeleteProduct(id); | |
return new OkResult(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment