public async Task <IActionResult> Edit(WorkOrderEditDto model, IFormFile file)
        {
            if (!ModelState.IsValid)
            {
                var users = await _userManager.Users.ToListAsync();

                ViewBag.Users = users.Select(x => new SelectListItem()
                {
                    Text  = x.FirstName + " " + x.LastName,
                    Value = x.Id.ToString()
                }).ToList();
                return(View(model));
            }

            var entity = _mapper.Map <WorkOrder>(model);

            if (file != null)
            {
                string oldImagePath = entity.ImageUrl;
                var    result       = await _imageHelper.UploadPicture(file);

                entity.ImageUrl = result.FullName;
                _imageHelper.Delete(oldImagePath);
            }
            await _myService.UpdateAsync(entity);

            await _unitOfWork.SaveChangesAsync();

            TempData["Success"] = "Başarılı Bir Şekilde Güncellendi";


            return(RedirectToAction("Index"));
        }
Esempio n. 2
0
        public async Task <JsonResult> Delete(int userId)
        {
            var user = await _usermanager.FindByIdAsync(userId.ToString());

            var result = await _usermanager.DeleteAsync(user);

            if (result.Succeeded)
            {
                if (user.Picture != "userImages/defaultUser.png")
                {
                    _ımageHelper.Delete(user.Picture);
                }
                var deletedUser = JsonSerializer.Serialize(new UserDto
                {
                    ResultStatus = ResultStatus.Succes,
                    Message      = $"{user.UserName} adlı kullanıcı başarıyla silinmiştir",
                    User         = user
                });
                return(Json(deletedUser));
            }
            else
            {
                string errorMessages = String.Empty;
                foreach (var error in result.Errors)
                {
                    errorMessages = $"*{error.Description}\n";
                }
                var deletedUserErrorModel = JsonSerializer.Serialize(new UserDto
                {
                    ResultStatus = ResultStatus.Error,
                    Message      = $"{user.UserName} adlı kullanıcı başarıyla silinirken bazı hatalar oluştu.\n {errorMessages}",
                    User         = user
                });
                return(Json(deletedUserErrorModel));
            }
        }
        public async Task <IActionResult> Update(UserUpdateDto userUpdateDto)
        {
            if (ModelState.IsValid)
            {
                bool isNewPictureUploaded = false;
                var  oldUser = await _userManager.FindByIdAsync(userUpdateDto.Id.ToString());

                var oldUserPicture = oldUser.Picture;
                if (userUpdateDto.PictureFile != null)
                {
                    var uploadedImageDtoResult = await _imageHelper.Upload(userUpdateDto.UserName, userUpdateDto.PictureFile, PictureType.user);

                    userUpdateDto.Picture = uploadedImageDtoResult.ResultStatus == ResultStatus.Success
                        ? uploadedImageDtoResult.Data.FullName
                        : "userImages/defaultUser.png";
                    if (oldUserPicture != "userImages/defaultUser.png")
                    {
                        isNewPictureUploaded = true;
                    }
                }

                var updatedUser = _mapper.Map <UserUpdateDto, User>(userUpdateDto, oldUser);
                var result      = await _userManager.UpdateAsync(updatedUser);

                if (result.Succeeded)
                {
                    if (isNewPictureUploaded)
                    {
                        _imageHelper.Delete(oldUserPicture);
                    }

                    var userUpdateViewModel = JsonSerializer.Serialize(new UserUpdateAjaxViewModel
                    {
                        UserDto = new UserDto
                        {
                            ResultStatus = ResultStatus.Success,
                            Message      = $"{updatedUser.UserName} adlı kullanıcı başarıyla güncellenmiştir.",
                            User         = updatedUser
                        },
                        UserUpdatePartial = await this.RenderViewToStringAsync("_UserUpdatePartial", userUpdateDto)
                    });
                    return(Json(userUpdateViewModel));
                }
                else
                {
                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError("", error.Description);
                    }
                    var userUpdateErorViewModel = JsonSerializer.Serialize(new UserUpdateAjaxViewModel
                    {
                        UserUpdateDto     = userUpdateDto,
                        UserUpdatePartial = await this.RenderViewToStringAsync("_UserUpdatePartial", userUpdateDto)
                    });
                    return(Json(userUpdateErorViewModel));
                }
            }
            else
            {
                var userUpdateModelStateErrorViewModel = JsonSerializer.Serialize(new UserUpdateAjaxViewModel
                {
                    UserUpdateDto     = userUpdateDto,
                    UserUpdatePartial = await this.RenderViewToStringAsync("_UserUpdatePartial", userUpdateDto)
                });
                return(Json(userUpdateModelStateErrorViewModel));
            }
        }