public IHttpActionResult Update([FromUri] Guid id, UpdateUserCommand command) { command.UserId = id; _userService.UpdateUser(command); return ResponseMessage(new HttpResponseMessage(HttpStatusCode.NoContent)); }
public void UpdateUser(UpdateUserCommand command) { EnsureIsValid(command); try { var user = _deps.Users.Find(command.UserId); if (user == null || user.Deleted) { throw NotFound.ExceptionFor<User>(command.UserId); } var paymentProfile = _deps.PaymentProfiles.Find(command.UserId); if (command.Role == UserClaim.Roles.Admin && paymentProfile != null) { _deps.PaymentProfiles.Delete(paymentProfile); } else if (command.Role == UserClaim.Roles.Customer && paymentProfile == null) { paymentProfile = _deps.UserPaymentProfileFactory.Create(user, command.Address, command.FullName); _deps.PaymentProfiles.Create(paymentProfile); } else if (command.Role == UserClaim.Roles.Customer) { Mapper.Map(command, paymentProfile); _deps.PaymentProfiles.Update(paymentProfile); } Mapper.Map(command, user); var role = UserClaim.CreateRole(command.UserId, command.Role); var existingRoles = user.Claims.Where(x => x.Type == ClaimTypes.Role).ToList(); foreach (var existingRole in existingRoles) { user.Claims.Remove(existingRole); } user.Claims.Add(role); if (command.ChangePassword) { user.UpdatePassword(command.Password); } Mapper.Map(command, user.Profile); Commit(); } catch (ServiceException) { throw; } catch (Exception ex) { throw new ServiceException("Can't update user.", ex); } }