public static UserProduct StaticDtoToDB(UserProductDto dto) { return(new UserProduct { Id = dto.Id, Category_Id = dto.Category_Id, Currency_Id = dto.Currency_Id, UnitType_Id = dto.Unit_Id, User_Id = dto.User_Id, Name = dto.Name, Price = dto.Price, Product_Id = dto.ProductId }); }
public void Create(UserProductDto userProduct) { ProductRepository productRepository = new ProductRepository(); productRepository.Create(new UserProduct { Id = userProduct.Id, Product_Id = userProduct.ProductId, Category_Id = userProduct.Category_Id, Currency_Id = userProduct.Currency_Id, UnitType_Id = userProduct.Unit_Id, User_Id = userProduct.User_Id, Name = userProduct.Name, Price = userProduct.Price }); }
public static string ExportUserProducts(ProductContext context) { var sb = new StringBuilder(); var userProducts = context.Users.Where(p => p.ProductsSold.Any()) .Select(p => new UserDto { FirstName = p.FirstName, LastName = p.LastName, Age = p.Age, SoldProducts = new SellerProductDto { Count = p.ProductsSold.Count(), Products = p.ProductsSold.Select(k => new SellerSoldProductDto { Name = k.Name, Price = k.Price }) .ToArray() } }) .OrderByDescending(p => p.SoldProducts.Count) .ThenBy(p => p.LastName) .ToArray(); var totalUsers = new UserProductDto { Count = userProducts.Length, Users = userProducts }; var serializer = new XmlSerializer(typeof(UserProductDto), new XmlRootAttribute("users")); serializer.Serialize(new StringWriter(sb), totalUsers, new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty })); return(sb.ToString()); }
public ActionResult EditItem(FormCollection collection) { try { var name = collection["Name"]; var reusable = collection["UserProduct"]; var price = decimal.Parse(collection["Price"]); var listId = int.Parse(collection["ListId"]); var qty = uint.Parse(collection["Quantity"]); var unitTypeId = int.Parse(collection["UnitTypesListId"]); int catId = 0; int userCatId = 0; if (collection["CategoryListId"] != "") { catId = int.Parse(collection["CategoryListId"]); } var userCat = collection["UserCategory"]; var currencyId = int.Parse(collection["CurrencyListId"]); var prodTypeId = int.Parse(collection["ProductTypeId"]); var prodId = int.Parse(collection["ProductId"]); /* UPDATING ShoppingListEntry */ ShoppingListEntryService entryService = new ShoppingListEntryService(); ShoppingListEntryDto entry = new ShoppingListEntryDto { Id = entryService.GetEntryId(prodId, listId), Quantity = (int)qty, ProductTypeId = prodTypeId, ShoppingList_Id = listId, Product_Id = prodId, State_Id = 2 //Default is unchecked }; entryService.Update(entry); //updates ShoppingListEntry /* CREATE NEW UserCatgory */ if (userCat != "") { LanguageService languageService = new LanguageService(); 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); } ProductService productService = new ProductService(); if (prodTypeId == 4 || prodTypeId == 3) { if (reusable == "false") { prodTypeId = 4; //4 = UserListProduct = non reusable } else { prodTypeId = 3; } /* UPDATING UserProduct */ UserProductDto userProduct = new UserProductDto { Id = productService.GetUserProductId(prodId), 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; productService.Update(userProduct); } else if (prodTypeId == 1) //if default Product { if (catId != 0) { //create LinkDefaultProductToCategory entry } else if (userCatId != 0) { //create LinkDefaultProductToCategory entry } } /* UPDATING Product -> for new Timestamp*/ ProductDto productDto = new ProductDto { Id = prodId, ProductTypeId = prodTypeId }; productService.Update(productDto); /* UPDATING ShoppingList -> for new Timestamp: */ ShoppingListDto shoppingList = new ShoppingListDto { Id = listId }; ShoppingListService listService = new ShoppingListService(); listService.Update(shoppingList); TempData["SuccessMessage"] = "Successfully edited this item"; return(RedirectToAction("SingleList", new { @id = listId })); } catch { TempData["ErrorMessage"] = "There was an error while editing the item"; return(Redirect(Request.UrlReferrer.ToString())); } }
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 UserProduct ConvertDtoToDB(UserProductDto dto) { return(StaticDtoToDB(dto)); }
public void Update(UserProductDto dto) { _prodRepository.Update(ConvertDtoToDB(dto)); }
public void Create(UserProductDto dto) { _prodRepository.Create(StaticDtoToDB(dto)); }