public void UpdateProductRecipe(ProductRecipe productRecipe)
        {
            var dAProductRecipe = ManualMapper.ManMap(productRecipe);

            _db.Update(dAProductRecipe);
            _db.SaveChanges();
        }
예제 #2
0
        private static ProductRecipe[] ReadProductRecipes(string tableName)
        {
            int[] productionRange       = { 4, 13 };
            int[] resourceRange         = { 14, 23 };
            List <ProductRecipe> result = new List <ProductRecipe>();

            string sqlQuery = "SELECT id, name, description, duration, product_1, product_1_count, product_2, product_2_count, " +
                              $" product_3, product_3_count, product_4, product_4_count, product_5, product_5_count, " +
                              $"source_1, source_1_count, source_2, source_2_count, source_3, source_3_count, source_4, source_4_count," +
                              $"source_5, source_5_count FROM {tableName}";

            dbcmd.CommandText = sqlQuery;
            reader            = dbcmd.ExecuteReader();

            while (reader.Read())
            {
                List <GoodsStack> prod      = new List <GoodsStack>();
                List <GoodsStack> resources = new List <GoodsStack>();
                for (int i = productionRange[0]; i < productionRange[1]; i += 2)
                {
                    if (!reader.IsDBNull(i) && !reader.IsDBNull(i + 1))
                    {
                        int goodsId = reader.GetInt32(i);
                        int amount  = reader.GetInt32(i + 1);
                        prod.Add(new GoodsStack()
                        {
                            id     = goodsId,
                            amount = amount
                        });
                    }
                }

                for (int i = resourceRange[0]; i < resourceRange[1]; i += 2)
                {
                    if (!reader.IsDBNull(i) && !reader.IsDBNull(i + 1))
                    {
                        int goodId = reader.GetInt32(i);
                        int amount = reader.GetInt32(i + 1);
                        resources.Add(new GoodsStack()
                        {
                            id     = goodId,
                            amount = amount
                        });
                    }
                }
                ProductRecipe recipe = new ProductRecipe(
                    id: reader.GetInt32(0),
                    name: reader.GetString(1),
                    description: reader.GetString(2),
                    duration: reader.GetInt32(3),
                    resources: resources.ToArray(),
                    production: prod.ToArray()
                    );
                result.Add(recipe);
            }
            reader.Close();
            return(result.ToArray());
        }
예제 #3
0
        public async Task <int> AddRecipeAsync(string name, string description, int timeToPrepare, string imageUrl, string neededProducts, int subcategoryId, string userId)
        {
            string[] splittedProducts = neededProducts
                                        .Split(new[] { NewLine }, StringSplitOptions.None)
                                        .Where(sp => sp != string.Empty)
                                        .Select(sp => sp.TrimEnd(' ', ',', '.', '-', '_', '!', '?'))
                                        .Distinct()
                                        .ToArray();

            Recipe recipe = new Recipe
            {
                Name          = char.ToUpper(name[0]) + name.Substring(1).ToLower().TrimEnd(' ', ',', '.', '-', '_', '!', '?'),
                Description   = description,
                TimeToPrepare = timeToPrepare,
                ImageUrl      = imageUrl,
                SubcategoryId = subcategoryId,
                UserId        = userId,
            };

            await this.recipesRepository.AddAsync(recipe);

            await this.recipesRepository.SaveChangesAsync();

            foreach (var prod in splittedProducts)
            {
                string productWithFixedLetterCase = char.ToUpper(prod[0]) + prod.Substring(1).ToLower();

                Product product = this.productsRepository.All()
                                  .Where(p => p.Name == productWithFixedLetterCase)
                                  .FirstOrDefault();

                if (product == null)
                {
                    product = new Product
                    {
                        Name = productWithFixedLetterCase,
                    };

                    await this.productsRepository.AddAsync(product);

                    await this.productsRepository.SaveChangesAsync();
                }

                ProductRecipe productRecipe = new ProductRecipe
                {
                    RecipeId  = recipe.Id,
                    ProductId = product.Id,
                };

                await this.productRecipeRepository.AddAsync(productRecipe);
            }

            await this.productsRepository.SaveChangesAsync();

            return(recipe.Id);
        }
예제 #4
0
        public async Task UpdateRecipeAsync(int id, string name, string description, int timeToPrepare, string products)
        {
            var recipeToEdit = this.recipesRepository.All().Where(r => r.Id == id).FirstOrDefault();

            string[] filteredProducts = products
                                        .Split(new[] { NewLine }, StringSplitOptions.None)
                                        .Where(p => p != string.Empty)
                                        .Select(sp => sp.TrimEnd(' ', ',', '.', '-', '_', '!', '?'))
                                        .Distinct()
                                        .ToArray();

            foreach (var pr in filteredProducts)
            {
                string productWithFixedLetterCase = char.ToUpper(pr[0]) + pr.Substring(1).ToLower();

                if (this.productsRepository.All().FirstOrDefault(p => p.Name == productWithFixedLetterCase) == null)
                {
                    Product product = new Product {
                        Name = productWithFixedLetterCase
                    };
                    await this.productsRepository.AddAsync(product);

                    await this.productsRepository.SaveChangesAsync();

                    ProductRecipe productRecipe = new ProductRecipe {
                        ProductId = product.Id, RecipeId = id
                    };
                    await this.productRecipeRepository.AddAsync(productRecipe);
                }
                else
                {
                    var product = this.productsRepository.All().Where(p => p.Name == productWithFixedLetterCase)
                                  .FirstOrDefault();

                    if (!this.productRecipeRepository.All().Any(pr => pr.ProductId == product.Id &&
                                                                pr.RecipeId == id))
                    {
                        ProductRecipe productRecipe = new ProductRecipe
                        {
                            ProductId = product.Id,
                            RecipeId  = id,
                        };
                        await this.productRecipeRepository.AddAsync(productRecipe);
                    }
                }
            }

            await this.productRecipeRepository.SaveChangesAsync();

            recipeToEdit.Name          = char.ToUpper(name[0]) + name.Substring(1).ToLower().TrimEnd(' ', ',', '.', '-', '_', '!', '?');
            recipeToEdit.Description   = description;
            recipeToEdit.TimeToPrepare = timeToPrepare;

            await this.recipesRepository.SaveChangesAsync();
        }
예제 #5
0
        /// <summary>
        /// Updates the product recipe
        /// </summary>
        /// <param name="productRecipe">>Product recipe</param>
        public void UpdateProductRecipe(ProductRecipe productRecipe)
        {
            if (productRecipe == null)
            {
                throw new ArgumentNullException(nameof(productRecipe));
            }

            _productRecipeRepository.Update(productRecipe);

            //event notification
            _eventPublisher.EntityUpdated(productRecipe);
        }
 public void InsertProductRecipe(ProductRecipe productRecipe)
 {
     _db.Add(ManualMapper.ManMap(productRecipe));
     _db.SaveChanges();
 }