Inheritance: DbContext
        public static UserProfile GetCurrentUser(this Controller controller)
        {
            if(controller.User != null && controller.User.Identity.IsAuthenticated) {

                UserProfile user = controller.Session["current_user"] as UserProfile;

                if(user == null) {
                    using(var dtx = new HeimContext()) {

                        user = dtx.UserProfiles.SingleOrDefault(u => u.Username == controller.User.Identity.Name);
                        if(user != null) {
                            controller.Session["current_user"] = user;
                            return user;
                        }

                        controller.Response.Cookies.Clear();
                        HttpCookie c = new HttpCookie("login");
                        c.Expires = DateTime.Now.AddDays(-1);
                        controller.Response.Cookies.Add(c);

                        WebMatrix.WebData.WebSecurity.Logout();
                        FormsAuthentication.SignOut();
                        controller.Session.Clear();
                        controller.Session.Abandon();

                        controller.Response.Redirect("Home/Index", true);
                    }
                }
                return user;
            }

            return null;
        }
        public ActionResult Edit(int id)
        {
            using(var dtx = new HeimContext()) {

                var query = from v in dtx.FloorVariants
                            where v.ID == id
                            select new FloorVariantViewModel {
                                ID = v.ID,
                                FloorID = v.FloorID,
                                PlanID = v.Floor.PlanID,
                                FloorNumber = v.Floor.FloorNumber,
                                Created = v.Created,
                                Updated = v.Updated,
                                Name = v.Name,
                                PlanPreviewImageFilePath = v.PlanPreviewImageFilePath,
                                ModelFilePath = v.ModelFilePath
                            };

                var variant = query.Single();

                if(!String.IsNullOrEmpty(variant.ModelFilePath)){
                    string modelFilePath = Server.MapPath(variant.ModelFilePath);
                    if(System.IO.File.Exists(modelFilePath)) {
                        variant.ModelFileSize = Math.Floor(new FileInfo(modelFilePath).Length / 1024.0);
                    }
                }
                return View(variant);
            }
        }
 public ActionResult Delete(int id)
 {
     using(var dtx = new HeimContext()) {
         ShiftRight.Heim.Models.Attribute attr = dtx.Attributes.Single(x => x.ID == id);
         return View(attr);
     }
 }
        public ActionResult Customize(int planId)
        {
            using(var dtx = new HeimContext()) {

                var plan = dtx.Plans.Include("Floors").Include("Attributes").Include("Floors.Variants").Single(p => p.ID == planId);

                if(plan == null) {
                    return RedirectToAction("New");
                }

                ViewBag.Title = plan.Name;

                var vm = new CustomizeProjectViewModel {
                    Name = plan.Name,
                    PreviewImage = plan.PreviewImageFilePath,

                    Plan = new PlanViewModel {
                        ID = plan.ID,
                        Name = plan.Name,
                        PreviewImage = plan.PreviewImageFilePath,
                        Floors = plan.Floors.Select(fl =>
                            new FloorViewModel {
                                ID = fl.ID,
                                FloorNumber = fl.FloorNumber,
                                Variants = fl.Variants.Select(vr =>
                                    new FloorVariantViewModel {
                                        ID = vr.ID,
                                        Name = vr.Name,
                                        IsDefault = false,
                                        PlanPreviewImageFilePath = vr.PlanPreviewImageFilePath
                                    }).ToList()
                            }).ToList(),
                    },

                    Attributes = plan.Attributes.ToList()
                };

                foreach(var item in vm.Plan.Floors) {
                    if(item.Variants.Count() > 0) {

                        foreach(var vr in item.Variants) {
                            vr.IsDefault = true;
                            item.PlanPreviewImage = vr.PlanPreviewImageFilePath;

                            break;
                        }
                    }
                }

                //vm.Attributes.Add(new ShiftRight.Heim.Models.Attribute {
                //	ID = 0,
                //	Name = "พื้นที่ใช้สอย",
                //	Unit =
                //});

                return View(vm);

            }// end using
        }
        //public ActionResult Index() {
        //    return View();
        //}
        public ActionResult Edit(int id)
        {
            using(var dtx = new HeimContext()) {
                var attr = dtx.Attributes.Find(id);

                return View(attr);
            }
        }
Example #6
0
        public ActionResult Delete(int id)
        {
            using(var dtx = new HeimContext()) {

                var asset = dtx.Assets.Find(id);

                return View(asset);
            }
        }
Example #7
0
        public ActionResult DeleteConfirmed(int id)
        {
            using(var dtx = new HeimContext()) {

                Asset asset = dtx.Assets.Single(x => x.ID == id);
                dtx.Assets.Remove(asset);
                dtx.SaveChanges();
                return RedirectToAction("Index");
            }
        }
        public ActionResult DeleteConfirmed(int id)
        {
            using(var dtx = new HeimContext()) {

                ShiftRight.Heim.Models.Attribute attr = dtx.Attributes.Single(x => x.ID == id);
                dtx.Attributes.Remove(attr);
                dtx.SaveChanges();
                return RedirectToAction("Edit", "Plans", new { id = attr.PlanID});
            }
        }
        public ActionResult Edit(ShiftRight.Heim.Models.Attribute attr)
        {
            using(var dtx = new HeimContext()) {

                if(this.ModelState.IsValid) {
                    dtx.Entry<ShiftRight.Heim.Models.Attribute>(attr).State = System.Data.Entity.EntityState.Modified;
                    dtx.SaveChanges();
                }

                return RedirectToAction("Edit", "Plans", new { id = attr.PlanID });
            }
        }
Example #10
0
        public ActionResult Delete(int id)
        {
            using(var dtx = new HeimContext()) {

                var variant = dtx.FloorVariants.Single(v => v.ID == id);

                dtx.FloorVariants.Remove(variant);
                dtx.SaveChanges();

                return RedirectToAction("Edit", "Floors", new { id = variant.FloorID });
            }
        }
Example #11
0
        public ActionResult AddAttribute(ShiftRight.Heim.Models.Attribute attr)
        {
            using(var dtx = new HeimContext()) {

                if(ModelState.IsValid) {
                    dtx.Attributes.Add(attr);
                    dtx.SaveChanges();
                }

                return RedirectToAction("Edit", new { id = attr.PlanID });
            }
        }
            public SimpleMembershipInitializer()
            {
                Database.SetInitializer<HeimContext>(null);

                try {
                    using(var context = new HeimContext()) {
                        if(!context.Database.Exists()) {
                            // Create the SimpleMembership database without Entity Framework migration schema
                            ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
                        }
                    }

                    WebSecurity.InitializeDatabaseConnection("DefaultConnection", "Users", "ID", "UserName", autoCreateTables: true);
                } catch(Exception ex) {
                    throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
                }
            }
Example #13
0
        public ActionResult CreateBlank(int planId)
        {
            using(var dtx = new HeimContext()) {

                int? floorNumber = dtx.Floors.Where(f => f.PlanID == planId).Max(f => (int?)f.FloorNumber);

                var floor = new FloorTemplate {
                    PlanID = planId,
                    FloorNumber = floorNumber.HasValue? + floorNumber.Value + 1: 1
                };

                dtx.Floors.Add(floor);
                dtx.SaveChanges();

                return RedirectToAction("Edit", new { id = floor.ID });
            }
        }
Example #14
0
        public ActionResult Edit(int id)
        {
            using(var dtx = new HeimContext()) {

                var asset = dtx.Assets.Find(id);

                return View(new AssetViewModel {
                    ID = asset.ID,
                    Name = asset.Name,
                    Mapping = asset.Mapping,
                    Created = asset.Created,
                    Updated = asset.Updated,
                    PreviewFilePath = asset.PreviewFilePath,
                    AssetType = asset.AssetType,
                    AssetFilePath = asset.AssetFilePath,
                });
            }
        }
Example #15
0
        //
        // GET: /Floors/Delete/5
        public ActionResult Delete(int id)
        {
            using(var dtx = new HeimContext()) {

                var query = from fl in dtx.Floors
                            where fl.ID == id
                            select new FloorViewModel {
                                ID = fl.ID,
                                FloorNumber = fl.FloorNumber,
                                Plan = new PlanViewModel {
                                    ID = fl.PlanID,
                                    Name = fl.Plan.Name
                                }
                            };

                return View(query.Single());
            }
        }
Example #16
0
        public ActionResult CreateBlank(int floorId)
        {
            using(var dtx = new HeimContext()) {

                var floor = dtx.Floors.Find(floorId);
                int variantCount = dtx.FloorVariants.Where(fv => fv.FloorID == floorId).Count();

                var variant = new FloorVariant {
                    Created = DateTimeOffset.UtcNow,
                    Updated = DateTimeOffset.UtcNow,
                    Name = "Variant " + (variantCount + 1),
                    FloorID = floorId,
                };

                floor.Variants.Add(variant);
                dtx.SaveChanges();

                return RedirectToAction("Edit", new { id = variant.ID, create = true });
            }
        }
Example #17
0
        public ActionResult Create([Bind(Exclude = "ID")] AssetViewModel mat)
        {
            using(var dtx = new HeimContext()) {

                if(ModelState.IsValid) {

                    mat.Created = DateTimeOffset.UtcNow;
                    mat.Updated = mat.Created;

                    dtx.Assets.Add(mat as Asset);
                    dtx.SaveChanges();

                    SaveAssetFiles(dtx, mat);

                    dtx.Entry<Asset>(mat as Asset).State = EntityState.Modified;
                    dtx.SaveChanges();
                }

                return View(mat);
            }
        }
Example #18
0
        public ActionResult Json(int? projectId = null)
        {
            Project cp = null;

            if(projectId != null) {
                using(var dtx = new HeimContext()) {
                    cp = dtx.Projects.Find(projectId);
                }
            } else {
                cp = CurrentProject;
            }

            if(cp == null) {

                Response.StatusCode = (int)HttpStatusCode.NotFound;
                return Json(new {
                    Message = "Project not found"
                }, JsonRequestBehavior.AllowGet);
            }

            var result = new {
                cp.ID,
                cp.Name,
                cp.Created,
                cp.Updated,
                cp.Data
            };

            return Json(result, JsonRequestBehavior.AllowGet);
        }
Example #19
0
        public ActionResult SaveCustomize(ProjectViewModel model)
        {
            using(var dtx = new HeimContext()) {

                var user = dtx.UserProfiles.Single(u => u.Username == User.Identity.Name);
                var selectedVariants = model.Floors.Select(f => f.ID).ToArray();
                var variants = dtx.FloorVariants
                    .Where(fv => selectedVariants.Contains(fv.ID))
                    .Select(fv => new {
                        fv.Floor.FloorNumber,
                        ModelName = "fl_" + SqlFunctions.StringConvert((decimal?)fv.Floor.FloorNumber),
                        fv.ModelFilePath,
                        fv.Name,
                        fv.ID
                    }).ToList();

                var project = new Project();

                model.ModelFile = dtx.Plans.Where(p => p.ID == model.Plan.ID).Select(p => p.ModelFilePath).FirstOrDefault();
                model.ModelFile = String.IsNullOrEmpty(model.ModelFile) ? null : VirtualPathUtility.ToAbsolute(model.ModelFile);

                project.ID = 0;
                project.OwnerID = user.ID;
                project.Created = DateTimeOffset.UtcNow;
                project.Updated = DateTimeOffset.UtcNow;
                project.IsDeleted = false;
                project.PlanTemplateID = model.Plan.ID;
                project.Name = model.Name.Trim();
                project.Data = System.Web.Helpers.Json.Encode(new {
                    ModelFilePath = model.ModelFile,
                    Floors = variants.Select(fv => new {
                        fv.FloorNumber,
                        ModelFilePath = String.IsNullOrEmpty(fv.ModelFilePath)? null: VirtualPathUtility.ToAbsolute(fv.ModelFilePath),
                        fv.Name,
                        fv.ID
                    }).ToArray()
                });

                //project.Floors
                //project.Data = ;

                dtx.Projects.Add(project);
                dtx.SaveChanges();

                return Json(project);
            }
        }
Example #20
0
        public ActionResult New(string search)
        {
            ViewBag.Title = "Select house plan";

            using(var dtx = new HeimContext()) {

                if(!String.IsNullOrWhiteSpace(search)) {
                    search = search.Trim().ToLower();
                } else {
                    search = null;
                }

                var query = from item in dtx.Plans
                            where (search == null || item.Name.ToLower().Contains(search)) && item.Floors.Count() > 0
                            orderby item.Name
                            select new PlanViewModel {
                                ID = item.ID,
                                Name = item.Name,
                                PreviewImage = item.PreviewImageFilePath,
                                Area = item.Area
                            };

                NewProjectViewModel vm = new NewProjectViewModel {
                    Search = search,
                    Plans = query.ToList()
                };

                return View(vm);

            }
        }
Example #21
0
        public ActionResult Index()
        {
            using(var dtx = new HeimContext()) {

                var query = from p in dtx.Projects
                            orderby p.Created descending
                            select new ProjectViewModel {
                                ID = p.ID,
                                Created = p.Created,
                                Name = p.Name,
                                Owner = p.Owner
                            };

                return View(new ProjectIndexViewModel {
                    Projects = query.ToList()
                });
            }
        }
Example #22
0
        public ActionResult Home()
        {
            using(var dtx  = new HeimContext()) {

                var user = this.GetCurrentUser();

                var query = from project in dtx.Projects
                            where project.OwnerID == user.ID
                            orderby project.Updated descending
                            select new ProjectViewModel {
                                ID = project.ID,
                                Created = project.Created,
                                Name = project.Name,
                                //PreviewImage = project.
                                Plan = new PlanViewModel {
                                    Area = project.PlanTemplate.Area,
                                    Name = project.PlanTemplate.Name,
                                    PreviewImage = project.PlanTemplate.PreviewImageFilePath
                                }
                            };

                var list = query.ToList();
                list.ForEach(x => x.PreviewImage = x.Plan.PreviewImage);

                return View(
                    new ProjectHomeViewModel {
                        Projects = list
                });
            }
        }
Example #23
0
 public ActionResult Delete(int id)
 {
     using(var dtx = new HeimContext()) {
         Project project = dtx.Projects.Single(x => x.ID == id);
         return View(project);
     }
 }
Example #24
0
        public ActionResult Save(Project project)
        {
            using(var dtx = new HeimContext()) {

                var p = dtx.Projects.Find(project.ID);
                if(p != null) {
                    p.Name = project.Name;
                    p.Data = project.Data;
                    p.Updated = DateTimeOffset.UtcNow;

                    dtx.SaveChanges();

                    return Json(p);
                } else {
                    throw new Exception("Project not found");
                }
            }
        }
Example #25
0
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if(CurrentProject == null && Request != null) {
                var cookie = Request.Cookies["current_project_id"];
                if(cookie != null) {

                    int id = int.Parse(cookie.Value);

                    using(var dtx = new HeimContext()) {

                        var project = dtx.Projects.Include("PlanTemplate.Floors").SingleOrDefault(p => p.ID == id);

                        if(project != null) {
                            CurrentProject = project;
                            Response.SetCookie(new HttpCookie("current_project_id", id.ToString()));
                        }
                    }
                }
            }

            base.OnActionExecuting(filterContext);
        }
Example #26
0
        public ActionResult Open(int id)
        {
            using(var dtx = new HeimContext()) {

                var project = dtx.Projects.Include("PlanTemplate.Floors").SingleOrDefault(p => p.ID == id);

                if(project != null) {
                    CurrentProject = project;

                    Response.SetCookie(new HttpCookie("current_project_id", id.ToString()));

                    var cookie = Request.Cookies["current_project_id"];

                    return RedirectToAction("Exterior");
                } else {
                    return RedirectToAction("Home", "Projects", null);
                }
            }
        }
Example #27
0
        //
        // GET: /Floors/Edit/5
        public ActionResult Edit(int id)
        {
            using(var dtx = new HeimContext()) {

                var query = from floor in dtx.Floors
                            where floor.ID == id
                            select new FloorViewModel {
                                ID = floor.ID,
                                FloorNumber = floor.FloorNumber,
                                Plan = new PlanViewModel{
                                    ID = floor.PlanID,
                                    Name = floor.Plan.Name
                                },
                                Variants = floor.Variants.Select(v => new FloorVariantViewModel {
                                    ID = v.ID,
                                    FloorNumber = v.Floor.FloorNumber,
                                    Name = v.Name,
                                    Created = v.Created,
                                    Updated = v.Updated
                                })
                            };

                var f = query.Single();
                return View(f);
            }
        }
Example #28
0
        public ActionResult Exterior()
        {
            ViewBag.BodyCssClass = "design-exterior";

            if(CurrentProject == null) {
                return RedirectToAction("Home", "Projects");
            }

            using(var dtx = new HeimContext()) {

                Project project = CurrentProject;

                var query = from asset in dtx.Assets
                            orderby asset.Name
                            select asset;

                var groups = from asset in query.ToList()
                             group new AssetViewModel{
                                ID = asset.ID,
                                Name = asset.Name,
                                Mapping = asset.Mapping,
                                Created = asset.Created,
                                Updated = asset.Updated,
                                PreviewFilePath = asset.PreviewFilePath,
                                AssetType = asset.AssetType,
                                AssetFilePath = asset.AssetFilePath,
                            } by asset.AssetType into assetGroup
                            select assetGroup;
                groups = groups.ToList();

                foreach(var group in groups) {
                    foreach(var item in group) {
                        item.Selected = true;
                        break;
                    }
                }

                var vm = new ExteriorViewModel {
                    Project = CurrentProject,
                    ProjectName = project.Name,
                    ProjectID = project.ID,
                    Assets = groups,
                    Floors = project.PlanTemplate.Floors
                };

                return View(vm);
            }
        }
Example #29
0
        public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl)
        {
            string provider = null;
            string providerUserId = null;

            if(User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId)) {
                return RedirectToAction("Manage");
            }

            if(ModelState.IsValid) {
                // Insert a new user into the database
                using(HeimContext db = new HeimContext()) {
                    UserProfile user = db.UserProfiles.FirstOrDefault(u => u.Username.ToLower() == model.UserName.ToLower());
                    // Check if user already exists
                    if(user == null) {
                        // Insert name into the profile table
                        db.UserProfiles.Add(new UserProfile { Username = model.UserName });
                        db.SaveChanges();

                        OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName);
                        OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);

                        return RedirectToLocal(returnUrl);
                    } else {
                        ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name.");
                    }
                }
            }

            ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName;
            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
Example #30
0
        public ActionResult Edit(PlanViewModel plan)
        {
            using(var dtx = new HeimContext()) {
                var planModel = dtx.Plans.Include("Floors").Single( s => s.ID == plan.ID );

                //plan.Attributes = planModel.Attributes.ToList();

                if(ModelState.IsValid) {
                    planModel.Updated = DateTimeOffset.UtcNow;
                    planModel.Name = plan.Name.Trim();
                    planModel.Area = plan.Area;
                    planModel.Price = plan.Price;

                    IStorage storage = null;
                    string root = null;

                    // prepare storage
                    if(plan.PreviewImageFile != null && plan.PreviewImageFile.ContentLength > 0 || plan.ModelFile != null && plan.ModelFile.ContentLength > 0) {

                        string fileName = planModel.Updated.UtcTicks.ToString();
                        root = ConfigurationManager.AppSettings["UserDataRoot"];
                        root = Path.Combine(root, "plans", plan.ID.ToString());

                        storage = this.GetStorage(Server.MapPath(root));
                        storage.EnsureRootExist();

                        if(plan.PreviewImageFile != null && plan.PreviewImageFile.ContentLength > 0) {
                            string ext = plan.PreviewImageFile.InputStream.GetFileExtension();
                            ext = ext == null ? null : ext.ToLower();

                            storage.Save(plan.PreviewImageFile.InputStream, fileName + ext);

                            planModel.PreviewImageFilePath = Path.Combine(root, fileName + ext);
                        }

                        if(plan.ModelFile != null && plan.ModelFile.ContentLength > 0) {
                            string ext = plan.ModelFile.InputStream.GetFileExtension();
                            ext = ext == null ? null : ext.ToLower();

                            storage.Save(plan.ModelFile.InputStream, fileName + ext);

                            planModel.ModelFilePath = Path.Combine(root, fileName + ext);
                        }
                    }

                    context.Entry<Plan>(planModel).State = EntityState.Modified;
                    context.SaveChanges();

                    if(plan.PreviewImageFile == null) {
                        return RedirectToAction("Index");
                    }
                }

                return RedirectToAction("Edit", new { id = plan.ID });
            }
        }