/// <summary> /// Executes an upsert/delete operation on a simulation's target library data /// Throws a RowNotInTableException if no simulation is found /// </summary> /// <param name="model"></param> /// <param name="db"></param> /// <param name="username"></param> /// <returns></returns> public TargetLibraryModel SaveAnySimulationTargetLibrary(TargetLibraryModel model, BridgeCareContext db) { var id = int.Parse(model.Id); if (!db.Simulations.Any(s => s.SIMULATIONID == id)) { throw new RowNotInTableException($"No scenario was found with id {id}."); } return(SaveSimulationTargetLibrary(model, db)); }
/// <summary> /// Executes an upsert/delete operation on a simulation's target library data if the user owns it /// Throws a RowNotInTableException if no simulation is found for the user /// </summary> /// <param name="model">TargetLibraryModel</param> /// <param name="db">BridgeCareContext</param> /// <param name="username">Username</param> /// <returns>TargetLibraryModel</returns> public TargetLibraryModel SavePermittedSimulationTargetLibrary(TargetLibraryModel model, BridgeCareContext db, string username) { var id = int.Parse(model.Id); if (!db.Simulations.Any(s => s.SIMULATIONID == id)) { throw new RowNotInTableException($"No scenario was found with id {id}."); } if (!db.Simulations.Include(s => s.USERS).First(s => s.SIMULATIONID == id).UserCanModify(username)) { throw new UnauthorizedAccessException("You are not authorized to modify this scenario's targets."); } return(SaveSimulationTargetLibrary(model, db)); }
/// <summary> /// Executes an upsert/delete operation on a simulation's target library data /// Throws a RowNotInTableException if no simulation is found /// </summary> /// <param name="model">TargetLibraryModel</param> /// <param name="db">BridgeCareContext</param> /// <returns>TargetLibraryModel</returns> public TargetLibraryModel SaveSimulationTargetLibrary(TargetLibraryModel model, BridgeCareContext db) { var id = int.Parse(model.Id); if (!db.Simulations.Any(s => s.SIMULATIONID == id)) { throw new RowNotInTableException($"No scenario was found with id {id}."); } var simulation = db.Simulations.Include(s => s.TARGETS).Single(s => s.SIMULATIONID == id); if (simulation.TARGETS.Any()) { simulation.TARGETS.ToList().ForEach(targetEntity => { var targetModel = model.Targets.SingleOrDefault(m => m.Id == targetEntity.ID_.ToString()); if (targetModel == null) { TargetsEntity.DeleteEntry(targetEntity, db); } else { targetModel.matched = true; targetModel.UpdateTarget(targetEntity); } }); } if (model.Targets.Any(m => !m.matched)) { db.Targets .AddRange(model.Targets .Where(targetModel => !targetModel.matched) .Select(targetModel => new TargetsEntity(id, targetModel)) .ToList() ); } db.SaveChanges(); return(new TargetLibraryModel(simulation)); }
public IHttpActionResult SaveSimulationTargetLibrary([FromBody] TargetLibraryModel model) { UserInformationModel userInformation = ESECSecurity.GetUserInformation(Request); return(Ok(TargetLibrarySaveMethods[userInformation.Role](model, userInformation))); }
public IHttpActionResult SaveSimulationTargetLibrary([FromBody] TargetLibraryModel model) => Ok(repo.SaveSimulationTargetLibrary(model, db));