using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using WebApplication1.Models;

namespace WebApplication1.Controllers {
  public class ShippersController : Controller {
    private NorthwindEntities db = new NorthwindEntities();
    public ActionResult Index() {
      return View( db.Shippers.ToList() );
    }
    public ActionResult Details( int? id ) {
      if( id == null ) {
        return new HttpStatusCodeResult( HttpStatusCode.BadRequest );
      }
      Shipper shipper = db.Shippers.Find( id );
      if( shipper == null ) {
        return HttpNotFound();
      }
      return View( shipper );
    }
    public ActionResult Create() {
      return View();
    }
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create( [Bind( Include = "ShipperID,CompanyName,Phone" )] Shipper shipper ) {
      if( ModelState.IsValid ) {
        db.Shippers.Add( shipper );
        db.SaveChanges();
        return RedirectToAction( "Index" );
      }
      return View( shipper );
    }
    public ActionResult Edit( int? id ) {
      if( id == null ) {
        return new HttpStatusCodeResult( HttpStatusCode.BadRequest );
      }
      Shipper shipper = db.Shippers.Find( id );
      if( shipper == null ) {
        return HttpNotFound();
      }
      return View( shipper );
    }
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit( [Bind( Include = "ShipperID,CompanyName,Phone" )] Shipper shipper ) {
      if( ModelState.IsValid ) {
        db.Entry( shipper ).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction( "Index" );
      }
      return View( shipper );
    }
    public ActionResult Delete( int? id ) {
      if( id == null ) {
        return new HttpStatusCodeResult( HttpStatusCode.BadRequest );
      }
      Shipper shipper = db.Shippers.Find( id );
      if( shipper == null ) {
        return HttpNotFound();
      }
      return View( shipper );
    }
    [HttpPost, ActionName( "Delete" )]
    [ValidateAntiForgeryToken]
    public ActionResult DeleteConfirmed( int id ) {
      Shipper shipper = db.Shippers.Find( id );
      db.Shippers.Remove( shipper );
      db.SaveChanges();
      return RedirectToAction( "Index" );
    }
    protected override void Dispose( bool disposing ) {
      if( disposing ) {
        db.Dispose();
      }
      base.Dispose( disposing );
    }
  }
}
