Esempio n. 1
0
        public async Task <long> AddRecipe(AddRecipeCommand command)
        {
            long recipeId;

            try
            {
                var selector = @"
                    insert into recipe (
                      title,
                      directions)
                    values (
                      @title,
                      @directions)
                    returning recipeId;";

                var parameters = new
                {
                    title      = command.Title,
                    directions = command.Directions
                };

                recipeId = await _context.Connection.ExecuteScalarAsync <long>(selector, parameters);
            }
            catch (Exception ex)
            {
                AddNotification("0", ex.Message);
                recipeId = 0;
            }

            return(recipeId);
        }
Esempio n. 2
0
        public async Task <bool> Add(AddRecipeCommand command)
        {
            var  rollBack = false;
            long recipeId = 0;

            await _context.Connection.OpenAsync();

            try
            {
                try
                {
                    recipeId = await AddRecipe(command);

                    rollBack = !(await AddRecipeIngredient(command, recipeId));
                }
                catch (Exception ex)
                {
                    this.AddNotification("0", ex.Message);
                    rollBack = true;
                }

                if (rollBack)
                {
                    await DeleteRecipe(recipeId);
                }
            }
            finally
            {
                recipeId = 0;
                _context.Connection.Close();
            }

            return(recipeId > 0);
        }
Esempio n. 3
0
        public async Task <IActionResult> Add([FromBody] AddRecipeCommand command)
        {
            try
            {
                var resultado = (GenericResult)await _handler.Handle(command);

                return(await Response(resultado.Dados, null));
            }
            catch (Exception ex)
            {
                var notifications = new List <Notification>();
                notifications.Add(new Notification("Add Recipe", ex.Message));
                return(await Response(null, notifications));
            }
        }
Esempio n. 4
0
        public async Task <bool> AddRecipeIngredient(AddRecipeCommand command, long recipeId)
        {
            try
            {
                var selector = @"
                    insert into recipeIngredient (
                      amount,
                      unitOfMeasurement,
                      ingredient,
                      recipeId)
                    values (
                      @amount,
                      @unitOfMeasurement,
                      @ingredient,
                      @recipeId)";

                foreach (var item in command.Ingredients)
                {
                    var parameters = new
                    {
                        amount            = item.Amount,
                        unitOfMeasurement = item.UnitOfMeasurement,
                        ingredient        = item.Ingredient.IngredientId,
                        recipeId          = recipeId
                    };

                    await _context.Connection.ExecuteAsync(selector, command);
                }
            }
            catch (Exception ex)
            {
                AddNotification("0", ex.Message);
                return(false);
            }

            return(true);
        }