예제 #1
0
        public async Task <IActionResult> Create([Bind("Id,Ad,Aciklama,Fiyat,Dosyalar")] Urun urun)
        {
            if (ModelState.IsValid)
            {
                var dosyaYolu = Path.Combine(_hostEnvironment.WebRootPath, "resimler");
                if (!Directory.Exists(dosyaYolu))
                {
                    Directory.CreateDirectory(dosyaYolu);
                }
                foreach (var item in urun.Dosyalar)
                {
                    if (item.ContentType.Substring(0, 5) == "image")
                    {
                        //memory stream herkes 100 dosya yüklese bellek şişer ve file stream
                        //create ile dosya varsa üzerine yazar ama createNew ile hata fırlatır
                        using (var dosyaAkisi = new FileStream(Path.Combine(dosyaYolu, item.FileName), FileMode.Create))
                        {
                            //program çalışma zamanında iken ilgili dosyayı arka planda sunucuya kopyalar...
                            await item.CopyToAsync(dosyaAkisi);
                        }


                        urun.Resimler.Add(new Resim {
                            DosyaAdi = item.FileName
                        });
                    }
                }

                _context.Add(urun);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(urun));
        }
        public async Task <IActionResult> Kayit(Kullanici kullanici)
        {
            if (ModelState.IsValid)
            {
                var    salt      = "17.3.999+2/5-qxw28012.992";
                SHA256 sha       = new SHA256CryptoServiceProvider();
                var    pwEncrypt =
                    Convert.ToBase64String(sha.ComputeHash(Encoding.UTF8.GetBytes(kullanici.password)));
                var saltEncrypt = Convert.ToBase64String(sha.ComputeHash(Encoding.UTF8.GetBytes(salt)));

                kullanici.password =
                    Convert.ToBase64String(sha.ComputeHash(Encoding.UTF8.GetBytes(pwEncrypt + saltEncrypt)));

                // kullanici.password =
                //     Convert.ToBase64String(sha.ComputeHash(Encoding.UTF8.GetBytes(kullanici.password)));
                kullanici.username = kullanici.username.ToLower();

                // char.ToUpper(kullanici.username[0]) + kullanici.username.Substring(1); //capitalized

                var kullaniciKayitliMi = await _context.Kullanicilar.Where(x => x.username == kullanici.username)
                                         .SingleOrDefaultAsync();

                if (kullaniciKayitliMi == null)
                {
                    _context.Add(kullanici);
                    await _context.SaveChangesAsync();

                    TempData["Mesaj"] = kullanici.username + " aramıza hoşgeldin (:";

                    return(RedirectToAction("Giris", "KullaniciIslemleri"));
                }
                TempData["Mesaj"] = "Kullanıcı adı alınmış.. Başka bir kullanıcı adı seçiniz!";
            }
            return(View(kullanici));
        }
        public async Task <IActionResult> Create(Guid?id, [Bind("Ad,Aciklama,Fiyat,Dosya")] Urun urun)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    foreach (var item in urun.Dosya)
                    {
                        string guid     = Guid.NewGuid().ToString();
                        var    split    = item.FileName.Split(".");
                        var    fileName = split[0] + guid + "." + split[1]; //split[0] noktadan öncesi guid . ve noktadan sonrası

                        var tamDosyaAdi = Path.Combine(_dosyaYolu, fileName);
                        await using (var dosyaAkisi = new FileStream(tamDosyaAdi, FileMode.Create))
                        {
                            await item.CopyToAsync(dosyaAkisi);
                        }

                        urun.Resimler.Add(new Resim {
                            DosyaAdi = fileName
                        });
                    }
                }
                catch (NullReferenceException)
                {
                }

                if (id != null)
                {
                    urun.KategoriUrunler.Add(new KategoriUrun {
                        KategoriId = (Guid)id
                    });
                }
                // if (id != null) urun.Kategorileri.Add(await _context.Kategoriler.FindAsync(id = id));
                _context.Add(urun);
                await _context.SaveChangesAsync();

                urun.Ad = char.ToUpper(urun.Ad[0]) + urun.Ad.Remove(0, 1);
                if (id != null)
                {
                    return(RedirectToAction(nameof(KategorininUrunleri), new { id }));
                }
                try
                {
                    TempData["mesaj"] = urun.Ad + " ürünü Başarıyla Eklendi!";
                }
                catch
                {
                    TempData["mesaj"] = urun.Ad + " ürünü Oluşturulurken bir HATA oluştu!";
                    throw;
                }

                return(RedirectToAction(nameof(Index)));
            }

            return(View(urun));
        }
예제 #4
0
        public async Task <IActionResult> Create([Bind("Id,Adi,Aciklama")] Kategori kategori)
        {
            if (ModelState.IsValid)
            {
                _context.Add(kategori);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(kategori));
        }
예제 #5
0
        public async Task <IActionResult> Create([Bind("Id,Ad,Aciklama,Fiyat")] Urun urun)
        {
            if (ModelState.IsValid)
            {
                _context.Add(urun);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(urun));
        }
        public async Task <IActionResult> Create(int?id, [Bind("Ad,Aciklama,Fiyat,Dosyalar")] Urun urun)
        {
            if (ModelState.IsValid)
            {
                foreach (var item in urun.Dosyalar)
                {
                    // IFormFile   -->  FileStream   :upload
                    using (var dosyaAkisi = new FileStream(Path.Combine(_dosyaYolu, item.FileName), FileMode.Create))
                    {
                        await item.CopyToAsync(dosyaAkisi);
                    }

                    urun.Resimler.Add(new Resim {
                        DosyaAdi = item.FileName
                    });
                }

                //var x = _context.Kategoriler.Find(id);  ---> buna gerek yok
                if (id != null)
                {
                    urun.KategoriUrunleri.Add(new KategoriUrun {
                        KategoriId = (int)id
                    });
                }

                _context.Add(urun);
                await _context.SaveChangesAsync();

                TempData["Mesaj"] = $"{urun.Ad} ürünü başarıyla eklendi";

                if (id != null)
                {
                    return(RedirectToAction(nameof(KategorininUrunleri), new { id = id }));
                }
                return(RedirectToAction(nameof(Index))); //????
            }
            return(View(urun));
        }
예제 #7
0
 public void Insert(T entity)
 {
     Context.Add(entity);
 }