public IActionResult Place(PredictionViewModel vm) { string username = HttpContext.Session.GetString("Account"); if (username != null) { Prediction p = new Prediction(); p.Competition = CompRepo.GetCompetition(vm.Competition_id); p.User = UserRepo.GetUser(username); int position = 1; foreach (int id in vm.Driver_id) { PredictionComponent pc = new PredictionComponent(); pc.Driver_id = id; pc.Position = position; position++; p.Components.Add(pc); } try { repo.Place(p); return(RedirectToAction("Index", "Prediction")); } catch (Exception) { return(View("Error")); } } return(RedirectToAction("LogIn", "User")); }
private List <PredictionComponent> GetPredictionComponents(int prediction_id) { using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand(PredictionQueries.GetPredictionComponents(prediction_id), connection); connection.Open(); try { using (SqlDataReader reader = command.ExecuteReader()) { CompetitionRepository compRepo = new CompetitionRepository(new CompetitionRepositorySQLContext()); List <PredictionComponent> list = new List <PredictionComponent>(); while (reader.Read()) { PredictionComponent pc = new PredictionComponent(); pc.ID = (int)reader["ID"]; pc.Driver_id = (int)reader["driver_id"]; pc.Driver = compRepo.GetDriver(pc.Driver_id); pc.Position = (int)reader["position"]; pc.Checked = (bool)reader["checked"]; if (pc.Checked) { pc.Correct = (bool)reader["correct"]; } list.Add(pc); } return(list); } } catch (SqlException e) { throw e; } } }