Esempio n. 1
0
        public ActionResult Edit(int id)
        {
            var db = new ChiZodiacDb("chinesezodiac.db");
            var an = (from a in db.Animals where a.Id == id select a).Single();

            // didn't check for failure... DANGER!
            return(View(an));
        }
Esempio n. 2
0
        private bool ValidInput(CZAnimal animal, bool editingExisting)
        {
            // basic sensible input checks
            if (animal.Name.Trim().Length == 0)
            {
                ModelState.AddModelError("Name", "Please add a name!");
            }
            if (animal.Position.GetType() != typeof(int) ||
                animal.Position < 1 ||
                animal.Position > 12)
            {
                ModelState.AddModelError(
                    "Position",
                    "The position should be an integer 0 < x < 13."
                    );
            }
            if (animal.Year.GetType() != typeof(int))
            {
                ModelState.AddModelError("Year", "The year should be an integer.");
            }

            // checking against the database

            // database matches
            var db        = new ChiZodiacDb("chinesezodiac.db");
            var names     = db.Animals.Where <CZAnimal> (x => x.Name == animal.Name).ToList();
            var positions = db.Animals.Where <CZAnimal> (x => x.Position == animal.Position)
                            .ToList();
            var years = db.Animals.Where <CZAnimal> (x => x.Year == animal.Year).ToList();

            if (names.Count > 0 && (!editingExisting || (editingExisting && names [0].Id != animal.Id)))
            {
                ModelState.AddModelError(
                    "Name",
                    string.Format("This animal is already in the database.")
                    );
            }
            if (positions.Count > 0 && (!editingExisting || (editingExisting && positions [0].Id != animal.Id)))
            {
                ModelState.AddModelError(
                    "Position",
                    String.Format("This position already has an animal (the {0}).", positions [0].Name
                                  )
                    );
            }
            if (years.Count > 0 && (!editingExisting || (editingExisting && years [0].Id != animal.Id)))
            {
                ModelState.AddModelError(
                    "Year",
                    String.Format(
                        "This year already has an animal (the {0}).",
                        years [0].Name
                        )
                    );
            }

            return(ModelState.IsValid);
        }
Esempio n. 3
0
 public ActionResult Delete(int aid)
 {
     // it would be polite and sensible to have a confirmation page first
     var db = new ChiZodiacDb ("chinesezodiac.db");
     var animals = from a in db.Animals where a.Id == aid select a;
     foreach (var a in animals) {
         db.Delete (a);
     }
     return Redirect ("All");
 }
Esempio n. 4
0
        public ActionResult Delete(int aid)
        {
            // it would be polite and sensible to have a confirmation page first
            var db      = new ChiZodiacDb("chinesezodiac.db");
            var animals = from a in db.Animals where a.Id == aid select a;

            foreach (var a in animals)
            {
                db.Delete(a);
            }
            return(Redirect("All"));
        }
Esempio n. 5
0
 public ActionResult Details(string name, int position, int year)
 {
     name = name.ToLower ();
     // get db
     var db = new ChiZodiacDb ("chinesezodiac.db");
     // find animal in db
     var an = (from a in db.Animals
               where a.Name.ToLower () == name
         && a.Position == position
         && a.Year == year
               select a).Single ();
     // didn't check for failure... DANGER!
     return View (an);
 }
Esempio n. 6
0
        public ActionResult Details(string name, int position, int year)
        {
            name = name.ToLower();
            // get db
            var db = new ChiZodiacDb("chinesezodiac.db");
            // find animal in db
            var an = (from a in db.Animals
                      where a.Name.ToLower() == name &&
                      a.Position == position &&
                      a.Year == year
                      select a).Single();

            // didn't check for failure... DANGER!
            return(View(an));
        }
Esempio n. 7
0
 public ActionResult Create(CZAnimal animal)
 {
     if (ValidInput (animal, false)) {
         var db = new ChiZodiacDb ("chinesezodiac.db");
         db.Add (animal);
         // show some kind of confirmation
         return RedirectToAction (
             "Details",
             "Animals", new {
             name = animal.Name, position = animal.Position, year = animal.Year }
         );
     } else {
         return View (animal);
     }
 }
