public ActionResult Create(Community community)
        {
            if (ModelState.IsValid)
            {
                community.Id = Guid.NewGuid();
                db.Communities.AddObject(community);
                db.SaveChanges();

                var templateSceneIdString = this.Request.Form["templateSceneId"];
                if (!string.IsNullOrEmpty(templateSceneIdString))
                {
                    // NOTE: make sure community ID is already created when move to "code first"
                    using (var cms = new CmsEntities())
                    {
                        var scene = cms.Scenes.Attach(community.GetScene());
                        var templateSceneId = long.Parse(templateSceneIdString);
                        var templateScene = cms.Scenes.First(s => s.Id == templateSceneId);
                        scene.ApplyTemplate(cms, templateScene);
                        cms.SaveChanges();
                    }
                }
                return RedirectToAction("Index");
            }

            ViewBag.ZoneId = new SelectList(db.Zones, "Id", "Name", community.ZoneId);
            return View(community);
        }
 public void Db()
 {
     var db = new CmsEntities(@"data source=localhost\sqlserver;Initial Catalog=bnh;Integrated Security=SSPI;");
     var walls = db.Walls.ToList();
     Assert.IsTrue(walls.Count == 2);
     Assert.IsTrue(walls[0].Bricks.Count == 1);
     Assert.IsTrue(walls[1].Bricks.Count == 1);
 }
 public ActionResult Create()
 {
     ViewBag.ZoneId = new SelectList(db.Zones, "Id", "Name");
     using(var cm = new CmsEntities())
     {
         var sceneTemplates = from s in cm.Scenes
                              from t in cm.SceneTemplates
                              where s.OwnerGuidId == t.Id
                              select new { id = s.Id, title = t.Title };
         ViewBag.Templates = new SelectList(sceneTemplates.ToList(), "id", "title");
     }
     return View();
 }
 public static IEnumerable<Wall> GetWallsFromEntityId(Guid id)
 {
     using (var db = new CmsEntities())
     {
         // Convert to list to make sure context is open during request
         var walls = db.Scenes
             .Where(s => s.OwnerGuidId == id)
             .SelectMany(s => s.Walls)
             .OrderBy(w => w.Order).ToList();
         // ensure bricks while given db context is open
         walls.ForEach(w => w.Bricks.ToList());
         return walls;
     }
 }
 /// <summary>
 /// Get community's scene. If it doesn't exists it creates one.
 /// </summary>
 /// <param name="community">Community to get scene for</param>
 /// <returns></returns>
 public static Scene GetScene(this Community community)
 {
     using (var db = new CmsEntities())
     {
         var scene = db.Scenes.FirstOrDefault(s => s.OwnerGuidId == community.Id);
         if (scene == null)
         {
             scene = db.Scenes.Add(new Scene { OwnerGuidId = community.Id });
             db.SaveChanges();
         }
         // trick to ensure that walls, bricks and shared bricks are loaded in current DB context for given scene
         var bricks = scene.Walls.SelectMany(w => w.Bricks).ToList().OfType<LinkableBrick>().Select(b => b.LinkedBrick).ToList();
         return scene;
     }
 }
Exemple #6
0
        public static Scene ApplyTemplate(this Scene scene, CmsEntities db, Scene templateScene)
        {
            // delete all walls on obsolete scene
            foreach (var wall in scene.Walls.ToList())
            {
                db.Walls.Remove(wall);
            }

            // clone walls from template scene into our scene
            foreach (var wall in templateScene.Walls)
            {
                scene.Walls.Add(wall.Clone());
            }

            return scene;
        }