コード例 #1
0
 public ActionResult Edit(int id, ShowUserDto dto)
 {
     if (!ModelState.IsValid)
     {
         TempData["error"] = Messages.INPUT_ERROR;
         return(RedirectToAction(nameof(Create)));
     }
     try
     {
         dto.Id = id;
         executor.ExecuteCommand(editUser, dto);
         TempData["success"] = Messages.USER_EDIT_SUCCESS;
         return(RedirectToAction("Details", "Users", new { id }));
     }
     catch (EntityNotAllowedException)
     {
         return(RedirectToAction("PageNotFound", "Redirections"));
     }
     catch (EntityAlreadyExistsException e)
     {
         TempData["error"] = e.Message;
     }
     catch (Exception)
     {
         TempData["error"] = Messages.USER_EDIT_ERROR;
     }
     return(RedirectToAction("Edit", "Users", new { id }));
 }
コード例 #2
0
        public ActionResult Edit(int id, ShowUserDto dto)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.Roles = _getRoles.Execute(new GeneralSearchQuery());
                return(View(dto));
            }

            try
            {
                _editUser.Execute(dto);
                return(RedirectToAction(nameof(Index)));
            }
            catch (NotFoundException)
            {
                return(RedirectToAction(nameof(Index)));
            }
            catch (EntityAlreadyExistsException)
            {
                TempData["error"] = "User with the same username already exists.";
                return(View(dto));
            }
            catch (Exception)
            {
                TempData["error"] = "Something went wrong. Please try again.";
                return(View(dto));
            }
        }
コード例 #3
0
        public void Execute(ShowUserDto request)
        {
            var user = Context.Users.Find(request.Id);

            if (user == null || user.IsDeleted == true)
            {
                throw new EntityNotFoundException("User");
            }

            if (request.Username.ToLower() != user.Username.ToLower() && Context.Users.Any(u => u.Username.ToLower() == request.Username.ToLower()))
            {
                throw new EntityAlreadyExistsException("Username");
            }

            if (request.Email.ToLower() != user.Email.ToLower() && Context.Users.Any(u => u.Email.ToLower() == request.Email.ToLower()))
            {
                throw new EntityAlreadyExistsException("Email");
            }

            if (request.Password != request.ConfirmPassword)
            {
                throw new EntityMustHaveConfirmedPassword("Password fields must match.");
            }

            user.FirstName = request.FirstName;
            user.LastName  = request.LastName;
            user.Username  = request.Username;
            user.Email     = request.Email;
            user.Password  = request.Password;
            user.RoleId    = request.RoleId;

            Context.SaveChanges();
        }
コード例 #4
0
        public void Execute(ShowUserDto request)
        {
            var user = Context.Users.Find(request.Id);

            if (user == null)
            {
                throw new NotFoundException();
            }

            if (request.Username != user.Username && Context.Users.Any(u => u.Username == request.Username))
            {
                throw new EntityAlreadyExistsException();
            }

            //if (request.Password != null)
            //    user.Password = request.Password;

            user.FirstName = request.FirstName;
            user.LastName  = request.LastName;
            user.Username  = request.Username;
            // user.Password = request.Password;
            user.RoleId = request.RoleId;

            Context.SaveChanges();
        }
コード例 #5
0
 public IActionResult Put(int id, [FromBody] ShowUserDto dto)
 {
     try
     {
         dto.Id = id;
         _editUser.Execute(dto);
         return(StatusCode(204));
     }
     catch (NotFoundException)
     {
         return(NotFound());
     }
     catch (EntityAlreadyExistsException)
     {
         return(StatusCode(422));
     }
 }
コード例 #6
0
        public ShowUserDto Execute(int request)
        {
            var user = Context.Users
                       .Include(p => p.Posts)
                       .ThenInclude(pt => pt.PostTags)
                       .ThenInclude(t => t.Tag)
                       .Include(r => r.Role)
                       .Include(p => p.Posts)
                       .ThenInclude(c => c.Category)
                       .Where(u => u.Id == request)
                       .FirstOrDefault();

            if (user == null)
            {
                throw new NotFoundException();
            }

            var dto = new ShowUserDto
            {
                Id           = user.Id,
                FirstName    = user.FirstName,
                LastName     = user.LastName,
                Username     = user.Username,
                RoleName     = user.Role.Name,
                showPostDtos = user.Posts.Select(p => new ShowPostDto
                {
                    Title       = p.Title,
                    Summary     = p.Summary,
                    Text        = p.Text,
                    Category    = p.Category.Name,
                    showTagDtos = p.PostTags.Select(t => new ShowTagDto
                    {
                        Name = t.Tag.Name
                    })
                })
            };

            return(dto);
        }
コード例 #7
0
 public IActionResult Put(int id, [FromBody] ShowUserDto dto)
 {
     try
     {
         dto.Id = id;
         editUser.Execute(dto);
         return(StatusCode(204));
     }
     catch (EntityAlreadyExistsException e)
     {
         return(StatusCode(409, new
         {
             Errors = new List <string> {
                 e.Message
             }
         }));
     }
     catch (EntityNotFoundException e)
     {
         return(NotFound(new
         {
             Errors = new List <string> {
                 e.Message
             }
         }));
     }
     catch (Exception e)
     {
         return(StatusCode(500, new
         {
             Errors = new List <string> {
                 e.Message
             }
         }));
     }
 }