Beispiel #1
0
        /// <summary>
        /// Validates and converts stringDate to DateTime to call the GetDiaryEntryAsync(int userId, DateTime date) method
        /// </summary>
        /// <param name="username"></param>
        /// <param name="stringDate"></param>
        /// <returns>
        /// Unsuccessful DiaryEntryResponse if the stringDate is not in the format dd-mm-yyyy;
        /// A call to the GetDiaryEntry method with the converted date object
        /// </returns>
        public async Task <DiaryEntryResponse> GetDiaryEntryAsync(string username, string stringDate)
        {
            // convert the string date to a DateTime object
            DateTime date;

            try
            {
                date = StringDateHelper.ConverStringDateToDate(stringDate);
            }
            catch (ArgumentException e)
            {
                return(new DiaryEntryResponse(e.Message));
            }

            // call the GetDiaryEntryAsync method with the new date object
            return(await GetDiaryEntryAsync(username, date));
        }
Beispiel #2
0
        /// <summary>
        /// Adds a new food item to a users diary entry for a given date.
        /// If the user has no diary entry for that date, then one will be created.
        /// Updates the total calories for the diary entry.
        /// </summary>
        /// <param name="resource"></param>
        /// <returns>
        /// Unsuccessful FoodItemResponse if the user does not exist;
        /// Unsuccessful FoodItemResponse if the string date is not in the format dd-mm-yyyy;
        /// Unsuccessful FoodItemResponse if the date is in the future;
        /// Unsuccessful FoodItemResponse if there was an issue adding a new diary entry;
        /// Unsuccessful FoodItemResponse if there was an issue adding the food item to the diary entry;
        /// Successful FoodItemResponse with the food item
        /// </returns>
        public async Task <FoodItemResponse> AddFoodItemAsync(AddFoodItemResource resource)
        {
            // validate userId
            var userResult = await _userService.GetUserByNameAsync(resource.Username);

            if (!userResult.Success)
            {
                return(new FoodItemResponse(userResult.Message));
            }

            // validate and convert stringDate to DateTime
            DateTime date;

            try
            {
                date = StringDateHelper.ConverStringDateToDate(resource.StringDate);
            }
            catch (ArgumentException e)
            {
                return(new FoodItemResponse(e.Message));
            }

            DiaryEntry diaryEntry;

            // check if user has a diary entry for the day
            var existingDiaryResult = await _diaryEntryService.GetUsersDiaryEntryForDateAsync(userResult.User.Id, date);

            if (!existingDiaryResult.Success)
            {   // no diary entry for that day, create one
                var diaryEntryResult = await _diaryEntryService.AddDiaryEntryAsync(userResult.User.Id, date);

                if (!diaryEntryResult.Success)
                {
                    return(new FoodItemResponse($"Failed to add a new food item: {diaryEntryResult.Message}"));
                }

                diaryEntry = diaryEntryResult.DiaryEntry;
            }
            else
            {   // user has a diary entry for that day
                diaryEntry = existingDiaryResult.DiaryEntry;
            }

            // create food item
            FoodItem foodItem = new FoodItem
            {
                Name       = resource.Name,
                Calories   = resource.Calories,
                DiaryEntry = diaryEntry,
                User       = userResult.User
            };

            // update the total calories for the diary entry
            diaryEntry.TotalCalories += resource.Calories;

            // save the new food item and the changes to the diary entry
            try
            {
                await _foodItemRepository.AddAsync(foodItem);

                await _unitOfWork.CompleteAsync();
            }
            catch (Exception e)
            {
                return(new FoodItemResponse($"An error occurred when saving the food item: {e}"));
            }

            return(new FoodItemResponse(foodItem));
        }