private void SeedData(ModelBuilder modelBuilder) { var iphoneId = Guid.NewGuid(); var iphone = new Product() { Id = iphoneId, Name = "IPhone X", ProductRegisterDate = DateTime.UtcNow, UnitPrice = 1000 }; modelBuilder.Entity <Product>().HasData(iphone); var mobilePhoneId = Guid.NewGuid(); var mobilePhone = new Tag { Id = mobilePhoneId, Name = "Mobile phone" }; modelBuilder.Entity <Tag>().HasData(mobilePhone); var mobilePhoneIphone = new ProductsTags { Id = Guid.NewGuid(), ProductId = iphoneId, TagId = mobilePhoneId }; modelBuilder.Entity <ProductsTags>().HasData(mobilePhoneIphone); }
public IActionResult Add(ProductViewModel model, List <string> tags) { if (User.Identity.IsAuthenticated) { cartItems = unitOfWork.CartItems.GetUserCartItems(User.FindFirst(ClaimTypes.NameIdentifier).Value).ToList(); } if (ModelState.IsValid) { //------Create New Product to add to DataBase Product product = new Product { Name = model.Product.Name, Price = model.Product.Price, Discount = model.Product.Discount, LongDisc = model.Product.LongDisc, ShortDisc = model.Product.ShortDisc, Stock = model.Product.Stock, FK_CategoryId = model.Product.FK_CategoryId, FK_SellerId = userManager.GetUserId(HttpContext.User) }; //------Add product to DataBase //-------Get Product ID in DB int productId = unitOfWork.Products.Add(product).Id; //------Check Which items in checkBox is selected foreach (var item in model.PaymentMethods) { if (item.IsChecked == true) { //--------Add Record inn DB for Products and the Available Payment methods unitOfWork.ProductPayments .Add(new ProductsPayments { PaymentID = item.Id, ProductID = productId }); } } string uniqeFileName = null; if (model.photo != null) { //-------Get images Folder path in Server string uploaderFolder = Path.Combine(hostingEnvironment.WebRootPath, "imgs"); foreach (var item in model.photo) { //-------Create New Guid fo each image uniqeFileName = Guid.NewGuid().ToString() + "_" + item.FileName; //---------The full path for image string filePath = Path.Combine(uploaderFolder, uniqeFileName); //----------Copy image to server item.CopyTo(new FileStream(filePath, FileMode.Create)); //------Creat instance of image Image img = new Image { ImgPath = uniqeFileName, FK_ProductId = productId }; //-----Add image To DB unitOfWork.Images.Add(img); } if (tags.Count > 0) { foreach (var item in tags) { //------------Check if The Tag is already stored in DataBase Tag tag = unitOfWork.Tags.GetAll().Where(e => e.Name == item).FirstOrDefault(); //------if Tag isn`t stored in DB if (tag == null) { //------Create a new Tag tag = new Tag { Name = item }; //-------Store it in DB unitOfWork.Tags.Add(tag); } //------Create new instance of Product and Tag ProductsTags productTag = new ProductsTags { ProductID = productId, TagtID = tag.Id }; //----------Add the new Relation in DB ProductsTags productsTags = unitOfWork.ProductTag.Add(productTag); } } } return(RedirectToAction("SellerProducts", "ProductsPage", new { id = product.FK_SellerId })); } else { ProductViewModel productVM = new ProductViewModel(categories, cartItems) { //Categories = unitOfWork.Categories.GetAll().ToList(), PaymentMethods = unitOfWork.Payments.GetAll().ToList() }; return(View(productVM)); } }