public string DodajNovuKategoriju(string catName) { string id; using (Db db = new Db()) { if (db.Kategorije.Any(x => x.Name == catName)) { return("titletaken"); } KategorijaDTO dto = new KategorijaDTO(); dto.Name = catName; dto.Slug = catName.Replace(" ", "-").ToLower(); dto.Sorting = 100; db.Kategorije.Add(dto); db.SaveChanges(); id = dto.Id.ToString(); } return(id); }
public KategorijaVM(KategorijaDTO row) { Id = row.Id; Name = row.Name; Slug = row.Slug; Sorting = row.Sorting; }
public bool SpremiKategoriju(KategorijaDTO kategorija) { string query = "INSERT INTO [dbo].[Kategorija] ([IdRegata],[IdStarosnaKategorija],[IdCamac],[Kratica],[BrojKategorije]) VALUES(@IdRegata,@IdStarosnaKategorija,@IdCamac,@Kratica,@BrojKategorije)"; using (var connection = new SqlConnection(Repozitorij.Konekcija)) { var brRedova = connection.Execute(query, kategorija); return(brRedova > 0); } }
// GET: Administrator/Prodavnica/ObrisiKategoriju public ActionResult ObrisiKategoriju(int id) { using (Db db = new Db()) { KategorijaDTO dto = db.Kategorije.Find(id); db.Kategorije.Remove(dto); db.SaveChanges(); } return(RedirectToAction("Kategorije")); }
public ActionResult Category(string name) { List <ProductVM> productVMList; using (Db db = new Db()) { KategorijaDTO categoryDTO = db.Kategorije.Where(x => x.Slug == name).FirstOrDefault(); int catId = categoryDTO.Id; productVMList = db.Products.ToArray().Where(x => x.CategoryId == catId).Select(x => new ProductVM(x)).ToList(); var productCat = db.Products.Where(x => x.CategoryId == catId).FirstOrDefault(); ViewBag.CategoryName = productCat.CategoryName; } return(View(productVMList)); }
public ActionResult Index(KategorijaDTO kategorija) { var kategorijeRepozitorij = new KategorijaRepozitorij(); var uspjeh = kategorijeRepozitorij.SpremiKategoriju(kategorija); var regataRepozitorij = new RegataRepozitorij(); var regate = regataRepozitorij.DohvatiRegate(); var starosneKategorije = kategorijeRepozitorij.DohvatiStarosneKategorije(); var camci = kategorijeRepozitorij.DohvatiCamce(); var viewModel = new KateogrijaViewModel() { Regate = regate, Camci = camci, StarosneKategorije = starosneKategorije }; return(View(viewModel)); }
public string RenameKategoriju(string newCatName, int id) { using (Db db = new Db()) { if (db.Kategorije.Any(x => x.Name == newCatName)) { return("titletaken"); } KategorijaDTO dto = db.Kategorije.Find(id); dto.Name = newCatName; dto.Slug = newCatName.Replace(" ", " - ").ToLower(); db.SaveChanges(); } return("ok"); }
public ActionResult EditProduct(ProductVM model, HttpPostedFileBase file) { int id = model.Id; using (Db db = new Db()) { model.Kategorije = new SelectList(db.Kategorije.ToList(), "Id", "Name"); } model.Galerija = Directory.EnumerateFiles(Server.MapPath("~/Images/Uploads/Products/" + id + "/Gallery/Thumbs")) .Select(fn => Path.GetFileName(fn)); if (!ModelState.IsValid) { return(View(model)); } using (Db db = new Db()) { if (db.Products.Where(x => x.Id != id).Any(x => x.Name == model.Name)) { ModelState.AddModelError("", "Naziv proizvoda vec postoji !"); } } //Azuriranje proizvoda using (Db db = new Db()) { ProductDTO dto = db.Products.Find(id); dto.Name = model.Name; dto.Slug = model.Name.Replace(" ", "-").ToLower(); dto.Description = model.Description; dto.Price = model.Price; dto.CategoryId = model.CategoryId; dto.ImageName = model.ImageName; KategorijaDTO catDTO = db.Kategorije.FirstOrDefault(x => x.Id == model.CategoryId); dto.CategoryName = catDTO.Name; db.SaveChanges(); } TempData["SM"] = "Upravo ste izmenili proizvod"; #region Image Upload //Proveravamo da li je fajl uplodovan if (file != null && file.ContentLength > 0) { string ext = file.ContentType.ToLower(); //Verifikacija ekstenzije if (ext != "image/jpg" && ext != "image/jpeg" && ext != "image/pjpeg" && ext != "image/gif" && ext != "image/x-png" && ext != "iamge/png") { using (Db db = new Db()) { ModelState.AddModelError("", "Fotografija nije ubacena - pogresna ekstenzija"); return(View(model)); } } //Setovanje putanje do foldera var originalDirectory = new DirectoryInfo(string.Format("{0}Images\\Uploads", Server.MapPath(@"\"))); var pathString1 = Path.Combine(originalDirectory.ToString(), "Products\\" + id.ToString()); var pathString2 = Path.Combine(originalDirectory.ToString(), "Products\\" + id.ToString() + "\\Thumbs"); //Brisanje fajla iz foldera DirectoryInfo di1 = new DirectoryInfo(pathString1); DirectoryInfo di2 = new DirectoryInfo(pathString2); foreach (FileInfo file2 in di1.GetFiles()) { file2.Delete(); } foreach (FileInfo file3 in di2.GetFiles()) { file3.Delete(); } //Cuvanje fotografija string imageName = file.FileName; using (Db db = new Db()) { ProductDTO dto = db.Products.Find(id); dto.ImageName = imageName; db.SaveChanges(); } var path = string.Format("{0}\\{1}", pathString1, imageName); var path2 = string.Format("{0}\\{1}", pathString2, imageName); file.SaveAs(path); WebImage img = new WebImage(file.InputStream); img.Resize(200, 200); img.Save(path2); } #endregion return(RedirectToAction("EditProduct")); }
public ActionResult AddProduct(ProductVM model, HttpPostedFileBase file) { if (!ModelState.IsValid) { using (Db db = new Db()) { model.Kategorije = new SelectList(db.Kategorije.ToList(), "Id", "Name"); return(View(model)); } } using (Db db = new Db()) { if (db.Products.Any(x => x.Name == model.Name)) { model.Kategorije = new SelectList(db.Kategorije.ToList(), "Id", "Name"); ModelState.AddModelError("", "Dati proizvod vec postoji"); return(View(model)); } } int id; using (Db db = new Db()) { ProductDTO product = new ProductDTO(); product.Name = model.Name; product.Slug = model.Name.Replace(" ", "-").ToLower(); product.Description = model.Description; product.Price = model.Price; product.CategoryId = model.CategoryId; KategorijaDTO catDTO = db.Kategorije.FirstOrDefault(x => x.Id == model.CategoryId); product.CategoryName = catDTO.Name; db.Products.Add(product); db.SaveChanges(); id = product.Id; } TempData["SM"] = "Ddodali ste proizvod"; #region Upload Image var originalDirectory = new DirectoryInfo(string.Format("{0}Images\\Uploads", Server.MapPath(@"\"))); var pathString1 = Path.Combine(originalDirectory.ToString(), "Products"); var pathString2 = Path.Combine(originalDirectory.ToString(), "Products\\" + id.ToString()); var pathString3 = Path.Combine(originalDirectory.ToString(), "Products\\" + id.ToString() + "\\Thumbs"); var pathString4 = Path.Combine(originalDirectory.ToString(), "Products\\" + id.ToString() + "\\Gallery"); var pathString5 = Path.Combine(originalDirectory.ToString(), "Products\\" + id.ToString() + "\\Gallery\\Thumbs"); if (!Directory.Exists(pathString1)) { Directory.CreateDirectory(pathString1); } if (!Directory.Exists(pathString2)) { Directory.CreateDirectory(pathString2); } if (!Directory.Exists(pathString3)) { Directory.CreateDirectory(pathString3); } if (!Directory.Exists(pathString4)) { Directory.CreateDirectory(pathString4); } if (!Directory.Exists(pathString5)) { Directory.CreateDirectory(pathString5); } if (file != null && file.ContentLength > 0) { string ext = file.ContentType.ToLower(); if (ext != "image/jpg" && ext != "image/jpeg" && ext != "image/pjpeg" && ext != "image/gif" && ext != "image/x-png" && ext != "iamge/png") { using (Db db = new Db()) { model.Kategorije = new SelectList(db.Kategorije.ToList(), "Id", "Name"); ModelState.AddModelError("", "Fotografija nije ubacena - pogresna ekstenzija"); return(View(model)); } } string imageName = file.FileName; using (Db db = new Db()) { ProductDTO dto = db.Products.Find(id); dto.ImageName = imageName; db.SaveChanges(); } var path = string.Format("{0}\\{1}", pathString2, imageName); var path2 = string.Format("{0}\\{1}", pathString3, imageName); file.SaveAs(path); WebImage img = new WebImage(file.InputStream); img.Resize(200, 200); img.Save(path2); } #endregion return(RedirectToAction("AddProduct")); }