public async Task <Result <ICollection <RestaurantViewModel> > > HandleAsync(SysAdminSearchForRestaurantsQuery query, User currentUser, CancellationToken cancellationToken = default)
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }

            if (currentUser == null)
            {
                return(FailureResult <ICollection <RestaurantViewModel> > .Unauthorized());
            }

            if (currentUser.Role < Role.SystemAdmin)
            {
                return(FailureResult <ICollection <RestaurantViewModel> > .Forbidden());
            }

            var paymentMethods = (await paymentMethodRepository.FindAllAsync(cancellationToken))
                                 .ToDictionary(en => en.Id.Value, PaymentMethodViewModel.FromPaymentMethod);

            var restaurants = await restaurantRepository.SearchAsync(query.SearchPhrase, cancellationToken);

            return(SuccessResult <ICollection <RestaurantViewModel> > .Create(restaurants
                                                                              .Select(en => RestaurantViewModel.FromRestaurant(en, paymentMethods, userRepository)).ToList()));
        }
示例#2
0
        public async Task <Result <RestaurantViewModel> > HandleAsync(AddRestaurantCommand command, User currentUser, CancellationToken cancellationToken = default)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            if (currentUser == null)
            {
                return(FailureResult <RestaurantViewModel> .Unauthorized());
            }

            if (currentUser.Role < Role.SystemAdmin)
            {
                return(FailureResult <RestaurantViewModel> .Forbidden());
            }

            var paymentMethods = (await paymentMethodRepository.FindAllAsync(cancellationToken))
                                 .ToDictionary(en => en.Id.Value, PaymentMethodViewModel.FromPaymentMethod);

            var restaurant = restaurantFactory.CreateWithName(command.Name);
            await restaurantRepository.StoreAsync(restaurant, cancellationToken);

            return(SuccessResult <RestaurantViewModel> .Create(RestaurantViewModel.FromRestaurant(restaurant, paymentMethods, userRepository)));
        }
        public async Task <Result <ICollection <PaymentMethodViewModel> > > HandleAsync(GetAllPaymentMethodsQuery query, User currentUser, CancellationToken cancellationToken = default)
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }

            var paymentMethods = await paymentMethodRepository.FindAllAsync(cancellationToken);

            return(SuccessResult <ICollection <PaymentMethodViewModel> > .Create(paymentMethods.Select(PaymentMethodViewModel.FromPaymentMethod).ToList()));
        }
        public async Task <Result <RestaurantViewModel> > HandleAsync(GetRestaurantByIdQuery query, User currentUser, CancellationToken cancellationToken = default)
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }

            var paymentMethods = (await paymentMethodRepository.FindAllAsync(cancellationToken))
                                 .ToDictionary(en => en.Id.Value, PaymentMethodViewModel.FromPaymentMethod);

            var restaurant = await restaurantRepository.FindByRestaurantIdAsync(query.RestaurantId);

            if (restaurant == null)
            {
                return(FailureResult <RestaurantViewModel> .Create(FailureResultCode.RestaurantDoesNotExist));
            }

            return(SuccessResult <RestaurantViewModel> .Create(RestaurantViewModel.FromRestaurant(restaurant, paymentMethods, userRepository)));
        }