public IActionResult Feed()
        {
            int?          index       = HttpContext.Session.GetInt32("userid");
            MonsterObject userMonster = dbContext.Monsters
                                        .Where(m => m.UserId == index)
                                        .FirstOrDefault();

            userMonster.Feed();
            dbContext.SaveChanges();
            return(Redirect("/monster"));
        }
示例#2
0
        public ActionResult Create([Bind(Include = "Monster_ID,Monster_Name,Monster_HP,Monster_Race,Monster_Property")] Monster monster)
        {
            if (ModelState.IsValid)
            {
                db.Monsters.Add(monster);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(monster));
        }
示例#3
0
        public void QueryMonstersFromSqlTest()
        {
            //Add some monsters before querying
            _context.Monsters.Add(new Monster {
                Name = "Dave", Colour = "Orange", IsScary = false
            });
            _context.Monsters.Add(new Monster {
                Name = "Simon", Colour = "Blue", IsScary = false
            });
            _context.Monsters.Add(new Monster {
                Name = "James", Colour = "Green", IsScary = false
            });
            _context.Monsters.Add(new Monster {
                Name = "Imposter Monster", Colour = "Red", IsScary = true
            });
            _context.SaveChanges();

            //Execute the query
            ScaryMonstersQuery query = new ScaryMonstersQuery(_context);
            var scaryMonsters        = query.Execute();

            //Verify the results
            Assert.Equal(1, scaryMonsters.Count());
            var scaryMonster = scaryMonsters.First();

            Assert.Equal("Imposter Monster", scaryMonster.Name);
            Assert.Equal("Red", scaryMonster.Colour);
            Assert.True(scaryMonster.IsScary);
        }
        public IActionResult DoesRegister(UserObject user)
        {
            System.Console.WriteLine("****************Does Register");
            // Check initial ModelState
            if (ModelState.IsValid)
            {
                // If a User exists with provided email
                if (dbContext.Users.Any(u => u.Email == user.Email))
                {
                    System.Console.WriteLine("****************Email in use");
                    // Manually add a ModelState error to the Email field, with provided error message
                    ModelState.AddModelError("Email", "Email already in use!");
                    // You may consider returning to the View at this point
                    return(View("Index", user));
                }
                else
                {
                    System.Console.WriteLine("****************Creating User");
                    // Initializing a PasswordHasher object, providing our User class as its
                    PasswordHasher <UserObject> Hasher = new PasswordHasher <UserObject>();
                    user.Password = Hasher.HashPassword(user, user.Password);

                    //Save your user object to the database
                    dbContext.Add(user);
                    dbContext.SaveChanges();
                    HttpContext.Session.SetInt32("userid", user.UserId);

                    //create monster
                    MonsterObject myMonster = new MonsterObject();
                    myMonster.UserId = user.UserId;
                    dbContext.Add(myMonster);
                    dbContext.SaveChanges();

                    return(Redirect("/monster")); //This doesn't exist yet
                }
            }
            // other code
            else
            {
                System.Console.WriteLine("****************User Not Created");
                return(View("Index", user));
            }
        }
        public ActionResult Import(HttpPostedFileBase file)
        {
            // Currently only works with .xls files (xlxs if Excel is properly installed?)
            if (file != null && file.ContentLength > 0 &&
                (file.FileName.EndsWith(".xls") || file.FileName.EndsWith(".xlsx")))
            {
                // Getting file path of uploaded file with System.IO
                string path = Path.Combine(Server.MapPath("~/App_Data/"),
                                           Path.GetFileName(file.FileName));
                try
                {
                    /* Old file pathing
                     * string fileName = Path.GetFileName(file.FileName);
                     * string targetPath = Server.MapPath("~/App_Data/");
                     * file.SaveAs(targetPath + fileName);
                     * string path = targetPath + fileName;
                     */

                    file.SaveAs(path);
                    ViewBag.Message = "File uploaded successfully";

                    // LinqToExcel package to read Excel files
                    var excel = new ExcelQueryFactory();
                    excel.FileName = path;
                    var monsters = from x in excel.Worksheet <Monster>()
                                   select x;

                    // Iterating through read excel file and adding each row as a new monster
                    // into the monster database
                    foreach (var x in monsters)
                    {
                        Monster newMonster = new Monster();

                        newMonster.Monster_ID       = x.Monster_ID;
                        newMonster.Monster_Name     = x.Monster_Name;
                        newMonster.Monster_HP       = x.Monster_HP;
                        newMonster.Monster_Race     = x.Monster_Race;
                        newMonster.Monster_Property = x.Monster_Property;
                        db.Monsters.Add(newMonster);
                        db.SaveChanges();
                    }

                    // Delete the file from server after it has added entries to db
                    if (System.IO.File.Exists(path))
                    {
                        System.IO.File.Delete(path);
                    }
                }


                // General Exception
                catch (Exception ex)
                {
                    // If some other error, displays error message and deletes file from server
                    ViewBag.Message = "Error: " + ex.Message + " (maybe duplicate monster ID values?).";
                    if (System.IO.File.Exists(path))
                    {
                        System.IO.File.Delete(path);
                    }
                    //return RedirectToAction("Index");
                }
            }
            else if (file == null)
            {
                ViewBag.Message = "You have not specified a file";
            }
            else
            {
                ViewBag.Message = "Please input an Excel file";
            }

            //return RedirectToAction("Index");
            return(View());
        }
示例#6
0
 public void SaveChanges()
 {
     _context.SaveChanges();
 }