Пример #1
0
        public ActionResult AddCat(CatProfile userCreatedCat, HttpPostedFileBase uploadedPicture)
        {
            if (ModelState.IsValid == false)
            {
                return(View(userCreatedCat));
            }

            // izveido savienojumu ar datubāzi
            using (var catDb = new CatDb())
            {
                // pievienojam kaķi kaķu tabulā
                catDb.CatProfiles.Add(userCreatedCat);

                // saglabājam izmaiņas datubāzē
                catDb.SaveChanges();

                // ja ir pievienota profila bilde
                if (uploadedPicture != null)
                {
                    // izveidojam jaunu profila bildes datubāzes eksemplāru, ko ierakstīsim datubāzē
                    var profilePic = new File();

                    // saglabājam bildes faila nosaukumu
                    profilePic.FileName = Path.GetFileName(uploadedPicture.FileName);

                    // saglabājam bildes tipu
                    profilePic.ContentType = uploadedPicture.ContentType;

                    // izmantojam BinaryReader lai pārvērstu bildi baitos
                    using (var reader = new BinaryReader(uploadedPicture.InputStream))
                    {
                        // saglabājam baitus datubāzes ierakstā
                        profilePic.Content = reader.ReadBytes(uploadedPicture.ContentLength);
                    }

                    // pasakam profila bildei, kurš kaķa profils ir kaķa profils, kam šī bilde pieder
                    profilePic.CatProfileId = userCreatedCat.CatId;
                    profilePic.CatProfile   = userCreatedCat;

                    // pievienojam profila bildes datubāzes ierakstu Files tabulai
                    catDb.Files.Add(profilePic);

                    // saglabājam profila bildi datubāzē, lai iegūtu FileId priekš profila bildes ieraksta
                    catDb.SaveChanges();

                    // paskam kaķu profilam, kas ir viņa profila bilde
                    userCreatedCat.ProfilePicture = profilePic;

                    // saglabājam izmaiņas datubāzē
                    catDb.SaveChanges();
                }
            }

            // pavēlam browserim atgriezties Index lapā (t.i. pārlādēt to)
            return(RedirectToAction("Index"));
        }
        public ActionResult Create(Cat cat)
        {
            if (ModelState.IsValid)
            {
                _db.Cats.Add(cat);
                _db.SaveChanges();
                return(RedirectToAction("Index", "Cat"));
            }

            return(View(cat));
        }
Пример #3
0
        //lai dabūtu failu, ko var pievienot serverī, pievieno HttpPosted.... + to, ko ievadijām pie EditCats apakšā name=uploadedPicture
        public ActionResult EditCats(CatProfile catProfile, HttpPostedFileBase uploadedPicture)
        {
            if (ModelState.IsValid == false)
            {
                return(View(catProfile));
            }
            using (var CatDb = new CatDb())
            {
                var profilePic = new File();
                //saglabājam bildes faila nosaukumu
                profilePic.FileName    = System.IO.Path.GetFileName(uploadedPicture.FileName);
                profilePic.ContentType = uploadedPicture.ContentType;

                //BinaryReader pārvērš bildi baitos
                using (var reader = new System.IO.BinaryReader(uploadedPicture.InputStream))
                {
                    //saglabājam baitus datubāzes ierakstā
                    profilePic.Content = reader.ReadBytes(uploadedPicture.ContentLength);
                }

                profilePic.CatProfileId = catProfile.CatID;
                profilePic.CatProfile   = catProfile;

                CatDb.Files.Add(profilePic);

                catProfile.ProfilePicture = profilePic;

                CatDb.Entry(catProfile).State = EntityState.Modified;
                CatDb.SaveChanges();
            }

            return(RedirectToAction("Index"));
        }
Пример #4
0
        public ActionResult DeleteCats(int deletableCatId)
        {
            using (var catDb = new CatDb())
            {
                // atrast kaķi, kam pieder norādītais identifikators
                var deletableCat = catDb.CatProfiles.Include(catProf => catProf.ProfilePicture).First(record => record.CatId == deletableCatId);

                // atrast kaķa bildi, ja tāda ir
                if (deletableCat.ProfilePicture != null)
                {
                    // izdzēst šo bildi
                    catDb.Files.Remove(deletableCat.ProfilePicture);
                }

                // izdzēst šo kaķi no tabulas
                catDb.CatProfiles.Remove(deletableCat);

                // saglabāt veiktās izmaiņas datubāzē
                catDb.SaveChanges();
            }

            // jāpievieno using System.Net;
            // pavēlam browserim atgriezties Index lapā (t.i. pārlādēt to)
            return(RedirectToAction("Index"));
        }
