Created
September 17, 2019 10:55
-
-
Save debugmodedotnet/e1193a381d2745ed6ab63d18d8c22f16 to your computer and use it in GitHub Desktop.
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.Data; | |
using System.Data.Entity; | |
using System.Data.Entity.Infrastructure; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Http; | |
using System.Web.Http; | |
using System.Web.Http.Description; | |
using ProductApi.Infrastructure; | |
using ProductApi.core.Entities; | |
using ProductApi.core.Repositories; | |
namespace WebApplication1.Controllers | |
{ | |
public class ProductsController : ApiController | |
{ | |
private IProductRepository db; | |
public ProductsController(IProductRepository repository) | |
{ | |
db = repository; | |
} | |
//private ProductContext db = new ProductContext(); | |
// GET: api/Products | |
public IEnumerable<Product> GetProducts() | |
{ | |
return db.GetProducts(); | |
} | |
// GET: api/Products/5 | |
[ResponseType(typeof(Product))] | |
public IHttpActionResult GetProduct(string id) | |
{ | |
Product product = db.GetById(id); | |
if (product == null) | |
{ | |
return NotFound(); | |
} | |
return Ok(product); | |
} | |
// PUT: api/Products/5 | |
[ResponseType(typeof(void))] | |
public IHttpActionResult PutProduct(string id, Product product) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return BadRequest(ModelState); | |
} | |
if (id != product.ProductID) | |
{ | |
return BadRequest(); | |
} | |
//db.Entry(product).State = EntityState.Modified; | |
db.Edit(product); | |
return StatusCode(HttpStatusCode.NoContent); | |
} | |
// POST: api/Products | |
[ResponseType(typeof(Product))] | |
public IHttpActionResult PostProduct(Product product) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return BadRequest(ModelState); | |
} | |
db.Add(product); | |
return CreatedAtRoute("DefaultApi", new { id = product.ProductID }, product); | |
} | |
// DELETE: api/Products/5 | |
[ResponseType(typeof(Product))] | |
public IHttpActionResult DeleteProduct(string id) | |
{ | |
Product product = db.GetById(id); | |
if (product == null) | |
{ | |
return NotFound(); | |
} | |
db.Delete(id); | |
return Ok(product); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment