public static LinkUserToDefaultProduct StaticDefaultProductDtoToLinkDB(DefaultProductDto dto, int userId)
 {
     return(new LinkUserToDefaultProduct
     {
         UserId = userId,
         DefaultProductId = dto.Id,
         Timestamp = DateTime.Now
     });
 }
        //create Linking for User if DefaultProduct used as entry
        public void CreateLink(DefaultProductDto defaultProduct, int userId)
        {
            var link = _prodRepository.GetLinkUserToDefaultProduct(userId, defaultProduct.Id);

            if (link == null)
            {
                _prodRepository.Create(ConvertDefaultProductDtoToLinkDB(defaultProduct, userId));
            }
            //else if(link != null) if link already exists do nothing
        }
 public static DefaultProduct StaticDtoToDB(DefaultProductDto dto)
 {
     return(new DefaultProduct
     {
         Product_Id = dto.ProductTypeId,
         //Category_Id = dto.Category_Id,
         Currency_Id = dto.Currency_Id,
         UnitType_Id = dto.Unit_Id,
         //Name = dto.Name,
         Price = dto.Price
     });
 }
        public void Create(DefaultProductDto dto, int langId)
        {
            var product = new Product
            {
                Id             = dto.ProductId,
                Timestamp      = DateTime.Now,
                ProductType_Id = dto.ProductTypeId
            };
            var prodId = _prodRepository.CreateProduct(product);

            var defaultProduct = new DefaultProduct
            {
                Id          = dto.Id,
                Product_Id  = prodId,
                Currency_Id = dto.Currency_Id,
                UnitType_Id = dto.Unit_Id,
                Price       = dto.Price,
            };

            _prodRepository.Create(defaultProduct);

            // Save name
            var translation = new TranslationOfProduct
            {
                Language_Id = langId,
                Product_Id  = prodId,
                Translation = dto.Name
            };

            _prodRepository.SaveDefaultProductName(translation);

            // Link category
            var categoryrelation = new LinkDefaultProductToCategory
            {
                DefaultProductId = prodId,
                CategoryId       = (int)dto.Category_Id
            };

            _prodRepository.SaveDefaultProductCategory(categoryrelation);
        }
        public void Create(DefaultProductDto dto)
        {
            var product = new Product
            {
                Id             = dto.ProductId,
                Timestamp      = DateTime.Now,
                ProductType_Id = dto.ProductTypeId
            };
            var prodId = _prodRepository.CreateProduct(product);

            var defaultProduct = new DefaultProduct
            {
                Id          = dto.Id,
                Product_Id  = prodId,
                Currency_Id = dto.Currency_Id,
                UnitType_Id = dto.Unit_Id,
                Price       = dto.Price,
            };

            _prodRepository.Create(defaultProduct);

            // Link category
        }
        public ActionResult CreateItem(FormCollection collection)
        {
            try
            {
                var name            = collection["Name"];
                var reusable        = collection["UserProduct"];
                var price           = decimal.Parse(collection["Price"]);
                var listId          = int.Parse(collection["ShoppingList_Id"]);
                var qty             = int.Parse(collection["quantity"]);
                var unitTypeId      = int.Parse(collection["UnitTypesListId"]); //TODO: create lists in view and get id (like for countries/languages)
                var catId           = int.Parse(collection["CategoryListId"]);
                var userCat         = collection["UserCategory"];
                int userCatId       = 0;
                var currencyId      = int.Parse(collection["CurrencyListId"]);
                int prodType        = 0;
                var prodId          = 0;
                var chosenProductId = collection["ChosenProductId"]; //gets defaultProductId from dropdown

                if (name != "" && chosenProductId != "")             //Name filled in and defaultProduct chosen
                {
                    throw new Exception("You can either create a new item or choose from the default products, but not both!");
                }

                LanguageService languageService = new LanguageService();
                if (userCat != "")
                {
                    CategoryDto category = new CategoryDto
                    {
                        Name       = userCat,
                        LanguageId = languageService.GetByCode(Session["LanguageCode"].ToString()).Id,
                        UserId     = int.Parse(Session["UserId"].ToString())
                    };

                    CategoryService categoryService = new CategoryService();
                    userCatId = categoryService.Create(category);
                }

                ShoppingListEntryService entryService   = new ShoppingListEntryService();
                ProductService           productService = new ProductService();

                if (name != "") //IF UserProduct -> create Product - ShoppingListEntry - UserProduct
                {
                    if (reusable == "false")
                    {
                        prodType = 4;   //4 = UserListProduct = non reusable
                    }
                    else
                    {
                        prodType = 3;   //reusable UserProduct
                    }

                    //create a new Product
                    ProductDto productDto = new ProductDto
                    {
                        ProductTypeId = prodType
                    };
                    prodId = productService.Create(productDto);

                    //create new UserProduct
                    UserProductDto userProduct = new UserProductDto
                    {
                        ProductId = prodId,
                        Name      = name
                    };
                    if (userCat != "")
                    {
                        userProduct.Category_Id = userCatId;
                    }
                    else
                    {
                        userProduct.Category_Id = catId;
                    }
                    userProduct.User_Id     = int.Parse(Session["UserId"].ToString());
                    userProduct.Unit_Id     = unitTypeId;
                    userProduct.Price       = price;
                    userProduct.Currency_Id = currencyId;

                    entryService.Create(userProduct);
                }
                else if (chosenProductId != "")   //IF DefaultProduct -> create ShoppingListEntry & LinkDefaultProductToUser
                {
                    //check if chosen defaultProduct or reusable UserProduct
                    prodId   = int.Parse(chosenProductId);
                    prodType = productService.GetProductTypeId(prodId);

                    if (prodType == 1)    //if DefaultProduct: create Link entry
                    {
                        DefaultProductDto defaultProductDto = new DefaultProductDto
                        {
                            Id = productService.GetDefaultProductId(prodId)
                        };
                        productService.CreateLink(defaultProductDto, int.Parse(Session["UserId"].ToString()));
                    }

                    //if reusable UserProduct: only create ShoppingListEntry
                }

                //create Entry if not existent right now!
                var existentEntries = entryService.GetEntriesByListId(listId);
                foreach (ShoppingListEntryDto shoppingListEntry in existentEntries)
                {
                    if (shoppingListEntry.Product_Id == prodId)
                    {
                        throw new Exception("You can't add the same product to your list twice.");
                    }
                }

                ShoppingListEntryDto entry = new ShoppingListEntryDto
                {
                    Quantity        = qty,
                    Product_Id      = prodId,
                    ShoppingList_Id = listId,
                    State_Id        = 2 //Default is unchecked
                };
                entryService.Create(entry);

                //Update ShoppingList to update Timestamp:
                ShoppingListDto shoppingList = new ShoppingListDto
                {
                    Id = listId
                };
                ShoppingListService listService = new ShoppingListService();
                listService.Update(shoppingList);

                // Update Sorting
                var listEntries = productService.GetEntriesAsProducts(listId, languageService.GetByCode(Session["LanguageCode"].ToString()).Id);
                UserListSortingService listSortingService = new UserListSortingService();
                var sortingId = listService.Get(listId, int.Parse(Session["UserId"].ToString())).ChosenSortingId;
                UserListSortingDto sorting = new UserListSortingDto();
                if (sortingId != null)
                {
                    sorting.Id = (int)sortingId;
                }
                sorting.ShoppingList_Id = listId;
                listSortingService.SaveSorting(sorting, listEntries);

                TempData["SuccessMessage"] = "Successfully created a new item";
                return(RedirectToAction("SingleList", new { @id = listId }));
            }
            catch
            {
                TempData["ErrorMessage"] = "There was an error while creating a new item. Be aware that you can only create a new item or choose from the dropdown list of default products and your own reusable items, but you can't do both.";
                return(Redirect(Request.UrlReferrer.ToString()));
            }
        }
 protected DefaultProduct ConvertDtoToDB(DefaultProductDto dto)
 {
     return(StaticDtoToDB(dto));
 }
 protected LinkUserToDefaultProduct ConvertDefaultProductDtoToLinkDB(DefaultProductDto defaultProductDto, int userId)
 {
     return(StaticDefaultProductDtoToLinkDB(defaultProductDto, userId));
 }