/// <summary>
        /// Method to get user's food preferences given the username
        /// </summary>
        /// <param name="username"></param>
        /// <returns>Food Preferences DTO within the Response DTO</returns>
        public ResponseDto <ICollection <string> > GetFoodPreferences(string tokenString)
        {
            // Extract username from token string
            var tokenService = new TokenService();
            var username     = tokenService.GetTokenUsername(tokenString);

            using (var gateway = new UserGateway())
            {
                // Call the user gateway to use the method to get food preferences by username
                var responseDto = gateway.GetFoodPreferencesByUsername(username);

                // If no error occurs, return the DTO
                if (responseDto.Error == null)
                {
                    return(new ResponseDto <ICollection <string> >
                    {
                        Data = responseDto.Data.FoodPreferences
                    });
                }
            }

            // Otherwise, return a DTO with error message
            return(new ResponseDto <ICollection <string> >
            {
                Error = GeneralErrorMessages.GENERAL_ERROR
            });
        }
        /// <summary>
        /// Method to edit user's food preferences
        /// </summary>
        /// <param name="foodPreferencesDto"></param>
        /// <returns>Response DTO with a boolean determining success of the transaction</returns>
        public ResponseDto <bool> EditFoodPreferences(string tokenString, FoodPreferencesDto foodPreferencesDto)
        {
            // Validate the food preference dto
            var editFoodPreferencesValidationStrategy = new EditFoodPreferencesValidationStrategy(foodPreferencesDto);
            var validationResult = editFoodPreferencesValidationStrategy.ExecuteStrategy();

            // If the food preference dto fails validation, return an error
            if (validationResult.Error != null)
            {
                return(new ResponseDto <bool>
                {
                    Data = false,
                    Error = GeneralErrorMessages.MODEL_STATE_ERROR
                });
            }

            // Extract username from token string
            var tokenService = new TokenService();
            var username     = tokenService.GetTokenUsername(tokenString);

            using (var gateway = new UserGateway())
            {
                // Get list of current food preferences from database
                var currentFoodPreferences = gateway.GetFoodPreferencesByUsername(username).Data.FoodPreferences;

                // Get list of updated food preferences from dto
                var updatedFoodPreferences = foodPreferencesDto.FoodPreferences;

                // Call method to create lists of food preferences to be added and to be removed
                var preferencesToBeAdded   = LeftOuterJoin(updatedFoodPreferences, currentFoodPreferences);
                var preferencesToBeRemoved = LeftOuterJoin(currentFoodPreferences, updatedFoodPreferences);

                // If no changes are to be made and bypass front-end validation, return true
                if (preferencesToBeAdded.Count == 0 && currentFoodPreferences.Count == 0)
                {
                    return(new ResponseDto <bool>
                    {
                        Data = true
                    });
                }

                // Otherwise, call gateway to update user's food preferences
                var result = gateway.EditFoodPreferencesByUsername(username, preferencesToBeAdded, preferencesToBeRemoved);

                // Return boolean determining success of update
                return(result);
            }
        }
Example #3
0
        public ResponseDto <RestaurantProfileDto> GetProfile(string token)
        {
            var tokenService = new TokenService();
            var restaurantBusinessHourDtoService = new RestaurantBusinessHourDtoService();

            // Retrieve account by username
            var userGateway = new UserGateway();

            // Call the gateway
            var userAccountResponseDto = userGateway.GetUserByUsername(tokenService.GetTokenUsername(token));

            // Retrieve restaurant profile from database
            var restaurantProfileGateway = new RestaurantProfileGateway();

            var restaurantProfileResponseDto = restaurantProfileGateway.GetRestaurantProfileById(userAccountResponseDto.Data.Id);

            // Call the RestaurantBusinessHourDtoService
            var convertedRestaurantBusinessHourDtos = restaurantBusinessHourDtoService.SetStringTimesFromDateTimes(restaurantProfileResponseDto.Data.BusinessHours);

            // Replace the BusinessHourDtos with the converted ones
            restaurantProfileResponseDto.Data.BusinessHours = convertedRestaurantBusinessHourDtos;

            return(restaurantProfileResponseDto);
        }
