public void Create_POST_CreatesNewInstance()
        {
            var user = new User { UserId = 12 };
            InitDb(db => db.Users.Add(user));

            var controller = new InstanceController();
            controller.UserInfo = new UserInfo { Id = 12 };

            var model = new CreateViewModel
            {
                Name = "Name",
                Started = DateTime.Parse("2013-01-24")
            };

            var redirect = controller.Create(model) as RedirectToRouteResult;

            Assert.IsNotNull(redirect);

            var instances = DB.Use(db => db.Instances.Include("Users").ToList());

            Assert.AreEqual(1, instances.Count);
            var instance = instances.First();

            Assert.AreEqual("Name", instance.Name);
            Assert.AreEqual(DateTime.Parse("2013-01-24"), instance.Started);
            Assert.IsNull(instance.Closed);

            Assert.AreEqual(1, instance.Users.Count(u => u.UserId == 12));
        }
Пример #2
0
        public ActionResult Create(CreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                var userId = UserInfo.Id;

                var instance = new Instance
                {
                    Name = model.Name.Trim(),
                    Started = model.Started,
                    Closed = model.Closed,
                    Users = new List<User>()
                };

                DB.Save(db => {
                    var user = db.Users.Find(userId);
                    instance.Users.Add(user);
                    db.Instances.Add(instance);
                });

                return RedirectToAction("Details", new { id = instance.Id });

            }
            return View(model);
        }