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); }
public async Task <IActionResult> Post([FromBody] TownCreateModel townCreateModel) { if (await _townService.Exists(townCreateModel.Title)) { return(Conflict($"Such town already exists")); } TownDto townCreateDto = _mapper.Map <TownDto>(townCreateModel); TownDto createdTownDto = await _townService.CreateTown(townCreateDto); TownWebModel createdTownModel = _mapper.Map <TownWebModel>(createdTownDto); return(CreatedAtAction(nameof(Get), new { id = createdTownModel.TownId }, createdTownModel)); }
// AddTown <townName> <countryName> public string Execute(string[] data) { string townName = data[0]; if (townService.Exists(townName)) { throw new DuplicateObjectException(typeof(Town).Name, townName); } string countryName = data[1]; Town town = townService.Add(townName, countryName); return(String.Format(SuccessMessage, town.Name)); }
// AddTown <townName> <countryName> public string Execute(string[] data) { if (!userSessionService.IsLoggedIn()) { throw new InvalidOperationException("Invalid credentials!"); } string townName = data[0]; string country = data[1]; var townExists = townService.Exists(townName); if (townExists) { throw new ArgumentException($"Town {townName} was already added!"); } var townDto = new TownDto() { TownName = townName, CountryName = country }; var town = townService.Add(townName, country); return($"Town {townName} was added successfully!"); }
// 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); }