Example #4
0
        /// <summary>
        /// The SelectRestaurantForRegisteredUser method.
        /// Contains instructions to select a restaurant for a registered user which includes model validations and calls to third party APIs
        /// <para>
        /// @author: Jennifer Nguyen
        /// @update: 04/04/2018
        /// </para>
        /// </summary>
        /// <returns>SelectedRestaurantDto</returns>
        public override ResponseDto <SelectedRestaurantDto> SelectRestaurant()
        {
            var username = _tokenService.GetTokenUsername(_token);

            if (username == null)
            {
                return(new ResponseDto <SelectedRestaurantDto>
                {
                    Data = null,
                    Error = GeneralErrorMessages.GENERAL_ERROR
                });
            }

            // Validate RestaurantSelection data transfer object
            var result = _restaurantSelectionPreLogicValidationStrategy.ExecuteStrategy();

            if (result.Error != null)
            {
                return(new ResponseDto <SelectedRestaurantDto>
                {
                    Data = null,
                    Error = result.Error
                });
            }

            // Call GeocodeService to get geocoordinates of the client user
            var geocodeResponse = _geocodeService.Geocode(new Address(city: RestaurantSelectionDto.City, state: RestaurantSelectionDto.State));

            if (geocodeResponse.Data == null | geocodeResponse.Error != null)
            {
                return(new ResponseDto <SelectedRestaurantDto>
                {
                    Data = null,
                    Error = geocodeResponse.Error
                });
            }

            // Set the client user's geocoordinates to the RestaurantSelection data transfer object
            RestaurantSelectionDto.ClientUserGeoCoordinates = new GeoCoordinates()
            {
                Latitude  = geocodeResponse.Data.Latitude,
                Longitude = geocodeResponse.Data.Longitude
            };

            // Call TimeZoneService to get the timezone offset given the client user's geocoordinates
            var timeZoneResponse = _timeZoneService.GetOffset(RestaurantSelectionDto.ClientUserGeoCoordinates);

            if (timeZoneResponse.Error != null)
            {
                return(new ResponseDto <SelectedRestaurantDto>
                {
                    Data = null,
                    Error = GeneralErrorMessages.GENERAL_ERROR
                });
            }

            // Set the current Coordinate Universal Time (UTC) DateTime to RestaurantSelection data transfer object
            RestaurantSelectionDto.CurrentUtcDateTime = _dateTimeService.GetCurrentCoordinateUniversalTime();

            // Get current day of week local to the client user and set to RestaurantSelection data transfer object
            RestaurantSelectionDto.CurrentLocalDayOfWeek = _dateTimeService.GetCurrentLocalDayOfWeekFromUtc(timeZoneResponse.Data, RestaurantSelectionDto.CurrentUtcDateTime);

            // Get the user's food preferences from the database
            using (var userGateway = new UserGateway())
            {
                var gatewayResult = userGateway.GetFoodPreferencesByUsername(username);
                if (gatewayResult.Error != null)
                {
                    return(new ResponseDto <SelectedRestaurantDto>()
                    {
                        Data = null,
                        Error = gatewayResult.Error
                    });
                }

                RestaurantSelectionDto.FoodPreferences = gatewayResult.Data.FoodPreferences;
            }

            // Validate data transfer object before querying to select restaurant in the database
            result = _registeredUserRestaurantSelectionPostLogicValidationStrategy.ExecuteStrategy();
            if (result.Error != null)
            {
                return(new ResponseDto <SelectedRestaurantDto>
                {
                    Data = null,
                    Error = result.Error
                });
            }

            // Select a restaurant in the database
            using (var restaurantGateway = new RestaurantGateway())
            {
                var gatewayResult = restaurantGateway.GetRestaurantWithFoodPreferences(
                    city: RestaurantSelectionDto.City, state: RestaurantSelectionDto.State,
                    foodType: RestaurantSelectionDto.FoodType, distanceInMeters: (double)ConvertDistanceInMilesToMeters(RestaurantSelectionDto.DistanceInMiles),
                    avgFoodPrice: RestaurantSelectionDto.AvgFoodPrice,
                    currentUtcTimeOfDay: RestaurantSelectionDto.CurrentUtcDateTime.TimeOfDay,
                    currentLocalDayOfWeek: RestaurantSelectionDto.CurrentLocalDayOfWeek.ToString(),
                    location: RestaurantSelectionDto.Location, foodPreferences: RestaurantSelectionDto.FoodPreferences);

                if (gatewayResult.Error != null)
                {
                    return(new ResponseDto <SelectedRestaurantDto>()
                    {
                        Data = null,
                        Error = gatewayResult.Error
                    });
                }
                // If Data is null then that means there is not a restaurant within the user's selection criteria
                if (gatewayResult.Data == null)
                {
                    return(new ResponseDto <SelectedRestaurantDto>()
                    {
                        Data = null
                    });
                }

                // Set the result of the gateway query to the SelectedRestaurant data transfer object
                SelectedRestaurantDto = gatewayResult.Data;
            }

            // Sort the list of business hour data transfer objects by day using the DayOfWeek enum property
            SelectedRestaurantDto.BusinessHourDtos = SelectedRestaurantDto.BusinessHourDtos
                                                     .OrderBy(businessHourDto => (int)Enum.Parse(typeof(DayOfWeek), businessHourDto.Day))
                                                     .ThenBy(businessHourDto => businessHourDto.OpenTime)
                                                     .ToList();

            // Sort the food preferences by ascending order
            SelectedRestaurantDto.FoodPreferences = SelectedRestaurantDto.FoodPreferences
                                                    .OrderBy(foodPreferences => foodPreferences).ToList();

            // Return the selected restaurant
            return(new ResponseDto <SelectedRestaurantDto>()
            {
                Data = SelectedRestaurantDto
            });
        }
