Example #1
0
        public (bool, int) Ekle(UrunEkleDuzenleVM model, List <string> resimler)
        {
            List <Resim> eklenecekResimler = new List <Resim>();

            foreach (var resim in resimler)
            {
                eklenecekResimler.Add(new Resim
                {
                    ResimAdi = resim,
                });
            }
            _urunRepository.Add(new Urun
            {
                UrunAdi      = model.Adi,
                Fiyat        = model.Fiyat.HasValue ? model.Fiyat.Value : 0,
                KategoriId   = model.KategoriId,
                MarkaId      = model.MarkaId,
                StokAdet     = model.StokAdet.HasValue ? model.StokAdet.Value : 0,
                UretimTarihi = model.UretimTarihi.HasValue ? model.UretimTarihi.Value : DateTime.Now,
                Resimler     = eklenecekResimler
            });
            var  sonEklenenUrun = _urunRepository.GetLast <int>(u => u.Id);
            Urun resimliUrun    = _urunRepository.GetTable().Include(u => u.Resimler).FirstOrDefault(u => u.Id == sonEklenenUrun.Id);

            resimliUrun.VitrinResmiId = resimliUrun.Resimler.ToList()[0].Id;
            _urunRepository.Update(resimliUrun);

            return(true, sonEklenenUrun.Id);
        }
        public int UrunEkle(Urun urun)
        {
            var sonuc = _repo.Get(x => x.UrunAdi == urun.UrunAdi);

            if (sonuc != null)
            {
                return(0);
            }
            else
            {
                _repo.Add(urun);
                _unit.Save();
                return(1);
            }
        }
 public async Task <IActionResult> UrunEkle(Urun entity, IEnumerable <IFormFile> file)
 {
     ViewBag.Kategoriler = new SelectList(kategoriRepo.GetAll(), "KategoriId", "KategoriAd");
     if (ModelState.IsValid && file != null)
     {
         Account    acc        = new Account("naimmobilya", "742112311237693", "nqeO8i7tnQpNPnSAQyESOqImU-I");
         Cloudinary cloudinary = new Cloudinary(acc);
         Urun       urun       = urunRepo.Add(entity);
         foreach (var item in file)
         {
             string filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\images", item.FileName);
             using (var stream = new FileStream(filePath, FileMode.Create))
             {
                 await item.CopyToAsync(stream);
             }
             UrunImage image = new UrunImage()
             {
                 UrunId = urun.UrunId
             };
             imageRepo.Add(image);
             var uploadParams = new ImageUploadParams()
             {
                 File     = new FileDescription(filePath),
                 PublicId = "" + image.UrunImageId
             };
             var uploadResult = cloudinary.Upload(uploadParams);
             image.ImageUrl = uploadResult.Uri.ToString();
             imageRepo.Update(image);
             if (System.IO.File.Exists(filePath))
             {
                 System.IO.File.Delete(filePath);
             }
         }
         TempData["message"] = $"{entity.UrunId} id li ürün eklendi";
         return(RedirectToAction("UrunList"));
     }
     return(View(entity));
 }