예제 #1
0
        public ActionResult Edit(int?id)
        {
            if (!id.HasValue)
            {
                return(RedirectToAction("Index"));
            }

            Contact contact = unitOfWork.ContactRepository.GetById(id.Value);

            if (contact == null)
            {
                return(Redirect("~/Error/PageNotFound"));
            }

            ContactsCreateVM model = new ContactsCreateVM();

            model.ID            = contact.ID;
            model.FirstName     = contact.FirstName;
            model.LastName      = contact.LastName;
            model.UserId        = contact.UserId;
            model.Email         = contact.Email;
            model.PhotoFilePath = contact.PhotoFilePath;
            model.BirthDate     = contact.BirthDay;

            return(View("CreateEdit", model));
        }
예제 #2
0
        public ActionResult CreateEdit(ContactsCreateVM model, HttpPostedFileBase photo)
        {
            if (ModelState.IsValid)
            {
                Contact contact;

                if (model.ID > 0)
                {
                    contact = unitOfWork.ContactRepository.GetById(model.ID);
                }
                else
                {
                    contact = new Contact();
                }

                // Email

                // Photo
                WebImage img = null;

                if (photo != null)
                {
                    string path = Server.MapPath(@"~/Content/contacts_photos/" + AuthenticationManager.LoggedUser.ID);
                    var    file = Request.Files["photo"];

                    if (file.ContentLength > int.Parse(WebConfigurationManager.AppSettings["allowedImageSize"]))
                    {
                        ModelState.AddModelError(string.Empty, "Image too big");
                        return(View(model));
                    }

                    try
                    {
                        // if file is not an image, exception will be thrown - Invalid Image Format
                        img = new WebImage(file.InputStream);
                        string guid = Guid.NewGuid().ToString();
                        try
                        {
                            img.Save(path + "//" + guid, img.ImageFormat, true);
                        }
                        catch (IOException)
                        {
                            Directory.CreateDirectory(path);
                            img.Save(path + "//" + guid, img.ImageFormat, true);
                        }

                        if (model.ID > 0 && !contact.PhotoFilePath.Contains("default"))
                        {
                            System.IO.File.Delete(Server.MapPath("~" + contact.PhotoFilePath));
                        }

                        // Saving PhotoFilePath
                        contact.PhotoFilePath = "/Content/contacts_photos/" + AuthenticationManager.LoggedUser.ID + "/" + guid + "." + img.ImageFormat;
                    }
                    catch (ArgumentException)
                    {
                        ModelState.AddModelError(string.Empty, "Invalid Image Format");
                        return(View(model));
                    }
                }
                else
                {
                    string filepath = Server.MapPath(contact.PhotoFilePath);
                    DeleteExistingPhoto(filepath);

                    contact.PhotoFilePath = "/Content/contacts_photos/default.png";
                }

                model.UserId      = AuthenticationManager.LoggedUser.ID;
                contact.ID        = model.ID;
                contact.FirstName = model.FirstName;
                contact.LastName  = model.LastName;
                contact.UserId    = AuthenticationManager.LoggedUser.ID;
                contact.Email     = model.Email;
                contact.BirthDay  = model.BirthDate;

                if (model.ID == 0)
                {
                    unitOfWork.ContactRepository.Insert(contact);
                }
                else
                {
                    unitOfWork.ContactRepository.Update(contact);
                }
                unitOfWork.Save();

                return(RedirectToAction("Index"));
            }

            return(View(model));
        }