/* * AddPositionToComponentModalViewComponent * This modal handles Adding, Editing, and Deleting a Position via the RosterManager UI */ /// <summary> /// Gets the AddPositionToComponentViewComponent. /// </summary> /// <param name="componentId">The ComponentId of the Component to which a Position is being added</param> /// <returns>A <see cref="T:BlueDeck.ViewComponents.AddPositionToComponentViewComponent"/></returns> public IActionResult GetAddPositionToComponentViewComponent(int componentId, int?positionId = null) { if (positionId != null) { Position position = unitOfWork.Positions.GetPositionWithParentComponent(Convert.ToInt32(positionId)); if (User.IsInRole("GlobalAdmin")) { List <ComponentSelectListItem> componentList = unitOfWork.Components.GetComponentSelectListItems(); AddPositionToComponentViewComponentViewModel viewModel = new AddPositionToComponentViewComponentViewModel(position, componentList); return(ViewComponent("AddPositionToComponent", new { vm = viewModel })); } else if (User.IsInRole("ComponentAdmin")) { List <ComponentSelectListItem> componentList = JsonConvert.DeserializeObject <List <ComponentSelectListItem> >( User.Claims.FirstOrDefault( claim => claim.Type == "CanEditComponents") .Value .ToString()); AddPositionToComponentViewComponentViewModel viewModel = new AddPositionToComponentViewComponentViewModel(position, componentList); return(ViewComponent("AddPositionToComponent", new { vm = viewModel })); } else { return(Forbid()); } } else { Component parent = unitOfWork.Components.Get(componentId); AddPositionToComponentViewComponentViewModel viewModel = new AddPositionToComponentViewComponentViewModel(parent); return(ViewComponent("AddPositionToComponent", new { vm = viewModel })); } }
public IViewComponentResult Invoke(AddPositionToComponentViewComponentViewModel vm) { return(View(vm)); }
public IActionResult AddPositionToComponent([Bind("PositionId,ParentComponentId,LineupPosition,Callsign,PositionName,JobTitle,IsManager,IsAssistantManager,IsUnique")] AddPositionToComponentViewComponentViewModel form) { // populate the Form object's ParentComponent property with it's ParentComponent form.ParentComponent = unitOfWork.Components.GetComponentWithChildren(Convert.ToInt32(form.ParentComponentId)); // pull a list of all Positions in the Repo to use to check name conflict IEnumerable <Position> allPositions = unitOfWork.Positions.GetAll(); if (form.Callsign != null) { form.Callsign = form.Callsign.ToUpper(); } else { form.Callsign = "NONE"; } // check Model validation first if (ModelState.IsValid) { // if ModelValidation passes, create a counter to store count of errors for other validation rules var errors = 0; // check to make sure submitted new position name doesn't conflict with an existing position foreach (Position p in allPositions) { // check for conflict in Position Name if (p.Name == form.PositionName && p.ParentComponentId == form.ParentComponentId && p.PositionId != form.PositionId) { errors++; ViewBag.Message = $"A Position with the name {form.PositionName} already exists. Use a different Name.\n"; } else if (form.Callsign != "NONE") { if (p.Callsign == form.Callsign && p.ParentComponentId == form.ParentComponentId && p.PositionId != form.PositionId) { errors++; ViewBag.Message = $"The callsign '{form.Callsign}' is in use by {p.Name}. Choose another."; } } } // check for conflict in "IsManager" for all Positions in the ParentComponent foreach (Position p in form.ParentComponent.Positions) { if (form.IsManager == true && p.PositionId != form.PositionId) { if (p.IsManager == true) { errors++; ViewBag.Message += $"{p.ParentComponent.Name} already has a Position designated as Manager. Only one Manager Position is permitted.\n"; } } else if (form.IsAssistantManager == true && p.PositionId != form.PositionId) { if (p.IsAssistantManager == true) { errors++; ViewBag.Message += $"{p.ParentComponent.Name} already has a Position designated as Assistant Manager. Only one Assistant Manager Position is permitted.\n"; } } } // no validation errors, safe to add position if (errors == 0) { // add the position via unitOfWork if (form.PositionId == null) // null PositionId means this is a new Position { Position p = new Position() { ParentComponent = form.ParentComponent, Name = form.PositionName, JobTitle = form.JobTitle, IsManager = form.IsManager, IsAssistantManager = form.IsAssistantManager, IsUnique = form.IsUnique, LineupPosition = form.LineupPosition, Callsign = form.Callsign, CreatedDate = DateTime.Now, CreatorId = Convert.ToInt32(User.Claims.FirstOrDefault(claim => claim.Type == "MemberId").Value), LastModified = DateTime.Now, LastModifiedById = Convert.ToInt32(User.Claims.FirstOrDefault(claim => claim.Type == "MemberId").Value) }; unitOfWork.Positions.UpdatePositionAndSetLineup(p); unitOfWork.Complete(); } else // editing an existing position { Position p = new Position() { PositionId = Convert.ToInt32(form.PositionId), ParentComponent = form.ParentComponent, Name = form.PositionName, JobTitle = form.JobTitle, IsManager = form.IsManager, IsAssistantManager = form.IsAssistantManager, IsUnique = form.IsUnique, LineupPosition = form.LineupPosition, Callsign = form.Callsign, LastModified = DateTime.Now, LastModifiedById = Convert.ToInt32(User.Claims.FirstOrDefault(claim => claim.Type == "MemberId").Value), Members = null // this needs to be null, or the repo method will reassign the current members }; unitOfWork.Positions.UpdatePositionAndSetLineup(p); unitOfWork.Complete(); } // return a JSON object to the Ajax POST so that it can hide the Modal return(Json(new { success = true })); } // validation errors, return the ViewModel with the ViewBag.Message showing else { form.ComponentList = unitOfWork.Components.GetComponentSelectListItems(); return(ViewComponent("AddPositionToComponent", form)); } } else // ModelState invalid { form.ComponentList = unitOfWork.Components.GetComponentSelectListItems(); return(ViewComponent("AddPositionToComponent", form)); } }