Пример #5
0
        public ActionResult EditCats(int editableCatId)
        {
            using (var CatDb = new CatDb())
            {
                var editableCat = CatDb.CatProfiles.First(CatProfile => CatProfile.CatID == editableCatId);

                CatDb.SaveChanges();

                return(View("EditCats", editableCat));
            }
        }
Пример #6
0
        public ActionResult EditCat(CatProfile catProfile, HttpPostedFileBase uploadedPicture)
        {
            if (ModelState.IsValid == false)
            {
                return(View(catProfile));
            }

            using (var catDb = new CatDb())
            {
                // ja ir pievienota profila bilde
                if (uploadedPicture != null)
                {
                    // atrodam šobrīdējo bildi, ja tāda ir
                    var currentPic = catDb.Files.FirstOrDefault(fileRecord => fileRecord.CatProfileId == catProfile.CatId);
                    if (currentPic != null)
                    {
                        catDb.Files.Remove(currentPic);
                    }

                    // izveidojam jaunu profila bildes datubāzes eksemplāru, ko ierakstīsim datubāzē
                    var profilePic = new File();

                    // saglabājam bildes faila nosaukumu
                    profilePic.FileName = Path.GetFileName(uploadedPicture.FileName);

                    // saglabājam bildes tipu
                    profilePic.ContentType = uploadedPicture.ContentType;

                    // izmantojam BinaryReader lai pārvērstu bildi baitos
                    using (var reader = new BinaryReader(uploadedPicture.InputStream))
                    {
                        // saglabājam baitus datubāzes ierakstā
                        profilePic.Content = reader.ReadBytes(uploadedPicture.ContentLength);
                    }

                    // pasakam profila bildei, kurš kaķa profils ir kaķa profils, kam šī bilde pieder
                    profilePic.CatProfileId = catProfile.CatId;
                    profilePic.CatProfile   = catProfile;

                    // pievienojam profila bildes datubāzes ierakstu Files tabulai
                    catDb.Files.Add(profilePic);

                    // paskam kaķu profilam, kas ir viņa profila bilde
                    catProfile.ProfilePicture = profilePic;
                }

                // pievienot using System.Data.Entity;
                catDb.Entry(catProfile).State = EntityState.Modified;
                catDb.SaveChanges();
            }

            // pavēlam browserim atgriezties Index lapā (t.i. pārlādēt to)
            return(RedirectToAction("Index"));
        }
Пример #7
0
        public ActionResult AddCat(CatProfile userCreatedCat, HttpPostedFileBase uploadedPicture)
        {
            if (ModelState.IsValid == false)
            {
                return(View(userCreatedCat));
            }

            using (var catDb = new CatDb())
            {
                catDb.CatProfiles.Add(userCreatedCat);

                catDb.SaveChanges();

                if (uploadedPicture != null)
                {
                    var profilePic = new UploadedFiles.File();

                    profilePic.FileName = Path.GetFileName(uploadedPicture.FileName);

                    profilePic.ContentType = uploadedPicture.ContentType;

                    using (var reader = new BinaryReader(uploadedPicture.InputStream))
                    {
                        profilePic.Content = reader.ReadBytes(uploadedPicture.ContentLength);
                    }

                    profilePic.CatProfileIe = userCreatedCat.CatID;
                    profilePic.CatProfile   = userCreatedCat;

                    catDb.Files.Add(profilePic);
                    catDb.SaveChanges();

                    userCreatedCat.profilePicture = profilePic;

                    catDb.SaveChanges();
                }
            }

            return(RedirectToAction("Index"));
        }
        public ActionResult DeleteBlog(int deleteBlog)
        {
            using (var blogDb = new CatDb())
            {
                var deletableBlog = blogDb.Blog.First(record => record.BlogID == deleteBlog);

                blogDb.Blog.Remove(deletableBlog);

                blogDb.SaveChanges();
            }

            return(RedirectToAction("Index"));
        }
