0% found this document useful (0 votes)
25 views

Customercontroller

This document contains code for a CustomersController class that provides CRUD operations on Customer objects via a REST API. The class uses a UnitOfWork object to interact with a CustomerRepository to get, add, update, and delete individual Customer objects by ID. Methods are included to get all customers or a single customer by ID, update a customer by ID, create a new customer, and delete a customer by ID.

Uploaded by

api-296693081
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Customercontroller

This document contains code for a CustomersController class that provides CRUD operations on Customer objects via a REST API. The class uses a UnitOfWork object to interact with a CustomerRepository to get, add, update, and delete individual Customer objects by ID. Methods are included to get all customers or a single customer by ID, update a customer by ID, create a new customer, and delete a customer by ID.

Uploaded by

api-296693081
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

using ApiCrudDemo.

Models;
using ApiCrudDemo.Repository;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Web.Http;
using System.Web.Http.Description;
namespace ApiCrudDemo.Controllers
{
public class CustomersController : ApiController
{
private UnitOfWork unitOfWork = new UnitOfWork();
// GET: api/Customers
public IQueryable<Customer> GetCustomers()
{
return unitOfWork.CustomerRepository.GetAll();
}
// GET: api/Customers/5
[ResponseType(typeof(Customer))]
public IHttpActionResult GetCustomer(int id)
{
Customer customer = unitOfWork.CustomerRepository.GetSingle(id);
if (customer == null)
{
return NotFound();
}
return Ok(customer);

}
// PUT: api/Customers/5
[ResponseType(typeof(void))]
public IHttpActionResult PutCustomer(int id, Customer customer)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != customer.CustomerId)
{
return BadRequest();
}
unitOfWork.CustomerRepository.Attach(customer);
try
{
unitOfWork.Commit();
}
catch (DbUpdateConcurrencyException)
{
throw;
}
return StatusCode(HttpStatusCode.NoContent);
}
// POST: api/Customers
[ResponseType(typeof(Customer))]
public IHttpActionResult PostCustomer(Customer customer)

{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
unitOfWork.CustomerRepository.Add(customer);
unitOfWork.Commit();
return CreatedAtRoute("DefaultApi", new { id = customer.CustomerId }, customer);
}
// DELETE: api/Customers/5
[ResponseType(typeof(Customer))]
public IHttpActionResult DeleteCustomer(int id)
{
Customer customer = unitOfWork.CustomerRepository.GetSingle(id);
if (customer == null)
{
return NotFound();
}
unitOfWork.CustomerRepository.Delete(customer);
unitOfWork.Commit();
return Ok(customer);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
unitOfWork.Dispose();

}
base.Dispose(disposing);
}
}
}

You might also like