[HttpGet] //this requests data from a specified resource public ActionResult Fixtures(int id) { var model = new Euro2016.Models.TeamFixturesViewModel(); //this is creating a new instance of an object model.Fixtures = new List <Fixture>(); using (var dbContext = new Euro2016Entities()) //this gets a database connection until the curly bracket is closed { var team = dbContext.Teams .Include(x => x.HomeFixtures.Select(y => y.HomeTeam)) .Include(x => x.HomeFixtures.Select(y => y.AwayTeam)) .Include(x => x.HomeFixtures.Select(y => y.Venue)) .Include(x => x.AwayFixtures.Select(y => y.HomeTeam)) .Include(x => x.AwayFixtures.Select(y => y.AwayTeam)) .Include(x => x.AwayFixtures.Select(y => y.Venue)) .Where(x => x.Id == id).FirstOrDefault(); model.Fixtures.AddRange(team.HomeFixtures.ToList()); model.Fixtures.AddRange(team.AwayFixtures.ToList()); model.Team = team; } return(View(model)); }
// GET: Venues public ActionResult Index() { var model = new Euro2016.Models.VenuesViewModel(); //this is creating a new instance of an object using (var dbContext = new Euro2016Entities()) //this gets a database connection until the curly bracket is closed { model.Venues = dbContext.Venues.ToList(); } return(View(model)); }
[Authorize] //this means the user has to login to access the view public ActionResult Add() { var model = new Euro2016.Models.FixturesViewModel(); //this is creating a new instance of an object //this is creating a new instance of an object //this is creating a new instance of an object using (var dbContext = new Euro2016Entities()) //this gets a database connection until the curly bracket is closed { model.Teams = dbContext.Teams.OrderBy(x => x.Name).ToList(); model.Venues = dbContext.Venues.ToList(); } return(View(model)); }
[Authorize] //this means the user has to login to access the view public ActionResult Edit(int id) { var model = new Euro2016.Models.VenuesViewModel(); //this is creating a new instance of an object using (var dbContext = new Euro2016Entities()) //this gets a database connection until the curly bracket is closed { var venue = dbContext.Venues.Where(x => x.Id == id).FirstOrDefault(); model.Name = venue.Name; } return(View(model)); }
// GET: Home public ActionResult Tables() { var model = new Euro2016.Models.HomeViewModel(); //this is creating a new instance of an object using (var dbContext = new Euro2016Entities()) //this gets a database connection until the curly bracket is closed { model.Groups = dbContext.Groups .Include("Teams") .ToList(); } return(View(model)); }
[Authorize] //this means the user has to login to access the view public ActionResult Edit(int id) { var model = new Euro2016.Models.TeamViewModel(); //this is creating a new instance of an object using (var dbContext = new Euro2016Entities()) //this gets a database connection until the curly bracket is closed { var team = dbContext.Teams.Where(x => x.Id == id).FirstOrDefault(); model.Name = team.Name; model.Flag = team.Flag; model.GroupId = team.GroupId; model.Groups = dbContext.Groups.ToList(); } return(View(model)); }
// GET: Results public ActionResult Index() { var model = new Euro2016.Models.ResultsViewModel(); //this is creating a new instance of an object using (var dbContext = new Euro2016Entities()) //this gets a database connection until the curly bracket is closed { model.Results = dbContext.Results .Include(x => x.Fixture.HomeTeam) .Include(x => x.Fixture.AwayTeam) .Include(x => x.Fixture.Venue) .ToList(); } return(View(model)); }
[Authorize] //this means the user has to login to access the view public ActionResult Edit(int id) { var model = new Euro2016.Models.FixturesViewModel(); //this is creating a new instance of an object using (var dbContext = new Euro2016Entities()) //this gets a database connection until the curly bracket is closed { var fixture = dbContext.Fixtures.Where(x => x.Id == id).FirstOrDefault(); model.HomeTeamId = fixture.HomeTeamId; model.AwayTeamId = fixture.AwayTeamId; model.VenueId = fixture.VenueId; model.FixtureDate = fixture.Date; model.Teams = dbContext.Teams.OrderBy(x => x.Name).ToList(); model.Venues = dbContext.Venues.ToList(); } return(View(model)); }
[Authorize] //this means the user has to login to access the view public ActionResult Edit(Euro2016.Models.VenuesViewModel model) { if (!ModelState.IsValid) //this checks if the model is valid, if it's not valid then we are returning the view and displaying an error. The model is invalid if a required property is not filled in for example. { return(View(model)); } using (var dbContext = new Euro2016Entities()) //this gets a database connection until the curly bracket is closed { var venue = dbContext.Venues.Where(x => x.Id == model.Id).FirstOrDefault(); venue.Name = model.Name; dbContext.SaveChanges(); } return(RedirectToAction("Index")); }
[Authorize] //this means the user has to login to access the view public ActionResult Add(Euro2016.Models.VenuesViewModel model) { if (!ModelState.IsValid) //this checks if the model is valid, if it's not valid then we are returning the view and displaying an error. The model is invalid if a required property is not filled in for example. { return(View(model)); } var venue = new Venue(); //this is creating a new instance of an object venue.Name = model.Name; venue.CreationDate = DateTime.Now; venue.CreationSource = "Dan"; using (var dbContext = new Euro2016Entities()) //this gets a database connection until the curly bracket is closed { dbContext.Venues.Add(venue); dbContext.SaveChanges(); } return(RedirectToAction("Index")); }
[Authorize] //this means the user has to login to access the view public ActionResult Delete(int id) { using (var dbContext = new Euro2016Entities()) //this gets a database connection until the curly bracket is closed { var group = dbContext.Groups.Where(x => x.Id == id).FirstOrDefault(); if (group != null) { if (group.Teams.Any()) { TempData.Add("Message", "Cannot delete group as it contains teams, please delete the teams first."); return(RedirectToAction("Index")); } dbContext.Groups.Remove(group); dbContext.SaveChanges(); } } return(RedirectToAction("Index")); }
[Authorize] //this means the user has to login to access the view public ActionResult Delete(int id) { using (var dbContext = new Euro2016Entities()) //this gets a database connection until the curly bracket is closed { var venue = dbContext.Venues.Where(x => x.Id == id).FirstOrDefault(); if (venue != null) { if (venue.Fixtures.Any()) { TempData.Add("Message", "Cannot delete venue as it has fixtures, please delete the fixtures first."); return(RedirectToAction("Index")); } dbContext.Venues.Remove(venue); dbContext.SaveChanges(); } } return(RedirectToAction("Index")); }
[Authorize] //this means the user has to login to access the view public ActionResult Add(Euro2016.Models.TeamViewModel model) { using (var dbContext = new Euro2016Entities()) //this gets a database connection until the curly bracket is closed { model.Groups = dbContext.Groups.ToList(); } if (!ModelState.IsValid) //this checks if the model is valid, if it's not valid then we are returning the view and displaying an error. The model is invalid if a required property is not filled in for example. { return(View(model)); } using (var dbContext = new Euro2016Entities()) //this gets a database connection until the curly bracket is closed { var group = dbContext.Groups.Where(x => x.Id == model.GroupId).FirstOrDefault(); var teamCount = group.Teams.Count(); if (teamCount >= 4) { TempData.Add("Message", string.Format("{0} already has 4 teams, please choose another group.", group.Name)); return(View(model)); } } var team = new Team(); //this is creating a new instance of an object team.Name = model.Name; team.Flag = model.Flag; team.GroupId = model.GroupId; team.CreationDate = DateTime.Now; team.CreationSource = "Dan"; using (var dbContext = new Euro2016Entities()) //this gets a database connection until the curly bracket is closed { dbContext.Teams.Add(team); dbContext.SaveChanges(); } return(RedirectToAction("Index")); }
[Authorize] //this means the user has to login to access the view public ActionResult Edit(Euro2016.Models.TeamViewModel model) { if (!ModelState.IsValid) //this checks if the model is valid, if it's not valid then we are returning the view and displaying an error. The model is invalid if a required property is not filled in for example. { return(View(model)); } using (var dbContext = new Euro2016Entities()) //this gets a database connection until the curly bracket is closed { var team = dbContext.Teams.Where(x => x.Id == model.Id).FirstOrDefault(); team.MaintenanceDate = DateTime.Now; team.MaintenanceSource = "Dan"; team.Name = model.Name; team.GroupId = model.GroupId; team.Flag = model.Flag; dbContext.SaveChanges(); } return(RedirectToAction("Index")); }
[HttpGet] //this requests data from a specified resource public ActionResult Fixtures(int id) { var model = new Euro2016.Models.VenueFixturesViewModel(); //this is creating a new instance of an object model.Fixtures = new List <Fixture>(); //This is getting the requested venue from the database and including the venue fixtures using (var dbContext = new Euro2016Entities()) //this gets a database connection until the curly bracket is closed { var venue = dbContext.Venues .Include(x => x.Fixtures.Select(y => y.HomeTeam)) .Include(x => x.Fixtures.Select(y => y.AwayTeam)) .Where(x => x.Id == id).FirstOrDefault(); model.Fixtures.AddRange(venue.Fixtures.ToList()); model.Venue = venue; } return(View(model)); }
[Authorize] //this means the user has to login to access the view public ActionResult Edit(Euro2016.Models.FixturesViewModel model) { if (!ModelState.IsValid) //this checks if the model is valid, if it's not valid then we are returning the view and displaying an error. The model is invalid if a required property is not filled in for example. { return(View(model)); } using (var dbContext = new Euro2016Entities()) //this gets a database connection until the curly bracket is closed { var fixture = dbContext.Fixtures.Where(x => x.Id == model.Id).FirstOrDefault(); fixture.MaintenanceDate = DateTime.Now; fixture.MaintenanceSource = "Dan"; fixture.HomeTeamId = model.HomeTeamId; fixture.AwayTeamId = model.AwayTeamId; fixture.VenueId = model.VenueId; fixture.Date = model.FixtureDate.GetValueOrDefault(); dbContext.SaveChanges(); } return(RedirectToAction("Index")); }
[Authorize] //this means the user has to login to access the view public ActionResult Add(Euro2016.Models.FixturesViewModel model) { using (var dbContext = new Euro2016Entities()) //this gets a database connection until the curly bracket is closed { model.Teams = dbContext.Teams.OrderBy(x => x.Name).ToList(); model.Venues = dbContext.Venues.ToList(); } if (!ModelState.IsValid) //this checks if the model is valid, if it's not valid then we are returning the view and displaying an error. The model is invalid if a required property is not filled in for example. { return(View(model)); } if (model.HomeTeamId == model.AwayTeamId) { TempData.Add("Message", "Please select two different teams."); return(View(model)); } var fixture = new Fixture(); //this is creating a new instance of an object fixture.HomeTeamId = model.HomeTeamId; fixture.AwayTeamId = model.AwayTeamId; fixture.VenueId = model.VenueId; fixture.Date = model.FixtureDate.GetValueOrDefault(); fixture.CreationDate = DateTime.Now; fixture.CreationSource = "Dan"; using (var dbContext = new Euro2016Entities()) //this gets a database connection until the curly bracket is closed { dbContext.Fixtures.Add(fixture); dbContext.SaveChanges(); } return(RedirectToAction("Index")); }
public ActionResult GenerateResults() { var rnd = new Random(); //this is creating a new instance of an object var maxScore = 5; using (var dbContext = new Euro2016Entities()) //this gets a database connection until the curly bracket is closed { //Clear results var results = dbContext.Results.ToList(); foreach (var result in results) { dbContext.Results.Remove(result); } //Reset Teams var teams = dbContext.Teams.ToList(); foreach (var team in teams) { team.Played = 0; team.Won = 0; team.Drawn = 0; team.Lost = 0; team.Scored = 0; team.Conceded = 0; team.Points = 0; } //dbContext.SaveChanges(); var fixtures = dbContext.Fixtures.ToList(); foreach (var fixture in fixtures) { var homeScore = rnd.Next(maxScore); var awayScore = rnd.Next(maxScore); var homeTeam = fixture.HomeTeam; var awayTeam = fixture.AwayTeam; homeTeam.Played = homeTeam.Played + 1; awayTeam.Played = awayTeam.Played + 1; homeTeam.Scored = homeTeam.Scored + homeScore; awayTeam.Scored = awayTeam.Scored + awayScore; homeTeam.Conceded = homeTeam.Conceded + awayScore; awayTeam.Conceded = awayTeam.Conceded + homeScore; if (homeScore > awayScore) { homeTeam.Won = homeTeam.Won + 1; homeTeam.Points = homeTeam.Points + 3; awayTeam.Lost = awayTeam.Lost + 1; } if (awayScore > homeScore) { awayTeam.Won = awayTeam.Won + 1; awayTeam.Points = awayTeam.Points + 3; homeTeam.Lost = homeTeam.Lost + 1; } if (homeScore == awayScore) { homeTeam.Drawn = homeTeam.Drawn + 1; awayTeam.Drawn = awayTeam.Drawn + 1; homeTeam.Points = homeTeam.Points + 1; awayTeam.Points = awayTeam.Points + 1; } var result = new Result(); //this is creating a new instance of an object result.CreationDate = DateTime.Now; result.CreationSource = "Dan"; result.HomeScore = homeScore; result.AwayScore = awayScore; result.FixtureId = fixture.Id; dbContext.Results.Add(result); } dbContext.SaveChanges(); } return(RedirectToAction("Tables", "Home")); }