Пример #9
0
        public ActionResult AddCats(CatProfile userCreatedCat)
        {
            if (ModelState.IsValid == false)
            {
                return(View(userCreatedCat));
            }
            using (var CatDb = new CatDb())
            {
                CatDb.CatProfiles.Add(userCreatedCat);
                CatDb.SaveChanges();

                return(RedirectToAction("Index"));
            }
        }
 public ActionResult EditBlog(Blog editBlog)
 {
     if (ModelState.IsValid == false)
     {
         return(View(editBlog));
     }
     using (var blogDb = new CatDb())
     {
         blogDb.Entry(editBlog).State = System.Data.Entity.EntityState.Modified;
         editBlog.BlogModified        = DateTime.Now;
         blogDb.SaveChanges();
     }
     return(RedirectToAction("Index"));
 }
        public ActionResult AddBlog(Blog userCreatedBlog)
        {
            using (var blogDb = new CatDb())
            {
                blogDb.Blog.Add(userCreatedBlog);
                userCreatedBlog.BlogCreated  = DateTime.Now;
                userCreatedBlog.BlogModified = DateTime.Now;


                blogDb.SaveChanges();
            }

            return(RedirectToAction("Index"));
        }
Пример #12
0
        public ActionResult Edit(Blog model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            using (var db = new CatDb())
            {
                db.Entry(model).State = EntityState.Modified;
                db.SaveChanges();
            }

            return(RedirectToAction("Index"));
        }
Пример #13
0
        public ActionResult DeleteCat(int deletableCatId)
        {
            using (var catDb = new CatDb())
            {
                var deletableCat = catDb.CatProfiles.Include(catProf => catProf.profilePicture).First(record => record.CatID == deletableCatId);
                if (deletableCat.profilePicture != null)
                {
                    catDb.Files.Remove(deletableCat.profilePicture);
                }
                catDb.CatProfiles.Remove(deletableCat);


                catDb.SaveChanges();
            }

            return(RedirectToAction("Index"));
        }
Пример #14
0
        public ActionResult Create(Blog model)
        {
            if (ModelState.IsValid)
            {
                return(View(model));
            }

            model.BlogCreated  = DateTime.Now;
            model.BlogModified = DateTime.Now;

            using (var db = new CatDb())
            {
                db.Blogs.Add(model);
                db.SaveChanges();
            }

            return(RedirectToAction("Index"));
        }
Пример #15
0
        public ActionResult DeleteCats(int deletableCatId)
        {
            using (var catDb = new CatDb())
            {
                //atrast kaķi ar konkreto Id numuru
                var deletableCat = catDb.CatProfiles.First(CatProfile => CatProfile.CatID == deletableCatId);

                //izdzēst šo kaķi no tabulas
                catDb.CatProfiles.Remove(deletableCat);

                //saglabat veiktās izmaiņas datu bāzē
                catDb.SaveChanges();
            }

            //Jāpievieno using System.Net
            //pāvēlam browser atgriezties Index lapā (t.i. pārlādēt to)
            return(RedirectToAction("Index"));
        }
Пример #16
0
        public ActionResult EditCat(CatProfile catProfile, HttpPostedFileBase uploadedPicture)
        {
            if (ModelState.IsValid == false)
            {
                return(View(catProfile));
            }
            using (var catDb = new CatDb())
            {
                if (uploadedPicture != null)
                {
                    var currentPic = catDb.Files.FirstOrDefault(fileRecord => fileRecord.CatProfileIe == catProfile.CatID);
                    if (currentPic != null)
                    {
                        catDb.Files.Remove(currentPic);
                    }



                    var profilePic = new UploadedFiles.File();
                    profilePic.FileName    = Path.GetFileName(uploadedPicture.FileName);
                    profilePic.ContentType = uploadedPicture.ContentType;

                    profilePic.CatProfileIe = catProfile.CatID;
                    profilePic.CatProfile   = catProfile;

                    using (var reader = new BinaryReader(uploadedPicture.InputStream))
                    {
                        profilePic.Content = reader.ReadBytes(uploadedPicture.ContentLength);
                    }

                    catDb.Files.Add(profilePic);

                    catProfile.profilePicture = profilePic;
                }
                catDb.Entry(catProfile).State = System.Data.Entity.EntityState.Modified;
                catDb.SaveChanges();
            }

            return(RedirectToAction("Index"));
        }