Last active
February 13, 2019 19:29
-
-
Save demonguru18/0dd7ab1162eb4b1b0a8ede28fb5348ed to your computer and use it in GitHub Desktop.
This is the Product Controller Class for Managing Product related Actions.
This file contains 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 System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Authorization; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.EntityFrameworkCore; | |
using NG_Core_Auth.Data; | |
using NG_Core_Auth.Models; | |
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 | |
namespace NG_Core_Auth.Controllers | |
{ | |
[Route("api/[controller]")] | |
public class ProductController : Controller | |
{ | |
private readonly ApplicationDbContext _db; | |
public ProductController(ApplicationDbContext db) | |
{ | |
_db = db; | |
} | |
// GET: api/values | |
[HttpGet("[action]")] | |
[Authorize(Policy = "RequireLoggedIn")] | |
public IActionResult GetProducts() | |
{ | |
return Ok(_db.Products.ToList()); | |
} | |
[HttpPost("[action]")] | |
[Authorize(Policy = "RequireAdministratorRole")] | |
public async Task<IActionResult> AddProduct([FromBody] ProductModel formdata) | |
{ | |
var newproduct = new ProductModel | |
{ | |
Name = formdata.Name, | |
ImageUrl = formdata.ImageUrl, | |
Description = formdata.Description, | |
OutOfStock = formdata.OutOfStock, | |
Price = formdata.Price | |
}; | |
await _db.Products.AddAsync(newproduct); | |
await _db.SaveChangesAsync(); | |
return Ok(new JsonResult("The Product was Added Successfully")); | |
} | |
[HttpPut("[action]/{id}")] | |
[Authorize(Policy = "RequireAdministratorRole")] | |
public async Task<IActionResult> UpdateProduct([FromRoute] int id, [FromBody] ProductModel formdata) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return BadRequest(ModelState); | |
} | |
var findProduct = _db.Products.FirstOrDefault(p => p.ProductId == id); | |
if (findProduct == null) | |
{ | |
return NotFound(); | |
} | |
// If the product was found | |
findProduct.Name = formdata.Name; | |
findProduct.Description = formdata.Description; | |
findProduct.ImageUrl = formdata.ImageUrl; | |
findProduct.OutOfStock = formdata.OutOfStock; | |
findProduct.Price = formdata.Price; | |
_db.Entry(findProduct).State = EntityState.Modified; | |
await _db.SaveChangesAsync(); | |
return Ok(new JsonResult("The Product with id " + id + " is updated")); | |
} | |
[HttpDelete("[action]/{id}")] | |
[Authorize(Policy = "RequireAdministratorRole")] | |
public async Task<IActionResult> DeleteProduct([FromRoute] int id) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return BadRequest(ModelState); | |
} | |
// find the product | |
var findProduct = await _db.Products.FindAsync(id); | |
if (findProduct == null) | |
{ | |
return NotFound(); | |
} | |
_db.Products.Remove(findProduct); | |
await _db.SaveChangesAsync(); | |
// Finally return the result to client | |
return Ok(new JsonResult("The Product with id " + id + " is Deleted.")); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment