// GET: ArmyModels/Details public ActionResult Details(int id) { // this will show the army info, but will also show a list of models that come with the army // if this army is not owned by user and doesnt have role user is diverted. List <ArmyModelBLL> models = null; ArmyBLL army = null; try { // using is used because we use an SQL connection which is high in resources using (ContextBLL ctx = new ContextBLL()) { army = ctx.ArmyFindByID(id); if (army == null) { // If no record is returned we divert to a custom error screen. return(View("NotFound")); } models = ctx.ArmyModelsFindByArmyID(army.ArmyID, Constants.DefaultPageNumber, Constants.DefaultPageSize); } } catch (Exception oops) { // if the connection fails or something else goes wrong we log the error and divert. Error.Log(oops); return(View("Error", oops)); } FullArmyData item = new FullArmyData(army, models); // item is an instance of FullArmyData! item.models = models; if (!IsMineOrAdmin(item)) { // if user should not see this we divert him. return(View("Porpoise")); } return(View(item)); }
// A bool method that returns false if user should not be seeing/manipulating the army. public bool IsMineOrAdmin(FullArmyData Army) { // this little filter should make sure the object is either the users, or the user is in //developer / administrator role. It should make it so people can't edit others armies. // Returns a false if you shouldnt be able to mess with it. bool ReturnValue = false; if (User.IsInRole("Administrator") | User.IsInRole(Constants.DeveloperRoleName) | Army.UserName == User.Identity.Name) { ReturnValue = true; } return(ReturnValue); }
public ActionResult Edit(int id, FullArmyData Army) {// we check if the form returned is validated if (!ModelState.IsValid) { return(View(Army)); } ArmyBLL ArmyData = new ArmyBLL(Army); try { using (ContextBLL ctx = new ContextBLL()) { ctx.ArmyUpdate(ArmyData); ctx.ArmyModelsDeleteByArmyID(Army.ArmyID); for (int i = 0; i < Army.models.Count; i++) { // to make sure we have a good save of the edited army data we delete the army data, //then we put in the new army roster. I figured this is less taxing then pulling //up the old roster, compare info, then delete, edit and create records accordingly. ArmyModelBLL model = new ArmyModelBLL(); model.Quantity = Army.models[i].Quantity; model.FullSquats = Army.models[i].FullSquats; model.ModelID = Army.models[i].ModelID; model.ArmyID = Army.ArmyID; //models.Add(model); if (model.Quantity > 0) { // if this model is in the army create a record in the many to many table. ctx.ArmyModelCreate(model); } } } TempData.Add("Message", "Saved"); //we use this message for a pop-up confirmation return(RedirectToAction("Edit", new { id = Army.ArmyID })); // sending to edit because I want the user to see the new values and do possible tweaks //till they are happy. saving just updates the info and calculates points. } catch (Exception oops) { Error.Log(oops); return(View("error", oops)); } }
public FullArmyData TestArmy(int WarcasterQty, int warjackQty, int TroopQty, int FullTroopQty) { // testarmy we can populate with predictable info to run a clean test. FullArmyData ReturnValue = new FullArmyData(); List <ArmyModelBLL> models = new List <ArmyModelBLL>(); for (int i = 0; i < WarcasterQty; i++) { //add X amount of casters models.Add(Warcaster()); } for (int i = 0; i < warjackQty; i++) { //add X amount of jacks models.Add(WarJack()); } for (int i = 0; i < TroopQty; i++) { //add X amount of troops models.Add(Troop()); } for (int i = 0; i < FullTroopQty; i++) { //adds X amount of full troops models.Add(FullTroop()); } ReturnValue.models = models; return(ReturnValue); }
// GET: ArmyModels/Edit public ActionResult Edit(int id) { // this will show the army info, but will also show a list of models that come with the army List <ArmyModelBLL> models = null; ArmyBLL army = null; try { using (ContextBLL ctx = new ContextBLL()) { army = ctx.ArmyFindByID(id); if (army == null) { return(View("NotFound")); } models = ctx.ArmyModelsFindByFactionID(army.ArmyID, army.FactionID, Constants.DefaultPageNumber, Constants.DefaultPageSize); #region Pulldown Stuff //We want to make pull down menus for models so we can easily select the quantity and //amount of full squats we can select. These menus are restricted by the field allowence. List <List <SelectListItem> > FieldAllowance = new List <List <SelectListItem> >(); List <List <SelectListItem> > FullSquats = new List <List <SelectListItem> >(); ViewBag.MaxFieldAllowance = FieldAllowance; ViewBag.MaxFullSquats = FullSquats; for (int i = 0; i < models.Count; i++) { FieldAllowance.Add(new List <SelectListItem>()); FullSquats.Add(new List <SelectListItem>()); int MaxQuantity = 0; if (0 != models[i].AttachesToModelID) { // limits the attached models to qty of parent model ArmyModelBLL OriginalModel = ctx.ArmyModelsFindByArmyIDAndModelID(army.ArmyID, models[i].AttachesToModelID); MaxQuantity = OriginalModel.Quantity; } else { MaxQuantity = models[i].FieldAllowence; } for (int j = 0; j < MaxQuantity + 1; j++) { //this is the list for the quantity SelectListItem sli = new SelectListItem(); sli.Text = j.ToString(); sli.Value = j.ToString(); if (j == models[i].Quantity) { sli.Selected = true; } FieldAllowance[i].Add(sli); } for (int jj = 0; jj < models[i].Quantity + 1; jj++) { // this is the list for the Full squats. we limit this to the quantity , // since full squat is an upgrade to a squat // it makes no sense to make a selection larger then the amount of squats. SelectListItem sli = new SelectListItem(); sli.Text = jj.ToString(); sli.Value = jj.ToString(); if (jj == models[i].FullSquats) { sli.Selected = true; } FullSquats[i].Add(sli); } } #endregion Pulldown stuff } } catch (Exception oops) { //error logger and divert to error screen Error.Log(oops); return(View("error", oops)); } FullArmyData item = new FullArmyData(army, models); item.models = models; if (!IsMineOrAdmin(item)) { // check if user should be editing this return(View("Porpoise")); } return(View(item)); }