示例#1
0
        public async Task <IActionResult> Update(int id, [FromForm] UserInfoPostModel userVM)
        {
            if (userVM.Avatar is not null)
            {
                if (!_userServices.isValidImage(userVM.Avatar))
                {
                    return(BadRequest(new { error_message = "Lỗi hình ảnh" }));
                }
            }

            UserInfoViewModel userInfoViewModel = await _userServices.UpdateInfoAsync(userVM, id);

            if (userInfoViewModel is not null)
            {
                return(Ok(new { data = userInfoViewModel, success = true, message = "Cập nhật thông tin cá nhân thành công  " }));
            }
            else
            {
                return(Ok(new { error_message = "Có lỗi xảy ra" }));
            }
        }
示例#2
0
        internal async Task <UserInfoViewModel> UpdateInfoAsync(UserInfoPostModel userVM, int id)
        {
            UserInfoViewModel userInfoViewModel = null;
            User user = await _bookstoreContext.Users.FindAsync(id);

            if (user is null)
            {
                return(userInfoViewModel);
            }

            if (userVM.Name is not null)
            {
                user.Name = userVM.Name;
            }
            if (userVM.Email is not null)
            {
                user.Email = userVM.Email;
            }
            if (userVM.Phone is not null)
            {
                user.Phone = userVM.Phone;
            }

            if (userVM.Avatar is not null)
            {
                user.Avatar = DateTimeOffset.Now.ToUnixTimeSeconds() + "_" + userVM.Avatar.FileName;
            }
            if (userVM.IsAccess is not null)
            {
                user.IsAccess = userVM.IsAccess ?? false;
            }
            if (userVM.RoleId is not null)
            {
                user.RoleId = userVM.RoleId ?? 3;
            }
            _bookstoreContext.Entry(user).State = EntityState.Modified;
            try
            {
                bool isUpdateUserSuccess = await _bookstoreContext.SaveChangesAsync() != 0;

                if ((userVM.IsAccess is not null || userVM.RoleId is not null) && isUpdateUserSuccess) // Tai su dung de cap nhat trang thai, tranh truong hop save o bottom case
                {
                    return(_mapper.Map <UserInfoViewModel>(user));
                }

                if (isUpdateUserSuccess && userVM is not null)
                {
                    if (userVM.Avatar is not null)
                    {
                        UploadImage(userVM.Avatar, user.Avatar);
                    }
                    userInfoViewModel = _mapper.Map <UserInfoViewModel>(user);
                }

                UserAddress userAddress = await _bookstoreContext.UserAddress
                                          .Where(u => u.UserId == user.Id)
                                          .Where(u => u.IsDefault == true)
                                          .FirstOrDefaultAsync();

                if (userAddress is not null)
                {
                    //Update
                    if (userVM.CityAddressId is not null)
                    {
                        userAddress.CityAddressId = userVM.CityAddressId ?? 0;
                    }
                    if (userVM.DistrictAddressId is not null)
                    {
                        userAddress.DistrictAddressId = userVM.DistrictAddressId ?? 0;
                    }
                    if (userVM.Name is not null)
                    {
                        userAddress.Name = userVM.Name;
                    }
                    if (userVM.Phone is not null)
                    {
                        userAddress.Phone = userVM.Phone;
                    }
                    _bookstoreContext.Entry(userAddress).State = EntityState.Modified;
                }
                else
                {
                    //Create
                    UserAddress newUserAddress = new UserAddress
                    {
                        UserId    = user.Id,
                        IsDefault = true
                    };
                    if (userVM.CityAddressId is not null)
                    {
                        newUserAddress.CityAddressId = userVM.CityAddressId ?? 0;
                    }
                    if (userVM.DistrictAddressId is not null)
                    {
                        newUserAddress.DistrictAddressId = userVM.DistrictAddressId ?? 0;
                    }
                    if (userVM.Name is not null)
                    {
                        newUserAddress.Name = userVM.Name;
                    }
                    if (userVM.Phone is not null)
                    {
                        newUserAddress.Phone = userVM.Phone;
                    }
                    _bookstoreContext.Add(newUserAddress);
                }
                isUpdateUserSuccess = await _bookstoreContext.SaveChangesAsync() != 0;
            }
            catch (DbUpdateConcurrencyException)
            {
                throw;
            }
            return(_mapper.Map <UserInfoViewModel>(user));
        }