public IActionResult NewUnit() { var model = new NewUnitView(); model.Unit = new Unit(); model.Types = _unitsService.GetUnitTypesForDepartment(DepartmentId); var states = new List <CustomState>(); states.Add(new CustomState { Name = "Standard Actions" }); states.AddRange(_customStateService.GetAllActiveUnitStatesForDepartment(DepartmentId)); model.States = states; var groups = new List <DepartmentGroup>(); groups.Add(new DepartmentGroup { Name = "No Station" }); groups.AddRange(_departmentGroupsService.GetAllStationGroupsForDepartment(DepartmentId)); model.Stations = groups; return(View(model)); }
public IActionResult EditUnit(int unitId) { var model = new NewUnitView(); model.Unit = _unitsService.GetUnitById(unitId); if (!_authorizationService.CanUserModifyUnit(UserId, unitId)) { Unauthorized(); } model.Types = _unitsService.GetUnitTypesForDepartment(DepartmentId); var groups = new List <DepartmentGroup>(); groups.Add(new DepartmentGroup { Name = "No Station" }); groups.AddRange(_departmentGroupsService.GetAllStationGroupsForDepartment(DepartmentId)); model.Stations = groups; model.UnitRoles = _unitsService.GetRolesForUnit(unitId); return(View(model)); }
public async Task <IActionResult> NewUnit(NewUnitView model, IFormCollection form, CancellationToken cancellationToken) { model.Types = await _unitsService.GetUnitTypesForDepartmentAsync(DepartmentId); model.Stations = await _departmentGroupsService.GetAllStationGroupsForDepartmentAsync(DepartmentId); if ((await _unitsService.GetUnitByNameDepartmentIdAsync(DepartmentId, model.Unit.Name)) != null) { ModelState.AddModelError("Name", "Unit with that name already exists."); } var unitRoleNames = (from object key in form.Keys where key.ToString().StartsWith("unitRole_") select form[key.ToString()]).ToList(); if (ModelState.IsValid) { model.Unit.DepartmentId = DepartmentId; if (model.Unit.StationGroupId.HasValue && model.Unit.StationGroupId.Value == 0) { model.Unit.StationGroupId = null; } model.Unit = await _unitsService.SaveUnitAsync(model.Unit, cancellationToken); var roles = new List <UnitRole>(); if (unitRoleNames.Count > 0) { foreach (var roleName in unitRoleNames) { var role = new UnitRole(); role.Name = roleName; role.UnitId = model.Unit.UnitId; roles.Add(role); } } if (roles.Count > 0) { await _unitsService.SetRolesForUnitAsync(model.Unit.UnitId, roles, cancellationToken); } var auditEvent = new AuditEvent(); auditEvent.DepartmentId = DepartmentId; auditEvent.UserId = UserId; auditEvent.Type = AuditLogTypes.UnitAdded; auditEvent.After = model.Unit.CloneJson(); _eventAggregator.SendMessage <AuditEvent>(auditEvent); _eventAggregator.SendMessage <UnitAddedEvent>(new UnitAddedEvent() { DepartmentId = DepartmentId, Unit = model.Unit }); return(RedirectToAction("Index")); } return(View(model)); }
public IActionResult EditUnit(NewUnitView model, IFormCollection form) { model.Types = _unitsService.GetUnitTypesForDepartment(DepartmentId); model.Stations = _departmentGroupsService.GetAllStationGroupsForDepartment(DepartmentId); if (!_authorizationService.CanUserModifyUnit(UserId, model.Unit.UnitId)) { Unauthorized(); } if (_unitsService.GetUnitByNameDepartmentId(DepartmentId, model.Unit.Name) != null && _unitsService.GetUnitByNameDepartmentId(DepartmentId, model.Unit.Name).UnitId != model.Unit.UnitId) { ModelState.AddModelError("Name", "Unit with that name already exists."); } var unitRoleNames = (from object key in form.Keys where key.ToString().StartsWith("unitRole_") select form[key.ToString()]).ToList(); var unit = _unitsService.GetUnitById(model.Unit.UnitId); var auditEvent = new AuditEvent(); auditEvent.DepartmentId = DepartmentId; auditEvent.UserId = UserId; auditEvent.Type = AuditLogTypes.UnitChanged; auditEvent.Before = unit.CloneJson(); unit.Name = model.Unit.Name; unit.Type = model.Unit.Type; if (model.Unit.StationGroupId.HasValue && model.Unit.StationGroupId.Value != 0) { unit.StationGroupId = model.Unit.StationGroupId; } else { unit.StationGroupId = null; } if (ModelState.IsValid) { _unitsService.SaveUnit(unit); var roles = new List <UnitRole>(); if (unitRoleNames.Count > 0) { foreach (var roleName in unitRoleNames) { var role = new UnitRole(); role.Name = roleName; role.UnitId = unit.UnitId; roles.Add(role); } } if (roles.Count > 0) { _unitsService.SetRolesForUnit(unit.UnitId, roles); } else { _unitsService.ClearRolesForUnit(unit.UnitId); } auditEvent.After = unit.CloneJson(); _eventAggregator.SendMessage <AuditEvent>(auditEvent); return(RedirectToAction("Index")); } return(View(model)); }