public ActionResult Edit(int?id) { ContactsService contactsService = new ContactsService(); ContactsEditVM model = new ContactsEditVM(); Contact contact; if (!id.HasValue) { contact = new Contact(); model.CountryID = int.Parse(contactsService.GetSelectedCountries().FirstOrDefault().Value); } else { contact = contactsService.GetByID(id.Value); if (contact == null) { return(this.RedirectToAction(c => c.List())); } model.CountryID = contact.City.CountryID; } Mapper.Map(contact, model); model.Countries = contactsService.GetSelectedCountries(); model.Cities = contactsService.GetCitiesByCountryID(model.CountryID); model.Groups = contactsService.GetSelectedGroups(contact.Groups); return(View(model)); }
public ActionResult Edit() { UnitOfWork unitOfWork = new UnitOfWork(); ContactsService contactsService = new ContactsService(unitOfWork); ContactsEditVM model = new ContactsEditVM(); TryUpdateModel(model); Contact contact; if (model.ID == 0) { contact = new Contact(); } else { contact = contactsService.GetByID(model.ID); if (contact == null) { return(this.RedirectToAction(c => c.List())); } } if (model.ImageUpload != null && model.ImageUpload.ContentLength > 0) { var imageExtension = Path.GetExtension(model.ImageUpload.FileName); if (String.IsNullOrEmpty(imageExtension) || !imageExtension.Equals(".jpg", StringComparison.OrdinalIgnoreCase)) { ModelState.AddModelError(string.Empty, "Wrong image format."); } else { string filePath = Server.MapPath("~/Uploads/"); model.ImagePath = model.ImageUpload.FileName; model.ImageUpload.SaveAs(filePath + model.ImagePath); } } if (!ModelState.IsValid) { model.Countries = contactsService.GetSelectedCountries(); model.Cities = contactsService.GetCitiesByCountryID(model.CountryID); model.Groups = contactsService.GetSelectedGroups(contact.Groups, model.SelectedGroups); return(View(model)); } Mapper.Map(model, contact); contact.UserID = AuthenticationManager.LoggedUser.ID; contactsService.UpdateContactGroups(contact, model.SelectedGroups); contactsService.Save(contact); return(this.RedirectToAction(c => c.List())); }
public JsonResult DeleteImage(int contactID) { ContactsService contactsService = new ContactsService(); Contact contact = contactsService.GetByID(contactID); contact.ImagePath = "default.jpg"; contactsService.Save(contact); return(Json(new object[] { new object() }, JsonRequestBehavior.AllowGet)); }
public ActionResult Edit() { UnitOfWork uf = new UnitOfWork(); ContactsService contactService = new ContactsService(uf); ContactEditVM model = new ContactEditVM(); TryUpdateModel(model); Contact c; if (model.ID != 0) { c = contactService.GetByID(model.ID); if (c == null) { return(this.RedirectToAction(co => co.List())); } } else { c = new Contact(); } if (model.ImageUpload != null && model.ImageUpload.ContentLength > 0) { var ext = Path.GetExtension(model.ImageUpload.FileName); if (String.IsNullOrEmpty(ext) || !ext.Equals(".jpg", StringComparison.OrdinalIgnoreCase)) { ModelState.AddModelError("", "Image format not accepted!"); } else { var uploadDir = "/Uploads/"; var imagePath = Path.Combine(Server.MapPath(uploadDir), model.ImageUpload.FileName); model.ImagePath = model.ImageUpload.FileName; model.ImageUpload.SaveAs(imagePath); } } if (!ModelState.IsValid) { model.Groups = contactService.GetSelectedGroups(c.Groups); model.Countries = contactService.GetAllCountries().OrderBy(country => country.Text); model.Cities = contactService.GetCitiesByCountry(model.CountryID); return(View(model)); } Mapper.Map(model, c); c.UserID = AuthenticationService.LoggedUser.ID; contactService.SetSelectedGroups(c, model.SelectedGroups); contactService.Save(c); return(this.RedirectToAction(co => co.List())); }
public IHttpActionResult Get(int id) { var contact = Aggregates.Contact.ToClient(_contactService.GetByID(id)); if (contact == null) { return(NotFound()); } else { return(Ok(contact)); } }
public ActionResult Delete(int?id) { if (!id.HasValue) { return(this.RedirectToAction(c => c.List())); } UnitOfWork unitOfWork = new UnitOfWork(); ContactsService contactsService = new ContactsService(unitOfWork); contactsService.GetByID(id.Value).Groups.Clear(); contactsService.Delete(id.Value); return(this.RedirectToAction(c => c.List())); }