Example #1
0
        public async Task <Result <CuisineViewModel> > HandleAsync(AddCuisineCommand command, User currentUser, CancellationToken cancellationToken = default)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

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

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

            var cuisine = await cuisineRepository.FindByNameAsync(command.Name, cancellationToken);

            if (cuisine != null)
            {
                return(FailureResult <CuisineViewModel> .Create(FailureResultCode.CuisineAlreadyExists));
            }

            cuisine = cuisineFactory.Create(command.Name);
            await cuisineRepository.StoreAsync(cuisine, cancellationToken);

            return(SuccessResult <CuisineViewModel> .Create(CuisineViewModel.FromCuisine(cuisine)));
        }
        public async Task <Result <bool> > HandleAsync(ChangeCuisineCommand command, User currentUser, CancellationToken cancellationToken = default)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

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

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

            var cuisine = await cuisineRepository.FindByCuisineIdAsync(command.CuisineId, cancellationToken);

            if (cuisine == null)
            {
                return(FailureResult <bool> .Create(FailureResultCode.CuisineDoesNotExist));
            }

            cuisine.Change(command.Name);

            await cuisineRepository.StoreAsync(cuisine, cancellationToken);

            return(SuccessResult <bool> .Create(true));
        }