public async Task <IActionResult> UpdateProfile(AccountUpdateProfileViewModel vm)
        {
            if (ModelState.IsValid)
            {
                string userId = _userManagerService.GetUserId(User);

                Profile prof = _dataServiceProf.GetSingle(s => s.UserID == userId);

                prof.FirstName = vm.FirstName;
                prof.LastName  = vm.LastName;

                Address a = _context.TblAddress.Where(s => s.Favourite == true).FirstOrDefault();
                a.Favourite = false;

                Address add = _context.TblAddress.Where(s => s.AddressID == vm.FavouriteAddressId).FirstOrDefault();
                add.Favourite = true;

                await _context.SaveChangesAsync();

                _dataServiceProf.Update(prof);
                return(RedirectToAction("Index", "Home"));
            }
            //if invalid
            return(View(vm));
        }
        public IActionResult UpdateProfile()
        {
            //Current UserName
            string name = User.Identity.Name;
            //Current UserId
            string id = _userManagerService.GetUserId(User);

            Profile prof = _dataServiceProf.GetSingle(s => s.UserID == id);
            IEnumerable <Address> address = _dataServiceAddress.GetQuery().Where(a => a.UserID == id);



            AccountUpdateProfileViewModel vm = new AccountUpdateProfileViewModel
            {
                UserId    = id,
                Username  = name,
                FirstName = prof.FirstName,
                LastName  = prof.LastName,
                Addresses = address
            };

            vm.FavouriteAddressId = address.Where(a => a.Favourite == true).Select(a => a.AddressID).FirstOrDefault();

            return(View(vm));
        }
        public async Task <IActionResult> UpdateProfile(AccountUpdateProfileViewModel vm, IFormFile file)
        {
            if (ModelState.IsValid)
            {
                IdentityUser user = await _userManagerService.FindByNameAsync(User.Identity.Name);

                Profile profile = _profileDataService.GetSingle(p => p.UserId == user.Id);
                profile.FirstName = vm.FirstName;
                profile.LastName  = vm.LastName;
                profile.Mobile    = vm.Mobile;
                if (file != null)
                {
                    //check ??
                    if (file.FileName.ToLower().EndsWith("jpg") || file.FileName.ToLower().EndsWith("png") || file.FileName.ToLower().EndsWith("jpeg"))
                    {
                        //upload server path
                        var serverPath = Path.Combine(_environment.WebRootPath, "profilePhotos");
                        //create a folder
                        Directory.CreateDirectory(serverPath);
                        //get the file name
                        string fileName = Path.GetFileName(User.Identity.Name + ".jpg");

                        //stream the file to the srever
                        using (var fileStream = new FileStream(Path.Combine(serverPath, fileName), FileMode.Create))
                        {
                            await file.CopyToAsync(fileStream);
                        }
                        //assign the picture URL to the cat object
                        profile.Photo = fileName;
                    }
                    else
                    {
                        ViewBag.MyMessage = "Picture must be .jpg, .png or .jpeg";
                        return(View(vm));
                    }
                }
                else
                {
                    profile.Photo = vm.Photo;
                }
                _profileDataService.Update(profile);
                user.Email = vm.Email;
                await _userManagerService.UpdateAsync(user);

                return(RedirectToAction("Index", "Home"));
            }
            return(View(vm));
        }
        public async Task <IActionResult> UpdateProfile()
        {
            IdentityUser user = await _userManagerService.FindByNameAsync(User.Identity.Name);

            Profile profile = _profileDataService.GetSingle(p => p.UserId == user.Id);
            AccountUpdateProfileViewModel vm = new AccountUpdateProfileViewModel
            {
                FirstName = profile.FirstName,
                LastName  = profile.LastName,
                Email     = user.Email,
                Mobile    = profile.Mobile,
                Photo     = profile.Photo
            };

            return(View(vm));
        }
