Exemple #1
0
        public async Task <IActionResult> Build([FromBody] BuildWalksPayload payload)
        {
            var(statusCode, createdWalks) = await _walkingService.BuildWalksAsync(UserId, payload);

            switch (statusCode)
            {
            case Services.StatusCode.Ok:
                return(Ok(createdWalks));

            case Services.StatusCode.DogWalkerNotFound:
                return(NotFound("Dog walking professional is not found."));

            case Services.StatusCode.SaveDogWalkerDbError:
                return(StatusCode(500, "Couldn't save the result to the DB."));

            default:
                return(StatusCode(500, "Unknown error."));
            }
        }
Exemple #2
0
        /// <summary>
        /// Creates and saves walks and dog pack if needed for provided dog walker.
        /// </summary>
        /// <param name="userId">The dog walker's ID.</param>
        /// <returns>Tuple of two values where left item is a status of
        /// the operation and the right one is walks created.</returns>
        public async Task <(StatusCode, IEnumerable <Walk>)> BuildWalksAsync(int userId, BuildWalksPayload payload)
        {
            var dogWalker = await _dogWalkersRepo.GetByIdAsync(userId);

            if (dogWalker == null)
            {
                return(StatusCode.DogWalkerNotFound, null);
            }

            if (dogWalker.BookedWalks == null)
            {
                var(status, dogPacks) = await BuildDogPacksAsync(userId);

                if (status == StatusCode.Ok)
                {
                    dogWalker.DogPacks = dogPacks.ToList();
                }
                else
                {
                    return(status, null);
                }
            }

            dogWalker.BookedWalks = _walksBuilder.Build(new WeekDaysSchedulePayload
            {
                DateFrom     = payload.DateFrom,
                DateTo       = payload.DateTo,
                Now          = DateTime.Now,
                DogPacks     = dogWalker.DogPacks,
                WalkDuration = dogWalker.DefaultWalkDuration,
                WorkingDays  = dogWalker.WorkingDays,
                PriceRates   = dogWalker.PriceRates
            }).ToList();

            var isSaved = await _dogWalkersRepo.Save(dogWalker);

            if (isSaved)
            {
                return(StatusCode.Ok, dogWalker.BookedWalks);
            }
            else
            {
                return(StatusCode.SaveDogWalkerDbError, null);
            }
        }