Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 WebAPI.Models;
- namespace WebAPI.Controllers
- {
- public class EmployeeController : ApiController
- {
- private DBModel db = new DBModel();
- // GET: api/Employee
- public IQueryable<Employee> GetEmployees()
- {
- //return db.Employees;
- return db.Employees;
- }
- // PUT: api/Employee/5
- [ResponseType(typeof(void))]
- public IHttpActionResult PutEmployee(int id, Employee employee)
- {
- if (id != employee.EmployeeID)
- {
- return BadRequest();
- }
- db.Entry(employee).State = EntityState.Modified;
- try
- {
- db.SaveChanges();
- }
- catch (DbUpdateConcurrencyException)
- {
- if (!EmployeeExists(id))
- {
- return NotFound();
- }
- else
- {
- throw;
- }
- }
- return StatusCode(HttpStatusCode.NoContent);
- }
- // POST: api/Employee
- [ResponseType(typeof(Employee))]
- public IHttpActionResult PostEmployee(Employee employee)
- {
- db.Employees.Add(employee);
- db.SaveChanges();
- return CreatedAtRoute("DefaultApi", new { id = employee.EmployeeID }, employee);
- }
- // DELETE: api/Employee/5
- [ResponseType(typeof(Employee))]
- public IHttpActionResult DeleteEmployee(int id)
- {
- Employee employee = db.Employees.Find(id);
- if (employee == null)
- {
- return NotFound();
- }
- db.Employees.Remove(employee);
- db.SaveChanges();
- return Ok(employee);
- }
- protected override void Dispose(bool disposing)
- {
- if (disposing)
- {
- db.Dispose();
- }
- base.Dispose(disposing);
- }
- private bool EmployeeExists(int id)
- {
- return db.Employees.Count(e => e.EmployeeID == id) > 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment