| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- 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 APICancelar.Entities;
- namespace APICancelar.Controllers
- {
- public class ProductsController : ApiController
- {
- private Cancelar2024Entities db = new Cancelar2024Entities();
- // GET: api/Products
- public IQueryable<Product> GetProducts()
- {
- return db.Products;
- }
- // GET: api/Products/5
- [ResponseType(typeof(Product))]
- public IHttpActionResult GetProduct(string id)
- {
- Product product = db.Products.Find(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.ProductArticleNumber)
- {
- return BadRequest();
- }
- db.Entry(product).State = EntityState.Modified;
- try
- {
- db.SaveChanges();
- }
- catch (DbUpdateConcurrencyException)
- {
- if (!ProductExists(id))
- {
- return NotFound();
- }
- else
- {
- throw;
- }
- }
- return StatusCode(HttpStatusCode.NoContent);
- }
- // POST: api/Products
- [ResponseType(typeof(Product))]
- public IHttpActionResult PostProduct(Product product)
- {
- if (!ModelState.IsValid)
- {
- return BadRequest(ModelState);
- }
- db.Products.Add(product);
- try
- {
- db.SaveChanges();
- }
- catch (DbUpdateException)
- {
- if (ProductExists(product.ProductArticleNumber))
- {
- return Conflict();
- }
- else
- {
- throw;
- }
- }
- return CreatedAtRoute("DefaultApi", new { id = product.ProductArticleNumber }, product);
- }
- // DELETE: api/Products/5
- [ResponseType(typeof(Product))]
- public IHttpActionResult DeleteProduct(string id)
- {
- Product product = db.Products.Find(id);
- if (product == null)
- {
- return NotFound();
- }
- db.Products.Remove(product);
- db.SaveChanges();
- return Ok(product);
- }
- protected override void Dispose(bool disposing)
- {
- if (disposing)
- {
- db.Dispose();
- }
- base.Dispose(disposing);
- }
- private bool ProductExists(string id)
- {
- return db.Products.Count(e => e.ProductArticleNumber == id) > 0;
- }
- }
- }
|