public async Task <ApiResponse <UserOutput> > Update([FromBody] UserInput userToUpdate)
        {
            var user = await _users.GetById(userToUpdate.Id);

            if (!user.Success)
            {
                return(ApiResponses.Failure <UserOutput>("User doesnt exists"));
            }

            var isUpdatingPassword = userToUpdate.Password != user.Response.Password;

            if (isUpdatingPassword)
            {
                var isPasswordConfirmed = userToUpdate.Password == userToUpdate.PasswordConfirmation;
                if (!isPasswordConfirmed)
                {
                    return(ApiResponses.Failure <UserOutput>("Passwords doesnt matches"));
                }
            }

            var userId     = User.Claims.ToList()[2];
            var modifierId = Guid.Parse(userId.Value);

            userToUpdate.Role = UserRoles.NonAdmin;
            return(await base.Update(userToUpdate, modifierId));
        }
        public override async Task <UseCaseResult <CheckProductAvailabilityResult> > Execute(Guid productId)
        {
            var productToCheckAvailability = await _products.GetById(productId);

            if (!productToCheckAvailability.Success)
            {
                return(UseCasesResponses.Failure <CheckProductAvailabilityResult>(productToCheckAvailability.Message));
            }

            if (productToCheckAvailability.Response.IsAvailable)
            {
                return(UseCasesResponses.Success(new CheckProductAvailabilityResult(productToCheckAvailability.Response.IsAvailable)));
            }

            var lastProductRent = await _productsRentalHistories.GetLastProductRent(productId);

            return(!lastProductRent.Success
                ? UseCasesResponses.Failure <CheckProductAvailabilityResult>(lastProductRent.Message)
                : UseCasesResponses.Success(new CheckProductAvailabilityResult(lastProductRent.Result.LastProductRent.RentPeriod.EndDate)));
        }
        public override async Task <UseCaseResult <RentRequirementValidationResult> > Validate(RentRequirement requirement)
        {
            var isTryingToRentWithoutProducts = requirement.ProductsIds == null;

            if (isTryingToRentWithoutProducts)
            {
                return(UseCasesResponses.Failure <RentRequirementValidationResult>("Trying to Rent without products"));
            }

            var productsToRent = await _products.GetByIds(requirement.ProductsIds.ToList());

            if (!productsToRent.Success)
            {
                return(UseCasesResponses.Failure <RentRequirementValidationResult>(productsToRent.Message));
            }

            var renter = await _renter.GetById(requirement.RenterId);

            if (!renter.Success)
            {
                return(UseCasesResponses.Failure <RentRequirementValidationResult>(renter.Message));
            }

            var rentPeriod = DateRangeProvider.GetDateRange(requirement.StartDate, requirement.EndDate);

            if (!rentPeriod.Success)
            {
                return(UseCasesResponses.Failure <RentRequirementValidationResult>(rentPeriod.Message));
            }

            var validationResult = new RentRequirementValidationResult
            {
                Products   = productsToRent.Response,
                RentPeriod = rentPeriod.Result,
                Renter     = renter.Response
            };

            return(UseCasesResponses.Success(validationResult));
        }
        public virtual async Task <UseCaseResult <TEntity> > GetById(Guid id)
        {
            var persistenceResponse = await ReadOnlyPersistenceGateway.GetById(id);

            return(UseCasesResponses.GetUseCaseResult(persistenceResponse));
        }