// ModifyUser <username> <property> <new value> public string Execute(string[] data) { string username = data[0]; ModifyUserDto userDTO = userService.ByUsername <ModifyUserDto>(username); if (userDTO == null || userDTO.IsDeleted == true) { throw new ObjectNotFoundException(typeof(User).Name, username); } var modifiableProperties = userDTO.GetType() .GetProperties().Select(pi => pi.Name); string propertyName = data[1]; if (!modifiableProperties.Contains(propertyName)) { throw new PropertyNotSupportedException(propertyName); } string propertyValue = data[2]; switch (propertyName.ToUpper()) { case "BORNTOWN": TownDto townDTO = townService.ByName <TownDto>(propertyValue); if (townDTO == null) { throw new InvalidValueException( propertyValue, Environment.NewLine + new ObjectNotFoundException(typeof(Town).Name, propertyValue).Message); } userService.SetBornTown(userDTO.Id, townDTO.Id); break; case "CURRENTTOWN": townDTO = townService.ByName <TownDto>(propertyValue); if (townDTO == null) { throw new InvalidValueException( propertyValue, Environment.NewLine + new ObjectNotFoundException(typeof(Town).Name, propertyValue).Message); } userService.SetCurrentTown(userDTO.Id, townDTO.Id); break; case "PASSWORD": if (!IsPasswordValid(propertyValue)) { throw new InvalidValueException( propertyValue, Environment.NewLine + new InvalidObjectException("password").Message); } userService.ChangePassword(userDTO.Id, propertyValue); break; default: throw new PropertyNotSupportedException(propertyName); } return(String.Format(SuccessMessage, username, propertyName, propertyValue)); }
private void SetCurrentTown(string newCurrentTown, int userId) { if (this.townService.Exists(newCurrentTown)) { int townId = townService.ByName <TownDto>(newCurrentTown).Id; userService.SetCurrentTown(userId, townId); } else { throw new ArgumentException($"Value {newCurrentTown} not valid.\nTown {newCurrentTown} not found!”"); } }
private void SetCurrentTown(int userId, string town) { bool isTownExist = townService.Exists(town); if (!isTownExist) { throw new ArgumentException($"Value {town} not valid.\nTown {town} not found!"); } int townId = townService.ByName <Town>(town).Id; userService.SetCurrentTown(userId, townId); }
// ModifyUser <username> <property> <new value> // For example: // ModifyUser <username> Password <NewPassword> // ModifyUser <username> BornTown <newBornTownName> // ModifyUser <username> CurrentTown <newCurrentTownName> // !!! Cannot change username public string Execute(string[] data) { var username = data[0]; var property = data[1]; var newValue = data[2]; if (username != this.userSessionService.User.Username) { throw new InvalidOperationException("Invalid credentials!"); } if (!userService.Exists(username)) { throw new ArgumentException($"User {username} not found!"); } var result = string.Empty; var userDto = userService.ByUsername <UserDto>(username); switch (property.ToLower()) { case "currenttown": if (!townService.Exists(newValue)) { throw new ArgumentException($"Value {newValue} not valid." + $"{Environment.NewLine}Town {newValue} not found!"); } else { var townDto = townService.ByName <TownDto>(newValue); userService.SetCurrentTown(userDto.Id, townDto.Id); result = $"User {username} {property} is {newValue}."; } break; case "borntown": if (!townService.Exists(newValue)) { throw new ArgumentException($"Value {newValue} not valid." + $"{Environment.NewLine}Town {newValue} not found!"); } else { var townDto = townService.ByName <TownDto>(newValue); userService.SetBornTown(userDto.Id, townDto.Id); result = $"User {username} {property} is {newValue}."; } break; case "password": if (newValue.Any(x => Char.IsDigit(x)) && newValue.Any(x => Char.IsLower(x))) { userService.ChangePassword(userDto.Id, newValue); result = $"User {username} {property} is {newValue}."; } else { throw new ArgumentException($"Value {newValue} not valid." + $"{Environment.NewLine}Invalid Password"); } break; default: throw new ArgumentException($"Property {property} not supported!"); } return(result); }