Exemplo n.º 1
0
        public ActionResult AddBuilding(int id, int row, int column)
        {
            //validate
            int currentUserID = Int32.Parse(Request.Cookies["currentUser"]["id"]);
            var userOwnsCity  = (from C in entities.user_cities
                                 where C.city_id == id && C.user_id == currentUserID
                                 select C).Count();

            if (userOwnsCity == 0)
            {
                //ERROR PAGE
                return(RedirectToAction("Error", "Account"));
            }

            // napunimo model za korisnika
            user currentUser = (from e in entities.users
                                where e.id == currentUserID
                                select e).First();

            UserDetailsModel userDetails = new UserDetailsModel();

            userDetails.fillUserDetailsModel(currentUser, entities);

            /* napunimo addBuilding model, koji sadrzi detalje korisnika,
             * id grada u kojem pravimo zgradu,
             * i sve zgrade koje mogu da se prave*/

            AddBuildingModel addBuildingModel = new AddBuildingModel();

            addBuildingModel.fillAddBuildingModel(userDetails, id, row, column, entities);

            return(View(addBuildingModel));
        }
Exemplo n.º 2
0
        // GET: Home/AddBuilding
        public ActionResult AddBuilding()
        {
            AddBuildingModel buildingModel = new AddBuildingModel
            {
                _Campuses   = new SelectList(db.tb_Campus.OrderBy(s => s.CampusName), "CampusID", "CampusName"),
                _tb_College = db.tb_College
            };

            buildingModel._Colleges = new List <SelectListItem>
            {
                new SelectListItem()
                {
                    Value = "0", Text = "-- Select One --"
                }
            }.Concat(db.tb_College.Select(x => new SelectListItem
            {
                Value = x.CollegeID.ToString(), Text = x.CollegeName
            }));

            return(View(buildingModel));
        }
Exemplo n.º 3
0
        public ActionResult AddBuilding(HttpPostedFileBase file, [Bind(Include = "_CollegeID,_CampusID,_BuildingName")] AddBuildingModel model)
        {
            Error error = new Error();

            error.errCode = ErrorDetail.Success;
            error.errMsg  = ErrorDetail.GetMsg(error.errCode);
            List <string> errs = new List <string>();

            model._Colleges = new List <SelectListItem>
            {
                new SelectListItem()
                {
                    Value = "0", Text = "-- Select One --"
                }
            }.Concat(db.tb_College.Select(x => new SelectListItem
            {
                Value = x.CollegeID.ToString(), Text = x.CollegeName
            }));
            model._Campuses   = new SelectList(db.tb_Campus.OrderBy(s => s.CampusName), "CampusID", "CampusName", model._CampusID);
            model._tb_College = db.tb_College;

            if (file != null && file.ContentLength > 0)
            {
                try
                {
                    var extension = file.FileName.Split('.').Last().ToUpper();
                    if (extension != "PDF" && extension != "JPG")
                    {
                        ViewBag.Message = "Selected file type is not PDF or JPEG";
                        return(View(model));
                    }

                    string path = Path.Combine(Server.MapPath(MvcApplication.BuildingsFolder), file.FileName);
                    file.SaveAs(path);
                    ViewBag.Message = "File uploaded successfully";
                }
                catch (Exception ex)
                {
                    ViewBag.Message = "ERROR:" + ex.Message.ToString();
                }
            }
            else
            {
                ViewBag.Message = "You have not specified a file.";

                return(View(model));
            }

            var buildings = db.tb_Building.Where(s => s.BuildingName.ToUpper() == model._BuildingName.ToUpper());

            //Check dublicates
            if (buildings.ToList().Count == 0)
            {
                tb_Building building = new tb_Building()
                {
                    CampusID     = model._CampusID,
                    BuildingName = model._BuildingName,
                    ImagePath    = file.FileName
                };
                db.tb_Building.Add(building);
            }
            else
            {
                tb_Building building = buildings.FirstOrDefault();
                building.CampusID     = model._CampusID;
                building.BuildingName = model._BuildingName;
                building.ImagePath    = file.FileName;
                db.tb_Building.Attach(building);
            }
            try
            {
                db.SaveChanges();
                model._tb_College = db.tb_College;
            }
            catch (Exception ex)
            {
                error.errCode = ErrorDetail.DataImportError;
                error.errMsg  = ErrorDetail.GetMsg(error.errCode);
                errs.Add("Error #" + error.errCode.ToString() + "!" + error.errMsg + ". " + ex.Message);
                ViewData["ErrorList"] = errs;
                return(View(model));
            }
            model._tb_College = db.tb_College;
            return(View(model));
        }