Esempio n. 8
0
 public ActionResult Create(CZAnimal animal)
 {
     if (ValidInput(animal, false))
     {
         var db = new ChiZodiacDb("chinesezodiac.db");
         db.Add(animal);
         // show some kind of confirmation
         return(RedirectToAction(
                    "Details",
                    "Animals", new {
             name = animal.Name, position = animal.Position, year = animal.Year
         }
                    ));
     }
     else
     {
         return(View(animal));
     }
 }
Esempio n. 9
0
 public ActionResult Edit(CZAnimal animal)
 {
     /// Here we validate.
     if (ValidInput(animal, true))
     {
         // call some command on the database
         var db = new ChiZodiacDb("chinesezodiac.db");
         db.Update(animal);
         // show some kind of confirmation
         return(RedirectToAction(
                    "Details",
                    "Animals", new {
             name = animal.Name, position = animal.Position, year = animal.Year
         }
                    ));
     }
     else
     {
         return(View(animal));
     }
 }
Esempio n. 10
0
        /// <summary>
        /// Show all the animals in the SQLite3 database
        /// </summary>
        public ActionResult All()
        {
            var db = new ChiZodiacDb("chinesezodiac.db");

            return(View(db));
        }
Esempio n. 11
0
 public ActionResult Edit(int id)
 {
     var db = new ChiZodiacDb ("chinesezodiac.db");
     var an = (from a in db.Animals where a.Id == id select a).Single ();
     // didn't check for failure... DANGER!
     return View (an);
 }
Esempio n. 12
0
 /// <summary>
 /// Show all the animals in the SQLite3 database
 /// </summary>
 public ActionResult All()
 {
     var db = new ChiZodiacDb ("chinesezodiac.db");
     return View (db);
 }
Esempio n. 13
0
        private bool ValidInput(CZAnimal animal, bool editingExisting)
        {
            // basic sensible input checks
            if (animal.Name.Trim ().Length == 0) {
                ModelState.AddModelError ("Name", "Please add a name!");
            }
            if (animal.Position.GetType () != typeof(int) ||
                animal.Position < 1 ||
                animal.Position > 12) {
                ModelState.AddModelError (
                    "Position",
                    "The position should be an integer 0 < x < 13."
                );
            }
            if (animal.Year.GetType () != typeof(int)) {
                ModelState.AddModelError ("Year", "The year should be an integer.");
            }

            // checking against the database

            // database matches
            var db = new ChiZodiacDb ("chinesezodiac.db");
            var names = db.Animals.Where<CZAnimal> (x => x.Name == animal.Name).ToList ();
            var positions = db.Animals.Where<CZAnimal> (x => x.Position == animal.Position)
                .ToList ();
            var years = db.Animals.Where<CZAnimal> (x => x.Year == animal.Year).ToList ();

            if (names.Count > 0 && (!editingExisting || (editingExisting && names [0].Id != animal.Id))) {
                ModelState.AddModelError (
                    "Name",
                    string.Format ("This animal is already in the database.")
                );
            }
            if (positions.Count > 0 && (!editingExisting || (editingExisting && positions [0].Id != animal.Id))) {
                ModelState.AddModelError (
                    "Position",
                    String.Format ("This position already has an animal (the {0}).", positions [0].Name
                )
                );
            }
            if (years.Count > 0 && (!editingExisting || (editingExisting && years [0].Id != animal.Id))) {
                ModelState.AddModelError (
                    "Year",
                    String.Format (
                    "This year already has an animal (the {0}).",
                    years [0].Name
                )
                );
            }

            return ModelState.IsValid;
        }
Esempio n. 14
0
 public ActionResult Edit(CZAnimal animal)
 {
     /// Here we validate.
     if (ValidInput (animal, true)) {
         // call some command on the database
         var db = new ChiZodiacDb ("chinesezodiac.db");
         db.Update (animal);
         // show some kind of confirmation
         return RedirectToAction (
             "Details",
             "Animals", new {
             name = animal.Name, position = animal.Position, year = animal.Year }
         );
     } else {
         return View (animal);
     }
 }