// GET: Companies
 public ActionResult Index()
 {
    List<Company> companyList = new List<Company>();
    using (CompanyContactsContext db = new CompanyContactsContext())
    {
       companyList = db.Companies.ToList();
    }
    return View( companyList );
 }
 // GET: Contacts
 public ActionResult Index()
 {
    List<Contact> view = new List<Contact>();
    using (CompanyContactsContext db = new CompanyContactsContext())
    {
       view = db.Contacts.ToList();
    }
    return View( view );
 }
      public ActionResult Create( [Bind( Include = "CompanyName,CompanyRef" )] Company id )
      {
         if (ModelState.IsValid)
         {
            using (CompanyContactsContext db = new CompanyContactsContext())
            {
               db.Companies.Add( id );
               db.SaveChanges();
            }
            return RedirectToAction( "Index" );
         }

         return View( id );
      }
 public ActionResult Create( [Bind( Include = "CompanyName,ContactId,Title,Name,Surname,Email,Phone,Mobile,Position,ParentContactId,Notes" )] Contact contact )
 {
    contact.Companies    = GetCompanies();
    contact.ContactNames = GetContacts();
    using (CompanyContactsContext db = new CompanyContactsContext())
    {
       if (ModelState.IsValid)
       {
          db.Contacts.Add( contact );
          db.SaveChanges();
          return RedirectToAction( "Index" );
       }
    }
    return View( contact );
 }
 // GET: Contacts/_Details/{companyName}
 public ActionResult _Details(string id)
 {
    List<Contact> view = new List<Contact>();
    using (CompanyContactsContext db = new CompanyContactsContext())
    {
       foreach (Contact thisContact in db.Contacts)
       {
          if (thisContact.CompanyName.Equals( id ))
          {
             view.Add( thisContact );
          }
       }
    }
    return View( view );
 }
 // GET: Contacts/Edit/5
 public ActionResult Edit( int? id )
 {
    if (id == null)
    {
       return new HttpStatusCodeResult( HttpStatusCode.BadRequest );
    }
    using (CompanyContactsContext db = new CompanyContactsContext())
    {
       Contact contact = db.Contacts.Find(id);
       if (contact != null)
       {
          contact.Companies = GetCompanies();
          contact.ContactNames = GetContacts();
          return View( contact );
       }
    }
    return HttpNotFound();
 }
 // GET: Companies/Edit/{name}
 public ActionResult Edit( string id )
 {
    if (string.IsNullOrEmpty( id ))
    {
       return new HttpStatusCodeResult( HttpStatusCode.BadRequest );
    }
    using (CompanyContactsContext db = new CompanyContactsContext())
    {
       Company company = db.Companies.Find(id);
       if (company != null)
       {
          int? keyContactId = null;
          int? emergencyContactId = null;
          bool onSite = false;
          foreach (Contact contact in db.Contacts)
          {
             if (contact.CompanyName.Equals( company.CompanyName ))
             {
                if (contact.KeyContact)
                {
                   keyContactId = contact.ContactId;
                   onSite = contact.OnSite;
                }
                if (contact.EmergencyContact)
                {
                   emergencyContactId = contact.ContactId;
                }
             }
          }
          var viewModel = new CompanyContactDetailsViewModel
          {
             CompanyName = company.CompanyName,
             CompanyRef = company.CompanyRef,
             NatureOfBusiness = company.NatureOfBusiness,
             ContactNames = GetContacts(),
             KeyContact = keyContactId,
             OnSite = onSite,
             EmergencyContact = emergencyContactId
          };
          return View( viewModel );
       }
    }
    return HttpNotFound();
 }
      public ActionResult Edit( [Bind( Include = "CompanyName,CompanyRef,NatureOfBusiness,KeyContact,OnSite,EmergencyContact" )] CompanyContactDetailsViewModel companyView )
      {
         Company company = new Company();
         company.CompanyName = companyView.CompanyName;
         company.CompanyRef = companyView.CompanyRef;
         company.NatureOfBusiness = companyView.NatureOfBusiness;
         companyView.ContactNames = GetContacts();
         if (ModelState.IsValid)
         {
            using (CompanyContactsContext db = new CompanyContactsContext())
            {
               // We may have just made a contact the key or emergency contact. If so, ensure that only one
               // person is the specified contact for this company.
               foreach ( Contact contact in db.Contacts)
               {
                  if (contact.CompanyName.Equals( company.CompanyName ))
                  {
                     contact.KeyContact = false;
                     contact.OnSite = false;
                     if (contact.ContactId == companyView.KeyContact)
                     {
                        contact.KeyContact = true;
                        contact.OnSite = companyView.OnSite;
                     }

                     contact.EmergencyContact = false;
                     if (contact.ContactId == companyView.EmergencyContact)
                     {
                        contact.EmergencyContact = true;
                     }

                     db.Entry( contact ).State = EntityState.Modified;
                  }
               }

               db.Entry( company ).State = EntityState.Modified;
               db.SaveChanges();
            }
            return RedirectToAction( "Index" );
         }
         return View( companyView );
      }
 private IEnumerable<SelectListItem> GetContacts()
 {
    var selectList = new List<SelectListItem>();
    using (CompanyContactsContext db = new CompanyContactsContext())
    {
       selectList.Add( new SelectListItem { Value = "", Text = "Optional - contact" } );
       foreach (var contact in db.Contacts.ToList())
       {
          selectList.Add( new SelectListItem
          {
             Value = contact.ContactId.ToString(),
             Text = contact.Name
          } );
       }
    }
    return selectList;
 }
      public ActionResult DeleteConfirmed( string id )
      {
         using (CompanyContactsContext db = new CompanyContactsContext())
         {
            foreach (Contact staff in db.Contacts)
            {
               if (staff.CompanyName.Equals( id ))
               {
                  db.Contacts.Remove( staff );
               }
            }
            db.SaveChanges();

            Company company = db.Companies.Find(id);
            db.Companies.Remove( company );
            db.SaveChanges();
         }
         return RedirectToAction( "Index" );
      }
 // GET: Companies/Delete/{name}
 public ActionResult Delete( string id )
 {
    if (string.IsNullOrEmpty( id ))
    {
       return new HttpStatusCodeResult( HttpStatusCode.BadRequest );
    }
    using (CompanyContactsContext db = new CompanyContactsContext())
    {
       Company company = db.Companies.Find(id);
       if (company != null)
       {
          return View( company );
       }
    }
    return HttpNotFound();
 }
      public ActionResult Edit( [Bind( Include = "CompanyName,CompanyId,ContactId,Title,Name,Surname,Email,Phone,Mobile,Position,ParentContactId,Notes" )] Contact contact )
      {
         contact.Companies = GetCompanies();
         contact.ContactNames = GetContacts();

         if (ModelState.IsValid)
         {
            using (CompanyContactsContext db = new CompanyContactsContext())
            {
               db.Entry( contact ).State = EntityState.Modified;
               db.SaveChanges();
            }
            return RedirectToAction( "Index" );
         }
         return View( contact );
      }
 private IEnumerable<SelectListItem> GetCompanies()
 {
    var selectList = new List<SelectListItem>();
    using (CompanyContactsContext db = new CompanyContactsContext())
    {
       foreach (var company in db.Companies.ToList())
       {
          selectList.Add( new SelectListItem
          {
             Value = company.CompanyName,
             Text = company.CompanyName
          } );
       }
    }
    return selectList;
 }
      public ActionResult DeleteConfirmed( int id )
      {
         using (CompanyContactsContext db = new CompanyContactsContext())
         {
            // Need to null out any references to this contact as being a manger. Note that we do *not* want to implement a cascade delete !
            foreach (Contact staff in db.Contacts)
            {
               if (staff.ParentContactId == id)
               {
                  staff.ParentContactId = null;
                  db.Entry( staff).State = EntityState.Modified;
               }
            }
            db.SaveChanges();

            Contact contact = db.Contacts.Find(id);
            db.Contacts.Remove( contact );
            db.SaveChanges();
         }
         return RedirectToAction( "Index" );
      }