示例#5
0
        public async Task <IActionResult> UpdateProfile()
        {
            IdentityUser user = await _userManagerService.FindByNameAsync(User.Identity.Name);


            Profile profile = _profileDataService.GetSingle(p => p.Email == user.Email);


            AccountUpdateProfileViewModel vm;

            if (profile != null)

            {
                IEnumerable <Address> list = _AddressDataService.Query(x => x.ProfileId == profile.ProfileId);

                vm = new AccountUpdateProfileViewModel
                {
                    ProfileId = profile.ProfileId,
                    FirstName = profile.FirstName,
                    LastName  = profile.LastName,
                    //IsAdmin = profile.IsAdmin,
                    DOB       = profile.DOB,
                    Email     = profile.Email,
                    Addresses = list.Count() == 0 ?
                                new List <AccountAddressViewModel> {
                        new AccountAddressViewModel()
                    } :
                    list.Select(x => new AccountAddressViewModel()
                    {
                        AddressId   = x.AddressId,
                        Address1    = x.Address1,
                        Address2    = x.Address2,
                        PostCode    = x.PostCode,
                        Suburb      = x.Suburb,
                        State       = x.State,
                        IsFavourite = x.IsFavourite
                    }).ToList()
                };
            }
            else
            {
                vm = new AccountUpdateProfileViewModel();
            }
            return(View(vm));
        }
示例#6
0
        public IActionResult UpdateProfile(AccountUpdateProfileViewModel vm, string savechanges, string addaddress, string deleteAddress)
        {
            if (ModelState.IsValid)
            {
                if (!string.IsNullOrEmpty(savechanges))
                {
                    //map
                    Profile p = new Profile

                    {
                        ProfileId = vm.ProfileId,
                        FirstName = vm.FirstName,
                        LastName  = vm.LastName,
                        DOB       = vm.DOB,
                        //IsAdmin = vm.IsAdmin,
                        Email = User.Identity.Name,
                    };

                    //call service
                    _profileDataService.Update(p);
                    foreach (var address in vm.Addresses)
                    {
                        Address a = new Address
                        {
                            ProfileId   = p.ProfileId,
                            AddressId   = address.AddressId,
                            Address1    = address.Address1,
                            Suburb      = address.Suburb,
                            State       = address.State,
                            PostCode    = address.PostCode,
                            IsFavourite = address.IsFavourite
                        };

                        if (address.AddressId != 0)
                        {
                            _AddressDataService.Update(a);
                        }
                        else
                        {
                            _AddressDataService.Create(a);
                        }
                    }

                    ////update Password
                    //IdentityUser user = await _userManagerService.FindByNameAsync(User.Identity.Name);
                    //_userManagerService.ChangePasswordAsync(user, "", "");
                    //await _userManagerService.UpdateAsync(user);
                    return(RedirectToAction("Index", "Home"));
                }
                else if (!string.IsNullOrEmpty(addaddress))
                {
                    vm.Addresses.Add(new AccountAddressViewModel());
                }
                else if (!string.IsNullOrEmpty(deleteAddress))
                {
                    int     addressId = int.Parse(deleteAddress);
                    Address address   = _AddressDataService.GetSingle(a => a.AddressId == addressId);
                    if (address != null)
                    {
                        _AddressDataService.Remove(address);
                    }

                    IEnumerable <Address> list = _AddressDataService.Query(x => x.ProfileId == vm.ProfileId);

                    vm.Addresses = list.Count() == 0 ?
                                   new List <AccountAddressViewModel> {
                        new AccountAddressViewModel()
                    } :
                    list.Select(x => new AccountAddressViewModel()
                    {
                        AddressId   = x.AddressId,
                        Address1    = x.Address1,
                        Address2    = x.Address2,
                        PostCode    = x.PostCode,
                        Suburb      = x.Suburb,
                        State       = x.State,
                        IsFavourite = x.IsFavourite
                    }).ToList();
                }
            }
            return(View(vm));
        }