Пример #1
0
        public IHttpActionResult Put([FromBody] EditProfileViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var oldProfile = _repository.GetProfileByUserName(User.Identity.Name);

            if (model.MemberName != oldProfile.Member.MemberName)
            {
                return(BadRequest("You can only update your own profile."));
            }

            AutoMapper.Mapper.Map(model, oldProfile);

            if (_repository.SaveAll())
            {
                return(Ok(AutoMapper.Mapper.Map <EditProfileViewModel>(oldProfile))); // Copy back in case any business rules are executed
            }
            else
            {
                return(BadRequest("Couldn't Save the profile."));
            }
        }
Пример #2
0
        public ActionResult EditProfile(EditProfileViewModel model)
        {
            //var age = model.Birthdate.CalculateAge();

            //if (age < 18 || age > 120)
            //{
            //  ModelState.AddModelError("Birthdate", "You must be between 18 and 120 years old.");
            //}

            //ModelState.AddModelError("", "The Postal Code isn't in the specified State");

            if (ModelState.IsValid)
            {
                var profile = _repository.GetProfile(model.MemberName);

                AutoMapper.Mapper.Map(model, profile);

                if (_repository.SaveAll())
                {
                    return(RedirectToAction("ShowProfile"));
                }
            }

            return(View());
        }
Пример #3
0
        public IHttpActionResult Post([FromBody] PhotoViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var profile = _repository.GetProfileByUserName(User.Identity.Name);

            var newPhoto = AutoMapper.Mapper.Map <Photo>(model);

            newPhoto.DateAdded = DateTime.UtcNow;
            profile.Photos.Add(newPhoto);

            if (_repository.SaveAll())
            {
                return(Created("", AutoMapper.Mapper.Map <PhotoViewModel>(newPhoto)));
            }
            else
            {
                return(BadRequest("Failed to create photo."));
            }
        }