예제 #1
0
        public async Task <IOperationResult> SaveCityAsync(string userId, string cityName)
        {
            var operationResult = new OperationResult();

            operationResult.ValidateNotNull(userId, $"{nameof(userId)} should not have a default value.");
            operationResult.ValidateNotEmpty(userId, $"{nameof(userId)} should not be empty.");
            operationResult.ValidateNotNull(cityName, $"{nameof(cityName)} should not have a default value.");
            operationResult.ValidateNotEmpty(cityName, $"{nameof(cityName)} should not be empty.");


            if (operationResult.Success)
            {
                var user = await this.FindUserById(userId).ConfigureAwait(false);

                operationResult.ValidateNotNull(user, $"{nameof(user)} should not be null.");
                if (operationResult.Success == false)
                {
                    return(operationResult);
                }

                if (user.SavedCities.Contains(cityName))
                {
                    operationResult.AppendErrorMessage("City already saved");
                    return(operationResult);
                }

                user.SavedCities.Add(cityName);
                var update = Builders <User> .Update.Set("SavedCities", user.SavedCities);

                await this._usersCollection.UpdateOneAsync(u => u.Id == user.Id, update).ConfigureAwait(false);

                return(operationResult);
            }

            return(operationResult);
        }