Example #5
0
        public ResponseDto <bool> EditProfile(RestaurantProfileDto restaurantProfileDto, string token)
        {
            var geocodeService = new GoogleGeocodeService();
            var restaurantBusinessHourDtoService = new RestaurantBusinessHourDtoService();
            var tokenService = new TokenService();

            var editRestaurantProfilePreLogicValidationStrategy = new EditRestaurantUserProfilePreLogicValidationStrategy(restaurantProfileDto);

            var result = editRestaurantProfilePreLogicValidationStrategy.ExecuteStrategy();

            if (result.Error != null)
            {
                return(new ResponseDto <bool>
                {
                    Data = false,
                    Error = GeneralErrorMessages.GENERAL_ERROR
                });
            }

            // Retrieve account by username
            var userGateway = new UserGateway();

            var userAccountResponseDto = userGateway.GetUserByUsername(tokenService.GetTokenUsername(token));

            // Extrant user profile domain
            var userProfileDomain = new UserProfile
            {
                DisplayName = restaurantProfileDto.DisplayName
            };

            var geocodeResponse = geocodeService.Geocode(restaurantProfileDto.Address);

            if (geocodeResponse.Error != null)
            {
                return(new ResponseDto <bool>
                {
                    Data = false,
                    Error = GeneralErrorMessages.GENERAL_ERROR
                });
            }


            // Extract restaurant profile domain
            var restaurantProfileDomain = new RestaurantProfile(
                restaurantProfileDto.PhoneNumber,
                restaurantProfileDto.Address,
                restaurantProfileDto.Details)
            {
                GeoCoordinates = new GeoCoordinates(geocodeResponse.Data.Latitude, geocodeResponse.Data.Longitude)
            };

            // Extract business hours domains
            var restaurantBusinessHourDtos = restaurantProfileDto.BusinessHours;

            // Call the RestaurantBusinessHourDtoService
            var convertedRestaurantBusinessHourDtos = restaurantBusinessHourDtoService.SetDateTimesFromStringTimes(restaurantBusinessHourDtos);

            // Extract restaurant menus
            if (restaurantProfileDto.RestaurantMenusList.Count == 0)
            {
            }
            var restaurantMenuDomains = restaurantProfileDto.RestaurantMenusList;

            // Execute update of database
            var profileGateway = new RestaurantProfileGateway();

            var responseDtoFromGateway = profileGateway.EditRestaurantProfileById(userAccountResponseDto.Data.Id, userProfileDomain, restaurantProfileDomain, convertedRestaurantBusinessHourDtos, restaurantMenuDomains);

            return(responseDtoFromGateway);
        }