Exemple #1
0
        public ActionResult AddPet(AddPetViewModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return this.View(model);
            }

            var dataModel = AutoMapper.Mapper.Map<AddPetViewModel, PetCare.Models.Pet>(model);

            Image image = null;
            if (model.ProfilePicture != null)
            {
                using (var memory = new MemoryStream())
                {
                    model.ProfilePicture.InputStream.CopyTo(memory);
                    var content = memory.GetBuffer();

                    image = new Image
                    {
                        Content = content,
                        Extension = model.ProfilePicture.FileName.Split(new[] { '.' }).Last()
                    };
                }
            }

            dataModel.Image = image;
            var currentUser = this.users.GetByUsername(this.User.Identity.Name).FirstOrDefault();
            dataModel.Owner = currentUser;
            dataModel.HealthRecordId = 0;

            this.pets.Add(dataModel);

            this.TempData["Notification"] = model.Name + " added successfully!";
            return RedirectToAction("Index", "Home");
        }
Exemple #2
0
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                Image image = null;

                if (model.ProfilePicture != null)
                {
                    using (var memory = new MemoryStream())
                    {
                        model.ProfilePicture.InputStream.CopyTo(memory);
                        var content = memory.GetBuffer();

                        image = new Image
                        {
                            Content = content,
                            Extension = model.ProfilePicture.FileName.Split(new[] { '.' }).Last()
                        };
                    }
                }

                var user = new User
                {
                    UserName = model.UserName,
                    Email = model.Email,
                    FirstName = model.FirstName,
                    LastName = model.LastName,
                    PhoneNumber = model.PhoneNumber,
                    SergeryLocation = model.SergeryLocation,
                    ProfilePicture = image,
                    Gender = model.Gender,
                    IsVet = model.IsVet
                };

                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);

                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    if (model.IsVet)
                    {
                        UserManager.AddToRole(user.Id, "vet");
                    }
                    else
                    {
                        UserManager.AddToRole(user.Id, "user");
                    }

                    return RedirectToAction("Index", "Home");
                }

                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
Exemple #3
0
        public ActionResult EditUserProfile(EditUserProfileViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            Image image = null;

            if (model.Picture != null)
            {
                using (var memory = new MemoryStream())
                {
                    model.Picture.InputStream.CopyTo(memory);
                    var content = memory.GetBuffer();

                    image = new Image
                    {
                        Content = content,
                        Extension = model.Picture.FileName.Split(new[] { '.' }).Last()
                    };
                }
            }

            var dataModel = AutoMapper.Mapper.Map<EditUserProfileViewModel, PetCare.Models.User>(model);

            string username = this.User.Identity.Name;
            User user = this.users.GetByUsername(username).FirstOrDefault();
            
            this.users.UpdateUser(dataModel, user.Id);

            return RedirectToAction("Index", "Home");
        }