private void BtnAddPitch_Click(object sender, EventArgs e) { string PitchName = pitchname.Text; string PitchNumber = pitchnumber.Text; decimal PitchPrice = numprice.Value; if (PitchName != string.Empty && PitchNumber != string.Empty && PitchPrice != 0) { Pitch pt = null; pt = new Pitch { Pitch_Name = PitchName, Pitch_Number = PitchNumber, Price = PitchPrice, Status = 1 }; db.Pitches.Add(pt); db.SaveChanges(); } else { lblError.Visible = true; lblError.Text = "Please filled all input"; } MessageBox.Show("Pitch was added successfully", "Successfully", MessageBoxButtons.OK, MessageBoxIcon.Information); FilldtgViewPitches(); }
public int InsertScores(DateTime startDate) { var scores = ParseCsvScores(startDate); int counter = 0; using (var ctx = new FootballEntities()) { using (var transaction = ctx.Database.BeginTransaction()) { try { //TODO error model foreach (var s in scores) { ctx.Scores.Add(s.ToDbObject()); UpdateLeagueTable(s); counter++; ctx.SaveChanges(); } ctx.SaveChanges(); transaction.Commit(); } catch (Exception) { transaction.Rollback(); } } } return(counter); }
private static void CreateTeam(FootballEntities context, string teamName, Country country, League league) { Team team; if (country == null) { team = context.Teams.FirstOrDefault(t => (t.Country == null) && t.TeamName == teamName); } else { team = context.Teams.FirstOrDefault( t => t.Country.CountryName == country.CountryName && t.TeamName == teamName); } if (team == null) { team = new Team { TeamName = teamName, Country = country }; context.Teams.Add(team); context.SaveChanges(); Console.WriteLine( "Created team: {0} ({1})", team.TeamName, country != null ? team.Country.CountryName : "no country"); if (league != null) { team.Leagues.Add(league); context.SaveChanges(); Console.WriteLine("Added team to league: {0}", league.LeagueName); } } else { Console.WriteLine( "Existing team: {0} ({1})", team.TeamName, country != null ? team.Country.CountryName : "no country"); if (league != null) { if (!team.Leagues.Any(l => l.LeagueName == league.LeagueName)) { team.Leagues.Add(league); context.SaveChanges(); Console.WriteLine("Added team to league: {0}", league.LeagueName); } else { Console.WriteLine("Existing team in league: {0}", league.LeagueName); } } } }
public ActionResult Create([Bind(Include = "idEspecialidad,descripcion,usuarioCreacion,usuarioModificacion,fchCreacion,fchModificacion")] Especialidad especialidad) { if (ModelState.IsValid) { db.Especialidad.Add(especialidad); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(especialidad)); }
public int InsertScores(string csvFilePath, DateTime startDate) { List <ScoreModel> scores = null; if (csvFilePath == null) { scores = ParseCsvScores(startDate); } else { scores = ParseCsvScores(csvFilePath, startDate); } int counter = 0; using (var ctx = new FootballEntities()) { using (var transaction = ctx.Database.BeginTransaction()) { try { foreach (var s in scores) { var dbScore = s.ToDbObject(); if (ctx.Scores.Any(sc => sc.MatchId == dbScore.MatchId)) { logger.WarnFormat("Score already exists. Date: {0}, match: {1}-{2}", s.Date, s.HomeTeam, s.AwayTeam); continue; } else { ctx.Scores.Add(dbScore); logger.InfoFormat("Score added. Date: {0}, match: {1}-{2}", s.Date, s.HomeTeam, s.AwayTeam); } UpdateLeagueTable(s); counter++; ctx.SaveChanges(); } ctx.SaveChanges(); transaction.Commit(); logger.InfoFormat("{0}/{1} scores inserted to database", counter, scores.Count); } catch (Exception e) { logger.Error("Error while inserting scores. Transaction rollback.", e); transaction.Rollback(); } } } return(counter); }
public ActionResult Create([Bind(Include = "idCompeticion,anho,nroFecha,usuarioCreacion,usuarioModificacion,fchCreacion,fchModificacion")] FechaTorneo fechaTorneo) { if (ModelState.IsValid) { db.FechaTorneo.Add(fechaTorneo); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.idCompeticion = new SelectList(db.Torneo, "idCompeticion", "usuarioCreacion", fechaTorneo.idCompeticion); return(View(fechaTorneo)); }
public ActionResult Create([Bind(Include = "codigoFuncionario,Peso,Altura,nroCamiseta,usuarioCreacion,usuarioModificacion,fchCreacion,fchModificacion")] Jugador jugador) { if (ModelState.IsValid) { db.Jugador.Add(jugador); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.codigoFuncionario = new SelectList(db.Funcionario, "codigoFuncionario", "nombre", jugador.codigoFuncionario); return(View(jugador)); }
public ActionResult Create([Bind(Include = "idCompeticion,anho,usuarioCreacion")] Torneo torneo) { if (ModelState.IsValid) { torneo.fchCreacion = DateTime.Now; db.Torneo.Add(torneo); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.idCompeticion = new SelectList(db.Competicion, "IdCompeticion", "nombre", torneo.idCompeticion); return(View(torneo)); }
public ActionResult Create([Bind(Include = "idClub,codigoFuncionario,importe,fchInicio,fchFinProgramado,fchFinReal,usuarioCreacion,usuarioModificacion,fchCreacion,fchModificacion")] Contrato contrato) { if (ModelState.IsValid) { db.Contrato.Add(contrato); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.idClub = new SelectList(db.Club, "idClub", "nombre", contrato.idClub); ViewBag.codigoFuncionario = new SelectList(db.Funcionario, "codigoFuncionario", "nombre", contrato.codigoFuncionario); return(View(contrato)); }
public void Import(XDocument file, FootballEntities context) { var leaguesNodes = file.XPathSelectElements("leagues-and-teams/league"); foreach (var leagueNode in leaguesNodes) { // parse league info // all optional var tempLeague = new Leagues(); string leagueName = null; // check if there is a league if (leagueNode.Element("league-name") != null) { leagueName = leagueNode.Element("league-name").Value; var league = context.Leagues.FirstOrDefault(l => l.LeagueName == leagueName); // league exists + add league if (league != null) { Console.WriteLine("Existing league: " + leagueName); AddTeamsToLeagueAndDb(context, leagueNode, league); } // league doesn't exist - create it else { var newLeague = new Leagues(); newLeague.LeagueName = leagueName; context.Leagues.Add(newLeague); context.SaveChanges(); Console.WriteLine("Created league: " + leagueName); AddTeamsToLeagueAndDb(context, leagueNode, newLeague); } } //AddTeamsToLeagueAndDb(context, leagueNode, tempLeague); // Add the league to the DB // context.Leagues.Add(tempLeague); else { AddTeamsToDb(context, leagueNode); } context.SaveChanges(); } }
public Score SetRatio(Score score) { try { using (var ctx = new FootballEntities()) { var scoreDb = ctx.Scores.SingleOrDefault(s => s.Id == score.Id); var match = ctx.Matches.First(m => m.Id == scoreDb.MatchId); var home = ctx.Teams.First(t => t.Id == match.HomeId).Name; var away = ctx.Teams.First(t => t.Id == match.AwayId).Name; scoreDb.HOR = CalculateOffensiveRatio(GetArchiveScores(home, scoreDb.Date)); scoreDb.HDR = CalculateDefensiveRatio(GetArchiveScores(home, scoreDb.Date)); scoreDb.AOR = CalculateOffensiveRatio(GetArchiveScores(away, scoreDb.Date)); scoreDb.ADR = CalculateDefensiveRatio(GetArchiveScores(away, scoreDb.Date)); scoreDb.HORH = CalculateOffensiveRatio(GetArchiveScores(home, true, scoreDb.Date)); scoreDb.HDRH = CalculateDefensiveRatio(GetArchiveScores(home, true, scoreDb.Date)); scoreDb.AORA = CalculateOffensiveRatio(GetArchiveScores(away, false, scoreDb.Date)); scoreDb.ADRA = CalculateDefensiveRatio(GetArchiveScores(away, false, scoreDb.Date)); ctx.SaveChanges(); logger.InfoFormat("Ratio for teams: {0} and {1} on date: {2} set successfully", home, away, scoreDb.Date); return(scoreDb); } } catch (Exception e) { logger.Error("Error while setting team ratio.", e); return(null); } }
public static void Main() { using (var db = new FootballEntities()) { var xml = XDocument.Load("../../../Helper.Files/leagues-and-teams.xml"); var counter = 1; foreach (var league in xml.Descendants("league")) { Console.WriteLine("Processing league #{0} ...", counter++); var currentLeague = GetOrCreateLeague(league, db); foreach (var team in league.Descendants("team")) { if (team.Attribute("name") == null) { Console.WriteLine("Missing team name attribute."); continue; } var currentTeam = GetOrCreateTeam(team, db); AddTeamToLeague(currentTeam, currentLeague); } db.SaveChanges(); Console.WriteLine(); } } }
public ActionResult Create([Bind(Include = "nombre")] Imagenes imagenes, HttpPostedFileBase file) { Image convert = Image.FromFile("C:\\Users\\Giulliano\\Desktop\\" + file.FileName, true); ImageConverter _imageConverter = new ImageConverter(); byte[] xByte = (byte[])_imageConverter.ConvertTo(convert, typeof(byte[])); if (ModelState.IsValid) { imagenes.image_byte = xByte; db.Imagenes.Add(imagenes); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(imagenes)); }
private static void AddTeamsToLeagueAndDb(FootballEntities context, XElement leagueNode, Leagues tempLeague) { // parse teams info and add to league var teamNodes = leagueNode.XPathSelectElements("teams/team"); foreach (var teamNode in teamNodes) { var tempTeam = new Teams(); var teamName = teamNode.Attributes("name").FirstOrDefault().Value; string teamCountry = null; if (teamNode.Attributes("country").FirstOrDefault() != null) { teamCountry = teamNode.Attributes("country").FirstOrDefault().Value; } var team = context.Teams.FirstOrDefault(t => t.TeamName == teamName); var country = context.Countries.FirstOrDefault(c => c.CountryName == teamCountry); var teamCountryCode = context.Countries.Where(c => c.CountryName == teamCountry).Select(c => c.CountryCode).FirstOrDefault(); var teamCountryCodeInDb = context.Teams.Where(t => t.TeamName == teamName).Select(t => t.CountryCode).FirstOrDefault(); bool sameCountry = true; if (teamCountryCode != null && teamCountryCodeInDb != null) { sameCountry = teamCountryCode.Equals(teamCountryCodeInDb); } // team exists + add team to league if (team != null && country != null && sameCountry) { Console.WriteLine("Existing team: " + teamName); tempLeague.Teams.Add(team); context.SaveChanges(); Console.WriteLine("Added team to league: " + tempLeague.LeagueName); } // missing team name else if (teamName == string.Empty) { Console.WriteLine("Team name is mandatory!"); } // creating new team + add team else { var newTeam = new Teams(); newTeam.TeamName = teamName; newTeam.CountryCode = teamCountryCode; context.Teams.Add(newTeam); Console.WriteLine("Created team: " + teamName); tempLeague.Teams.Add(newTeam); Console.WriteLine("Added team to league: " + tempLeague.LeagueName); } } }
public int InsertAllMatches() { var matches = GetAllMatches(); int counter = 0; using (var ctx = new FootballEntities()) { using (var transaction = ctx.Database.BeginTransaction()) { try { foreach (var m in matches) { ctx.Matches.Add(m.ToDbObject()); counter++; } ctx.SaveChanges(); transaction.Commit(); } catch (Exception) { transaction.Rollback(); } } } return(counter); }
public void InsertTeams() { var teams = GetTeamsFromFile(); using (var ctx = new FootballEntities()) { if (ctx.Teams.Count() == 0) { using (var transaction = ctx.Database.BeginTransaction()) { try { if (teams.Count > 0) { foreach (var t in teams) { ctx.Teams.Add(t); } ctx.SaveChanges(); transaction.Commit(); } else { transaction.Rollback(); } } catch (Exception) { transaction.Rollback(); } } } } }
public ActionResult Create([Bind(Include = "nombre,fchNacimiento,usuarioCreacion")] Socio socio) { var last = (from m in db.Socio select m.codigoSocio).Max(); socio.codigoSocio = last + 1; if (ModelState.IsValid) { socio.fchCreacion = DateTime.Now; db.Socio.Add(socio); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(socio)); }
private static League CreateLeagueIfNotExists(FootballEntities context, XElement xLeague) { League league = null; var xElementLeagueName = xLeague.Element("league-name"); if (xElementLeagueName != null) { string leagueName = xElementLeagueName.Value; league = context.Leagues.FirstOrDefault(l => l.LeagueName == leagueName); if (league != null) { Console.WriteLine("Existing league: {0}", leagueName); } else { // Create a new league in the DB league = new League() { LeagueName = leagueName }; context.Leagues.Add(league); context.SaveChanges(); Console.WriteLine("Created league: {0}", leagueName); } } return(league); }
public int InsertMatches(int matchday) { var matches = GetMatches(matchday); int counter = 0; using (var ctx = new FootballEntities()) { using (var transaction = ctx.Database.BeginTransaction()) { try { foreach (var m in matches) { var matchDb = m.ToDbObject(); matchDb.HomeId = ctx.Teams.First(t => t.Name == m.HomeTeam).Id; matchDb.AwayId = ctx.Teams.First(t => t.Name == m.AwayTeam).Id; ctx.Matches.Add(m.ToDbObject()); counter++; } ctx.SaveChanges(); transaction.Commit(); } catch (Exception) { transaction.Rollback(); } } } return(counter); }
private static League ProcessLeague(FootballEntities context, XElement league) { League newLeague = null; var leagueNode = league.Element("league-name"); if (leagueNode != null) { newLeague = context.Leagues.FirstOrDefault(l => l.LeagueName == leagueNode.Value); if (newLeague == null) { newLeague = new League() { LeagueName = leagueNode.Value }; context.Leagues.AddOrUpdate(newLeague); context.SaveChanges(); Console.WriteLine("Created league: {0}", newLeague.LeagueName); } else { Console.WriteLine("Existing league: {0}", newLeague.LeagueName); } } return(newLeague); }
public Match Predict(Match match) { try { using (var ctx = new FootballEntities()) { var matchDb = ctx.Matches.First(m => m.Id == match.Id); string home = ctx.Teams.First(t => t.Id == matchDb.HomeId).Name; string away = ctx.Teams.First(t => t.Id == matchDb.AwayId).Name; RatioModel ratio = Calculator.CalculateTeamsRatio(home, away, match.Date); var res = Machine.PredictScore(ratio); matchDb.HomeGoalsPredicted = res.Item1; matchDb.AwayGoalsPredicted = res.Item2; ctx.SaveChanges(); return(matchDb); } } catch (Exception e) { logger.Error(string.Format("Error while predicting match: homeId = {0}, awayId = {1}, date = {2}", match.HomeId, match.AwayId, match.Date), e); return(null); } }
public ActionResult Create([Bind(Include = "nombre,fchFundacion,usuarioCreacion")] Federacion federacion) { var last = (from m in db.Federacion select m.idFederacion).Max(); federacion.idFederacion = last + 1; if (ModelState.IsValid) { federacion.fchCreacion = DateTime.Now; db.Federacion.Add(federacion); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(federacion)); }
public ActionResult Create([Bind(Include = "idArbitro,categoria,nombre,usuarioCreacion,usuarioModificacion,fchCreacion,fchModificacion")] Arbitro arbitro) { var last = (from m in db.Arbitro select m.idArbitro).Max(); arbitro.idArbitro = last + 1; if (ModelState.IsValid) { arbitro.fchCreacion = DateTime.Now; db.Arbitro.Add(arbitro); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(arbitro)); }
public ActionResult Create([Bind(Include = "idFederacion,nombre,fchFundacion,usuarioCreacion")] Club club) { var last = (from m in db.Club select m.idClub).Max(); club.idClub = last + 1; if (ModelState.IsValid) { club.fchCreacion = DateTime.Now; db.Club.Add(club); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.idFederacion = new SelectList(db.Federacion, "idFederacion", "idFederacion", club.idFederacion); return(View(club)); }
public ActionResult Create([Bind(Include = "idFederacion,nbrCompeticion,tipo,usuarioCreacion")] Competicion competicion) { var last = (from m in db.Competicion select m.IdCompeticion).Max(); competicion.IdCompeticion = last + 1; if (ModelState.IsValid) { competicion.fchCreacion = DateTime.Now; db.Competicion.Add(competicion); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.idFederacion = new SelectList(db.Federacion, "idFederacion", "idFederacion", competicion.idFederacion); return(View(competicion)); }
public ActionResult Create([Bind(Include = "nombre,fchInicioCarrera,usuarioCreacion")] Entrenador entrenador) { var last = (from m in db.Funcionario select m.codigoFuncionario).Max(); entrenador.codigoFuncionario = last + 1; if (ModelState.IsValid) { entrenador.fchCreacion = DateTime.Now; db.Entrenador.Add(entrenador); db.SaveChanges(); return(RedirectToAction("Index")); } //ViewBag.codigoFuncionario = new SelectList(db.Funcionario, "codigoFuncionario", "idClub", entrenador.codigoFuncionario); return(View(entrenador)); }
public void UpdateLeagueTable(ScoreModel s) { using (var ctx = new FootballEntities()) { var home = ctx.FullStatistics .FirstOrDefault(t => t.Team.Name == s.HomeTeam && t.Season == s.Season); // jesli nie ma jeszcze teamu w tabeli, to go dodaj z zerowym dorobkiem if (home == null) { home = AddToLeagueTable(s.HomeTeam, s.Season); } // jesli nie ma jeszcze teamu w tabeli, to go dodaj z zerowym dorobkiem var away = ctx.FullStatistics .FirstOrDefault(t => t.Team.Name == s.AwayTeam && t.Season == s.Season); if (away == null) { away = AddToLeagueTable(s.AwayTeam, s.Season); } home.MatchesPlayed++; home.GoalsScored += s.HomeGoals; home.GoalsLost += s.AwayGoals; away.MatchesPlayed++; away.GoalsScored += s.AwayGoals; away.GoalsLost += s.HomeGoals; if (s.HomeGoals > s.AwayGoals) { home.MatchesWon++; home.Points += 3; away.MatchesLost++; } else if (s.HomeGoals < s.AwayGoals) { away.MatchesWon++; away.Points += 3; home.MatchesLost++; } else { home.MatchesDrawn++; away.MatchesDrawn++; home.Points++; away.Points++; } ctx.SaveChanges(); } }
public FullStatistic AddToLeagueTable(string teamName, string season) { using (var ctx = new FootballEntities()) { var fs = new FullStatistic(); var team = ctx.Teams.First(t => t.Name == teamName); fs.TeamId = team.Id; fs.Season = season; ctx.FullStatistics.Add(fs); ctx.SaveChanges(); return(fs); } }
public ActionResult Edit([Bind(Include = "idFederacion,nombre,fchFundacion,usuarioModificacion")] Federacion federacion) { if (ModelState.IsValid) { Federacion federacionOut = db.Federacion.Find(federacion.idFederacion); federacion.usuarioCreacion = federacionOut.usuarioCreacion; federacion.fchCreacion = federacionOut.fchCreacion; federacion.fchModificacion = DateTime.Now; var newContext = new FootballEntities(); newContext.Entry(federacion).State = EntityState.Modified; newContext.SaveChanges(); return(RedirectToAction("Index")); } return(View(federacion)); }
public ActionResult Edit([Bind(Include = "idArbitro,categoria,nombre,usuarioModificacion")] Arbitro arbitro) { if (ModelState.IsValid) { Arbitro arbitroOut = db.Arbitro.Find(arbitro.idArbitro); arbitro.usuarioCreacion = arbitroOut.usuarioCreacion; arbitro.fchCreacion = arbitroOut.fchCreacion; arbitro.fchModificacion = DateTime.Now; var newContext = new FootballEntities(); newContext.Entry(arbitro).State = EntityState.Modified; newContext.SaveChanges(); return(RedirectToAction("Index")); } return(View(arbitro)); }