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)); }
public async Task <Result <Guid> > HandleAsync(AddDishCategoryToRestaurantCommand command, User currentUser, CancellationToken cancellationToken = default) { if (command == null) { throw new ArgumentNullException(nameof(command)); } if (currentUser == null) { return(FailureResult <Guid> .Unauthorized()); } if (currentUser.Role < Role.RestaurantAdmin) { return(FailureResult <Guid> .Forbidden()); } var restaurant = await restaurantRepository.FindByRestaurantIdAsync(command.RestaurantId, cancellationToken); if (restaurant == null) { return(FailureResult <Guid> .Create(FailureResultCode.RestaurantDoesNotExist)); } if (currentUser.Role == Role.RestaurantAdmin && !restaurant.HasAdministrator(currentUser.Id)) { return(FailureResult <Guid> .Forbidden()); } var dishCategory = new DishCategory(new DishCategoryId(Guid.NewGuid()), command.RestaurantId, command.Name); await dishCategoryRepository.StoreAsync(dishCategory, cancellationToken); return(SuccessResult <Guid> .Create(dishCategory.Id.Value)); }
public async Task <Result <bool> > HandleAsync(RemoveUserCommand 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()); } if (command.UserId == currentUser.Id) { return(FailureResult <bool> .Create(FailureResultCode.CannotRemoveCurrentUser)); } await userRepository.RemoveAsync(command.UserId, cancellationToken); return(SuccessResult <bool> .Create(true)); }
public async Task <Result <bool> > HandleAsync(AddAdminToRestaurantCommand 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.RestaurantAdmin) { return(FailureResult <bool> .Forbidden()); } var restaurant = await restaurantRepository.FindByRestaurantIdAsync(command.RestaurantId, cancellationToken); if (restaurant == null) { return(FailureResult <bool> .Create(FailureResultCode.RestaurantDoesNotExist)); } if (currentUser.Role == Role.RestaurantAdmin && !restaurant.HasAdministrator(currentUser.Id)) { return(FailureResult <bool> .Forbidden()); } restaurant.AddAdministrator(command.UserId); await restaurantRepository.StoreAsync(restaurant, cancellationToken); return(SuccessResult <bool> .Create(true)); }
public async Task <Result <PaymentMethodViewModel> > HandleAsync(AddPaymentMethodCommand command, User currentUser, CancellationToken cancellationToken = default) { if (command == null) { throw new ArgumentNullException(nameof(command)); } if (currentUser == null) { return(FailureResult <PaymentMethodViewModel> .Unauthorized()); } if (currentUser.Role < Role.SystemAdmin) { return(FailureResult <PaymentMethodViewModel> .Forbidden()); } var paymentMethod = await paymentMethodRepository.FindByNameAsync(command.Name, cancellationToken); if (paymentMethod != null) { return(FailureResult <PaymentMethodViewModel> .Create(FailureResultCode.PaymentMethodAlreadyExists)); } paymentMethod = paymentMethodFactory.Create(command.Name, command.Description); await paymentMethodRepository.StoreAsync(paymentMethod, cancellationToken); return(SuccessResult <PaymentMethodViewModel> .Create(PaymentMethodViewModel.FromPaymentMethod(paymentMethod))); }
public async Task <Result <bool> > HandleAsync(RemoveRestaurantCommand 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 dishCategories = await dishCategoryRepository.FindByRestaurantIdAsync(command.RestaurantId, cancellationToken); if (dishCategories != null && dishCategories.Count > 0) { return(FailureResult <bool> .Create(FailureResultCode.RestaurantContainsDishCategories)); } var dishes = await dishRepository.FindByRestaurantIdAsync(command.RestaurantId, cancellationToken); if (dishes != null && dishes.Count > 0) { return(FailureResult <bool> .Create(FailureResultCode.RestaurantContainsDishes)); } await restaurantRepository.RemoveAsync(command.RestaurantId, cancellationToken); return(SuccessResult <bool> .Create(true)); }
public async Task Should_Execute_Task_Continuation() { var successResult = SuccessResult <int, int> .Create(1); var failureResult = FailureResult <int, int> .Create(2); Assert.AreEqual( 2, await successResult .OnSuccessAsync(s => Task.FromResult(s * 2)) .OnFailureAsync(f => Task.FromResult(f * 5)) .GetResultOrThrowExceptionAsync(f => new Exception())); Assert.AreEqual( 2, await successResult .OnSuccessAsync(s => SuccessResult <int, int> .CreateAsync(s * 2)) .OnFailureAsync(f => FailureResult <int, int> .CreateAsync(f * 5)) .GetResultOrThrowExceptionAsync(f => new Exception())); Assert.AreEqual( 4, await failureResult .OnFailureAsync(f => Task.FromResult(f * 2)) .OnSuccessAsync(s => Task.FromResult(s * 5)) .HandleAsync(s => 0, f => f)); Assert.AreEqual( 4, await failureResult .OnFailureAsync(f => FailureResult <int, int> .CreateAsync(f * 2)) .OnSuccessAsync(s => SuccessResult <int, int> .CreateAsync(s * 5)) .HandleAsync(s => 0, f => f)); }
public async Task <Result <bool> > HandleAsync(ChangeUserPasswordCommand 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 user = await userRepository.FindByUserIdAsync(command.UserId, cancellationToken); if (user == null) { return(FailureResult <bool> .Create(FailureResultCode.UserDoesNotExist)); } user.ChangePassword(command.Password); await userRepository.StoreAsync(user, cancellationToken); return(SuccessResult <bool> .Create(true)); }
public void Should_Throw_Exception_When_No_Result() { var failure = "failure"; var failableResult = FailureResult <string, string> .Create(failure); Assert.Throws <Exception>(() => failableResult.GetResultOrThrowException(f => new Exception())); }
public async Task <Result <UserViewModel> > HandleAsync(AddUserCommand command, User currentUser, CancellationToken cancellationToken = default) { if (command == null) { throw new ArgumentNullException(nameof(command)); } if (currentUser == null) { return(FailureResult <UserViewModel> .Unauthorized()); } if (currentUser.Role < Role.SystemAdmin) { return(FailureResult <UserViewModel> .Forbidden()); } var user = await userRepository.FindByNameAsync(command.Name, cancellationToken); if (user != null) { return(FailureResult <UserViewModel> .Create(FailureResultCode.UserAlreadyExists)); } user = userFactory.Create(command.Name, command.Role, command.Email, command.Password); await userRepository.StoreAsync(user, cancellationToken); return(SuccessResult <UserViewModel> .Create(UserViewModel.FromUser(user))); }
public async Task <Result <bool> > HandleAsync(ChangePaymentMethodCommand 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 paymentMethod = await paymentMethodRepository.FindByPaymentMethodIdAsync(command.PaymentMethodId, cancellationToken); if (paymentMethod == null) { return(FailureResult <bool> .Create(FailureResultCode.PaymentMethodDoesNotExist)); } paymentMethod.Change(command.Name, command.Description); await paymentMethodRepository.StoreAsync(paymentMethod, cancellationToken); return(SuccessResult <bool> .Create(true)); }
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 void Should_Not_Get_New_Result_On_Success_Failable_Result_When_Is_Failure() { var result = FailureResult <int, int> .Create(1) .OnSuccess(s => SuccessResult <int, int> .Create(s + 5)); Assert.AreEqual(1, result.Handle(s => s, f => f)); }
public void Should_Throw_Exception_When_Handler_Not_Provided() { var failure = "failure"; var failureResult = FailureResult <bool, string> .Create(failure); Assert.Throws <ArgumentNullException>(() => failureResult.Handle(r => r, null)); }
public void Should_Not_Get_New_Failure_Failable_Result_On_Failure_When_Is_Success() { var success = SuccessResult <int, int> .Create(2) .OnSuccess(s => s * 2) .OnFailure(f => FailureResult <int, int> .Create(f * 3)); Assert.AreEqual(4, success.Handle(s => s, f => f)); }
public void Should_Get_New_Failure_Failable_Result_When_On_Failure() { var failure = FailureResult <int, int> .Create(2) .OnSuccess(s => s * 2) .OnFailure(f => FailureResult <int, int> .Create(f * 3)); Assert.AreEqual(6, failure.Handle(s => s, f => f)); }
public static async Task <Result <TSuccessResult, FailureResult> > ExecuteAsync <TSuccessResult>( Func <Task <TSuccessResult> > action, Expression <Func <TSuccessResult, bool> > waitCondition, TimeSpan maxWaitTime, Func <int, TimeSpan> stepEngine, string timeoutMessage, IList <Type> notIgnoredExceptionType, Action <int, TimeSpan> callbackIfWaitSuccessful, bool continueOnCapturedContext = false) { var retryAttempt = 0; TSuccessResult? value = default; List <Exception> ex = new(); var wc = waitCondition.Compile(); var stopwatch = Stopwatch.StartNew(); do { try { value = await action().ConfigureAwait(continueOnCapturedContext); if (wc(value)) { callbackIfWaitSuccessful?.Invoke(retryAttempt, stopwatch.Elapsed); return(value); } } catch (Exception e) when(notIgnoredExceptionType.Any(x => x == e.GetType())) { throw; } catch (Exception e) { ex.Add(e); } if (retryAttempt < int.MaxValue) { retryAttempt++; } var sleep = stepEngine.Invoke(retryAttempt); var stopwatchElapsed = stopwatch.Elapsed; var canRetry = stopwatch.Elapsed < maxWaitTime; if (!canRetry) { var baseFailureResult = FailureResult.Create(retryAttempt, maxWaitTime, stopwatchElapsed, timeoutMessage); return(ex.Any() ? baseFailureResult.WhenExceptions(ex) : baseFailureResult.WhenNotExpectedValue(value, waitCondition !)); } await Task.Delay(sleep); } while (true); }
public void Should_Create_Failure_Result_When_Factory_Method_Used() { var failure = "failure"; var failureResult = FailureResult <bool, string> .Create(failure); Assert.AreEqual( failure, failureResult.Handle(r => string.Empty, f => f)); Assert.AreEqual(failure, (failureResult as FailureResult <bool, string>).Failure); }
public void Should_Not_Get_New_Result_On_Success_When_Is_Failure() { var failure = "failure"; var FailableResult = FailureResult <string, string> .Create(failure); var newResult = 2; var newFailableResult = FailableResult.OnSuccess(r => newResult); Assert.AreEqual("failure", newFailableResult.Handle(s => string.Empty, f => f)); }
public async Task <Result <bool> > HandleAsync(RemoveDishFromRestaurantCommand 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.RestaurantAdmin) { return(FailureResult <bool> .Forbidden()); } var restaurant = await restaurantRepository.FindByRestaurantIdAsync(command.RestaurantId, cancellationToken); if (restaurant == null) { return(FailureResult <bool> .Create(FailureResultCode.RestaurantDoesNotExist)); } if (currentUser.Role == Role.RestaurantAdmin && !restaurant.HasAdministrator(currentUser.Id)) { return(FailureResult <bool> .Forbidden()); } var dishCategories = await dishCategoryRepository.FindByRestaurantIdAsync(command.RestaurantId, cancellationToken); var dishCategory = dishCategories?.FirstOrDefault(en => en.Id == command.DishCategoryId); if (dishCategory == null) { return(FailureResult <bool> .Create(FailureResultCode.DishCategoryDoesNotBelongToRestaurant)); } var dishes = await dishRepository.FindByDishCategoryIdAsync(dishCategory.Id, cancellationToken); var dish = dishes?.FirstOrDefault(en => en.Id == command.DishId); if (dish == null) { return(FailureResult <bool> .Create(FailureResultCode.DishDoesNotBelongToDishCategory)); } await dishRepository.RemoveAsync(command.DishId, cancellationToken); return(SuccessResult <bool> .Create(true)); }
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))); }
public async Task <Result <ICollection <DishCategoryViewModel> > > HandleAsync(GetDishesOfRestaurantQuery query, User currentUser, CancellationToken cancellationToken = default) { if (query == null) { throw new ArgumentNullException(nameof(query)); } var restaurant = await restaurantRepository.FindByRestaurantIdAsync(query.RestaurantId, cancellationToken); if (restaurant == null) { return(FailureResult <ICollection <DishCategoryViewModel> > .Create(FailureResultCode.RestaurantDoesNotExist)); } var dishCategories = await dishCategoryRepository.FindByRestaurantIdAsync(query.RestaurantId, cancellationToken); var dishes = await dishRepository.FindByRestaurantIdAsync(query.RestaurantId, cancellationToken); var result = new List <DishCategoryViewModel>(); if (dishCategories != null) { foreach (var dishCategory in dishCategories) { var dishViewModels = new List <DishViewModel>(); if (dishes != null) { foreach (var dish in dishes.Where(en => en.CategoryId == dishCategory.Id)) { var variantViewModels = new List <DishVariantViewModel>(); if (dish.Variants != null) { foreach (var variant in dish.Variants) { var extraViewModels = new List <DishVariantExtraViewModel>(); if (variant.Extras != null) { foreach (var extra in variant.Extras) { extraViewModels.Add(new DishVariantExtraViewModel { ExtraId = extra.ExtraId, Name = extra.Name, ProductInfo = extra.ProductInfo, Price = extra.Price }); } } variantViewModels.Add(new DishVariantViewModel { VariantId = variant.VariantId, Name = variant.Name, Price = variant.Price, Extras = extraViewModels }); } } dishViewModels.Add(new DishViewModel { Id = dish.Id.Value, Name = dish.Name, Description = dish.Description, ProductInfo = dish.ProductInfo, Variants = variantViewModels }); } } result.Add(new DishCategoryViewModel { Id = dishCategory.Id.Value, Name = dishCategory.Name, Dishes = dishViewModels }); } } return(SuccessResult <ICollection <DishCategoryViewModel> > .Create(result)); }
public async Task <Result <Guid> > HandleAsync(AddOrChangeDishOfRestaurantCommand command, User currentUser, CancellationToken cancellationToken = default) { if (command == null) { throw new ArgumentNullException(nameof(command)); } if (currentUser == null) { return(FailureResult <Guid> .Unauthorized()); } if (currentUser.Role < Role.RestaurantAdmin) { return(FailureResult <Guid> .Forbidden()); } var restaurant = await restaurantRepository.FindByRestaurantIdAsync(command.RestaurantId, cancellationToken); if (restaurant == null) { return(FailureResult <Guid> .Create(FailureResultCode.RestaurantDoesNotExist)); } if (currentUser.Role == Role.RestaurantAdmin && !restaurant.HasAdministrator(currentUser.Id)) { return(FailureResult <Guid> .Forbidden()); } var dishCategories = await dishCategoryRepository.FindByRestaurantIdAsync(command.RestaurantId, cancellationToken); var dishCategory = dishCategories?.FirstOrDefault(en => en.Id == command.DishCategoryId); if (dishCategory == null) { return(FailureResult <Guid> .Create(FailureResultCode.DishCategoryDoesNotBelongToRestaurant)); } Dish dish; if (command.Dish.Id != Guid.Empty) { var dishes = await dishRepository.FindByDishCategoryIdAsync(dishCategory.Id, cancellationToken); dish = dishes?.FirstOrDefault(en => en.Id.Value == command.Dish.Id); if (dish == null) { return(FailureResult <Guid> .Create(FailureResultCode.DishDoesNotBelongToDishCategory)); } if (!string.Equals(dish.Name, command.Dish.Name)) { dish.ChangeName(command.Dish.Name); } if (!string.Equals(dish.Description, command.Dish.Description)) { dish.ChangeDescription(command.Dish.Description); } if (!string.Equals(dish.ProductInfo, command.Dish.ProductInfo)) { dish.ChangeProductInfo(command.Dish.ProductInfo); } dish.ReplaceVariants(FromVariantViewModels(command.Dish.Variants)); } else { dish = new Dish( new DishId(Guid.NewGuid()), command.RestaurantId, command.DishCategoryId, command.Dish.Name, command.Dish.Description, command.Dish.ProductInfo, FromVariantViewModels(command.Dish.Variants) ); } await dishRepository.StoreAsync(dish, cancellationToken); return(SuccessResult <Guid> .Create(dish.Id.Value)); }