public static int getWeekId(long id)
 {
     int weekId = 0;
     using (var context = new escorcenterdbEntities())
     {
         weekId = (from w in context.weeks where w.id == id select w.id).FirstOrDefault<int>();
     }
     return weekId;
 }
 public IHttpActionResult GetWeekId(long id)
 {
     int weekId = getWeekId(id);
     week _week;
     using (var context = new escorcenterdbEntities())
     {
         _week = (from w in context.weeks where w.id == weekId select w).FirstOrDefault<week>();
     }
     return Ok(_week);
 }
 public IHttpActionResult CreateField([FromBody]FieldDTO field)
 {
     field f = new field { address = field.Address, description = field.Description, enabled = true, latitude = (float)field.latitude, longitude = (float)field.longitude, name = field.Name, sport = (int)field.sport };
     using (var context = new escorcenterdbEntities())
     {
         context.fields.Add(f);
         context.SaveChanges();
     }
     return Ok(f);
 }
 public IHttpActionResult DeleteField(long id)
 {
     field f;
     using (var context = new escorcenterdbEntities())
     {
         f = (from r in context.fields where r.Id == id select r).FirstOrDefault();
         context.SaveChanges();
     }
     return Ok(f);
 }
 public IHttpActionResult CreateWeek(WeekDTO _week)
 {
     week w = new week { dateFrom = DateTime.Now, dateTo = new DateTime(2016, 02, 29), enabled = true, description = _week.Description, season = (int)_week.Season, title = _week.Title };
     using (var context = new escorcenterdbEntities())
     {
         context.weeks.Add(w);
         context.SaveChanges();
     }
     return Ok(w);
 }
 public IHttpActionResult DeleteWeek(long id)
 {
     week w;
     using (var context = new escorcenterdbEntities())
     {
         w = (from r in context.weeks where r.id == id select r).FirstOrDefault();
         context.SaveChanges();
     }
     return Ok(w);
 }
 public IHttpActionResult CreateLeague([FromBody]LeagueDTO _league)
 {
     league l = new league() { Name = _league.Name, Description = _league.Description, Enabled = true, Region = _league.Region };
     using (var context = new escorcenterdbEntities())
     {
         context.leagues.Add(l);
         context.SaveChanges();
     }
     return Ok(l);
 }
 public IHttpActionResult CreateSeason([FromBody]SeasonDTO _season)
 {
     season s = new season() { description = _season.Description, league = (int)_season.League, dateFrom = DateTime.Now, dateTo = new DateTime(2016, 04, 29), title = _season.Title };
     using (var context = new escorcenterdbEntities())
     {
         context.seasons.Add(s);
         context.SaveChanges();
     }
     return Ok(s);
 }
 public IHttpActionResult DeleteTeams(long id)
 {
     team _team;
     using (var context = new escorcenterdbEntities())
     {
         _team = (from t in context.teams where t.Id == id select t).FirstOrDefault();
         context.teams.Remove(_team);
         context.SaveChanges();
     }
     return Ok(_team);
 }
 public IHttpActionResult DeleteLeague(long id)
 {
     league l;
     using (var context = new escorcenterdbEntities())
     {
         l = (from r in context.leagues where r.Id == id select r).FirstOrDefault();
         context.leagues.Remove(l);
         context.SaveChanges();
     }
     return Ok(l);
 }
 public IHttpActionResult CreateMatch(MatchDTO _match)
 {
     int weekId = WeeksApiController.getWeekId(_match.Id);
     match m = new match { date = DateTime.Parse(_match.Date), details = _match.Details, enabled = true, field = (int)_match.Field.Id, scoreTeam1 = (sbyte)_match.ScoreTeam1, scoreTeam2 = (sbyte)_match.ScoreTeam2, team1 = (int)_match.Team1.Id, team2 = (int)_match.Team2.Id, week = weekId };
     using (var context = new escorcenterdbEntities())
     {
         context.matches.Add(m);
         context.SaveChanges();
     }
     return Ok(m);
 }
 public IHttpActionResult DeleteNotification(long id)
 {
     notification _notification;
     using (var context = new escorcenterdbEntities())
     {
         _notification = (from n in context.notifications where n.Id == id select n).FirstOrDefault();
         context.notifications.Remove(_notification);
         context.SaveChanges();
     }
     return Ok(_notification);
 }
 public IHttpActionResult DeleteMatch(long id)
 {
     match _match;
     using (var context = new escorcenterdbEntities())
     {
         _match = (from m in context.matches where m.Id == id select m).FirstOrDefault();
         context.matches.Remove(_match);
         context.SaveChanges();
     }
     return Ok(_match);
 }
 public IHttpActionResult UpdateMatch([FromBody] MatchDTO matchDTO)
 {
     match matchMap = AutoMapper.Mapper.Map<MatchDTO, match>(matchDTO);
     match _match;
     using (var context = new escorcenterdbEntities())
     {
         _match = (from m in context.matches where m.Id == matchMap.Id select m).FirstOrDefault();
         _match = matchMap;
         context.SaveChanges();
     }
     return Ok(_match);
 }
 public IHttpActionResult UpdateField([FromBody] FieldDTO _field)
 {
     field fieldMap = AutoMapper.Mapper.Map<FieldDTO, field>(_field);
     field f;
     using (var context = new escorcenterdbEntities())
     {
         f = (from r in context.fields where r.Id == fieldMap.Id select r).FirstOrDefault();
         f = fieldMap;
         context.SaveChanges();
     }
     return Ok(f);
 }
 public IHttpActionResult CreateTeam([FromBody]TeamDTO _team)
 {
     LeaguesApiController leaguesApiController = new LeaguesApiController();
     int leagueId = leaguesApiController.getLeagueIdByTeamId(_team.Id);
     team t = new team { Name = _team.Name, Description = _team.Description, League = leagueId, Enabled = true, Logo = _team.Logo };
     using (var context = new escorcenterdbEntities())
     {
         context.teams.Add(t);
         context.SaveChanges();
     }
     return Ok(t);
 }
 public IHttpActionResult UpdateWeeks([FromBody]WeekDTO _week)
 {
     week weekMap = AutoMapper.Mapper.Map<WeekDTO, week>(_week);
     week w;
     using (var context = new escorcenterdbEntities())
     {
         w = (from r in context.weeks where r.id == weekMap.id select r).FirstOrDefault();
         w = weekMap;
         context.SaveChanges();
     }
     return Ok(w);
 }
 public ActionResult League(int regionId)
 {
     region region = null;
     using(var context = new escorcenterdbEntities() )
     {
         region = context.regions.Find(regionId);
     }
     if(region != null)
     {
         ViewBag.regionName = region.Name;
     }
     return View();
 }
 public long GetCurrentSeasonId(long leagueId, DateTime date)
 {
     season _season = null;
     using (var context = new escorcenterdbEntities())
     {
         _season = (from s in context.seasons where s.league == leagueId && s.dateFrom <= date && s.dateTo >= date && s.enabled == true select s).FirstOrDefault<season>();
     }
     if (_season == null)
     {
         return 0;
     }
     return _season.id;
 }
 public IHttpActionResult GetSeasons()
 {
     season[] _season = null;
     using (var context = new escorcenterdbEntities())
     {
         _season= (from s in context.seasons select s).ToArray<season>();
     }
     if (_season == null)
     {
         return Ok();
     }
     SeasonDTO[] seasons = AutoMapper.Mapper.Map<season[], SeasonDTO[]>(_season);
     return Ok(seasons);
 }
        public IHttpActionResult GetNotifications(string AllValues)
        {
            string[] values = AllValues.Split('/');
            HashSet<int> leagueId = new HashSet<int>();
            HashSet<int> teamId = new HashSet<int>();
            bool teamAppeared = false;

            foreach (string value in values)
            {
                if (value.Equals("teams"))
                {
                    teamAppeared = true;
                }
                else if (teamAppeared)
                {
                    teamId.Add(int.Parse(value));
                }
                else
                {
                    leagueId.Add(int.Parse(value));
                }
            }

            List<notification> _notifications;

            using (var context = new escorcenterdbEntities())
            {
                _notifications = ((from n in context.notifications.AsEnumerable()
                                   where IncludeNotification(n, teamId, leagueId)
                                   select n).OrderBy(n => n.Date).ToList<notification>());
            }

            if (_notifications == null)
            {
                return Ok();
            }

            _notifications = _notifications.OrderBy(n => n.Id).ToList().ToList<notification>();
            NotificationDTO[] notifications = AutoMapper.Mapper.Map<notification[], NotificationDTO[]>(_notifications.ToArray());

            NotificationListDTO notificationList = new NotificationListDTO();
            notificationList.items.AddRange(notifications);
            if (notificationList.items.Count == 0)
            {
                return Ok();
            }
            return Ok(notificationList);
        }
 public IHttpActionResult CreateNotification([FromBody]NotificationDTO _notification, TeamListDTO _teamList, LeagueDTO _league)
 {
     String teamsId = "";
     List<TeamDTO> teams = _teamList.items;
     foreach (TeamDTO t in teams)
     {
         teamsId = teamsId + "<" + t.Id + ">";
     }
     notification n = new notification { Content = _notification.Content, Date = DateTime.Parse(_notification.Date), Enabled = true, Title = _notification.Title, TeamsId = teamsId, LeagueId = (int)_league.Id };
     using (var context = new escorcenterdbEntities())
     {
         context.notifications.Add(n);
         context.SaveChanges();
     }
     return Ok();
 }
        public IHttpActionResult GetAllTeams()
        {
            team[] _teams = null;
            using (var context = new escorcenterdbEntities())
            {
                _teams = (from t in context.teams where t.Enabled select t).OrderBy(x => x.Name).ToArray<team>();
            }

            if (_teams == null)
                return Ok();
            TeamDTO[] _teamsDTO = AutoMapper.Mapper.Map<team[], TeamDTO[]>(_teams);
            TeamListDTO teamList = new TeamListDTO() { PageNumber = 1, PageTotal = 1 };
            teamList.items.AddRange(_teamsDTO);

            return Ok(teamList);
        }
        public IHttpActionResult GetScoreTableResult(int teamId)
        {
            scoretableview _scoreTableView = null;
            LeaguesApiController leagueApi = new LeaguesApiController();
            int position = 0;

            team _team = null;
            scoretableview[] _scoreTable = null;

            using (var context = new escorcenterdbEntities())
            {
                _scoreTableView = (from s in context.scoretableviews where s.team == teamId select s).FirstOrDefault<scoretableview>();
                _team = (from t in context.teams where t.Id == teamId && t.Enabled == true select t).FirstOrDefault<team>();
                long seasonId = leagueApi.GetCurrentSeasonId(_team.League, DateTime.Now);
                _scoreTable = (from st in context.scoretableviews where st.season == seasonId select st).OrderBy(st => st.GamesWon.Value * 3 + st.GamesDrawn.Value * 1).ToArray<scoretableview>();
            }
            if (_scoreTableView == null)
                return Ok();
            foreach (scoretableview st in _scoreTable)
            {
                if (st.team == teamId)
                    break;
                position++;
            }
            // scoretableview y ScoreTableResultDTO son incompatibles team del primero es Int32, team del segundo es TeamDTO
            TeamDTO teamDTO = AutoMapper.Mapper.Map<team, TeamDTO>(_team);
            ScoreTableResultDTO scoreTableResultDTO = new ScoreTableResultDTO()
            {
                League = leagueApi.getLeagueNameById(_team.League),
                Position = (long)position,
                GamesDrawn = (long)_scoreTableView.GamesDrawn,
                GamesLost = (long)_scoreTableView.GamesLost,
                GamesPlayed = (long)_scoreTableView.GamesPlayed,
                GamesWined = (long)_scoreTableView.GamesWon,
                Points = (long)_scoreTableView.ScoreFavor,
                ScoreFavor = (long)_scoreTableView.ScoreFavor,
                ScoreDifference = (long)_scoreTableView.ScoreDifference,
                ScoreAgainst = (long)_scoreTableView.ScoreAgainst,
                Team = teamDTO
            };
            return Ok(scoreTableResultDTO);
        }
        public IHttpActionResult GetFutureMatchesByLeagueId(long leagueId)
        {
            DateTime now = DateTime.Now;
            long seasonId = GetCurrentSeasonId(leagueId, now);
            if (seasonId < 0)
                return Ok();

            season _season = null;
            List<WeekDTO> weeks = new List<WeekDTO>();
            using (var context = new escorcenterdbEntities())
            {
                week[] _weeks = (from w in context.weeks where w.enabled == true && w.season == seasonId && now <= w.dateTo select w).OrderBy(w => w.dateFrom).ToArray<week>();
                foreach (week _week in _weeks)
                {
                    WeekDTO week = GetFutureMatchesByWeekId(_week.id, now);
                    if (week != null)
                    {
                        weeks.Add(week);
                    }
                }
                _season = (from s in context.seasons where s.id == seasonId select s).FirstOrDefault<season>();
            }
            if (_season == null)
            {
                return Ok();
            }
            SeasonDTO season = new SeasonDTO();
            season.Title = _season.title;
            season.DateFrom = _season.dateFrom.ToString();
            season.DateTo = _season.dateTo.ToString();
            season.Description = _season.description;
            season.Id = _season.id;
            season.League = _season.league;
            season.Weeks.AddRange(weeks);
            return Ok(season);
        }
        public IHttpActionResult GetLeagueById(long id = 0)
        {
            league _league = null;
            team[] _teams;
            using (var context = new escorcenterdbEntities())
            {
                _league = (from l in context.leagues where l.Id == id && l.Enabled == true select l).FirstOrDefault<league>();
                _teams = (from t in context.teams where t.League == id && t.Enabled == true select t).ToArray<team>();
            }

            if (_league == null)
            {
                return NotFound();
            }

            LeagueDTO league = AutoMapper.Mapper.Map<LeagueDTO>(_league);
            TeamDTO[] teams = AutoMapper.Mapper.Map<team[], TeamDTO[]>(_teams.ToArray());
            league.teams.AddRange(teams);
            return Ok(league);
        }
 List<MatchDTO> ParseMatches(match[] _matches)
 {
     List<MatchDTO> matches = new List<MatchDTO>();
     using (var context = new escorcenterdbEntities())
     {
         foreach (match _match in _matches)
         {
             team team1 = (from t in context.teams where t.Id == _match.team1 select t).FirstOrDefault<team>();
             team team2 = (from t in context.teams where t.Id == _match.team2 select t).FirstOrDefault<team>();
             field field = (from f in context.fields where f.Id == _match.field select f).FirstOrDefault<field>();
             MatchDTO match = new MatchDTO
             {
                 Team1 = AutoMapper.Mapper.Map<team, TeamDTO>(team1),
                 ScoreTeam1 = _match.scoreTeam1,
                 Team2 = AutoMapper.Mapper.Map<team, TeamDTO>(team2),
                 ScoreTeam2 = _match.scoreTeam2,
                 Date = _match.date.ToString(),
                 Details = _match.details,
                 Field = AutoMapper.Mapper.Map<field, FieldDTO>(field),
                 Finished = _match.finished,
                 Forfeit = _match.forfeit,
                 Id = _match.Id,
                 ScoreExtraTimeTeam1 = 0,
                 ScoreExtraTimeTeam2 = 0
             };
             matches.Add(match);
         }
     }
     return matches;
 }
        WeekDTO GetPastMatchesByWeekId(long weekId, DateTime date)
        {
            match[] _matches = null;
            List<MatchDTO> matches = new List<MatchDTO>();
            week _week = null;

            using (var context = new escorcenterdbEntities())
            {
                _week = (from w in context.weeks where w.enabled == true && w.id == weekId select w).FirstOrDefault<week>();
                _matches = (from m in context.matches where m.enabled == true && m.week == _week.id && m.date < date select m).OrderByDescending(m => m.date).ToArray<match>();
            }
            matches = ParseMatches(_matches);
            if (matches == null || _week == null)
            {
                return null;
            }

            WeekDTO week = new WeekDTO()
            {
                DateFrom = _week.dateFrom.ToString(),
                DateTo = _week.dateTo.ToString(),
                Description = _week.description,
                Id = _week.id,
                Season = _week.season,
                Title = _week.title
            };

            week.matches.AddRange(matches);
            return week;
        }
 public IHttpActionResult UpdateLeague([FromBody] LeagueDTO leagueDto)
 {
     league leagueMap = AutoMapper.Mapper.Map<LeagueDTO, league>(leagueDto);
     league l;
     using (var context = new escorcenterdbEntities())
     {
         l = (from r in context.leagues where r.Id == leagueMap.Id select r).FirstOrDefault();
         l = leagueMap;
         context.SaveChanges();
     }
     return Ok(l);
 }
        public IHttpActionResult GetResultTable(int leagueId)
        {
            long seasonId = GetCurrentSeasonId(leagueId, DateTime.Now);
            if (seasonId == 0)
            {
                return Ok();
            }

            scoretableview[] _scoreTable = null;
            season _season = null;
            List<ScoreTableResultDTO> scoreTable = new List<ScoreTableResultDTO>();

            using (var context = new escorcenterdbEntities())
            {
                _scoreTable = (from st in context.scoretableviews where st.season == seasonId select st).OrderBy(st => st.GamesWon.Value * 3 + st.GamesDrawn.Value * 1).ToArray<scoretableview>();
                _season = (from s in context.seasons where s.id == seasonId && s.enabled == true select s).FirstOrDefault<season>();

                if (_scoreTable == null || _season == null)
                {
                    return NotFound();
                }

                foreach (scoretableview st in _scoreTable)
                {
                    team _team = (from t in context.teams where t.Id == st.team select t).FirstOrDefault<team>();

                    String leagueName = getLeagueNameById(_team.League);
                    ScoreTableResultDTO result = new ScoreTableResultDTO
                    {
                        Team = AutoMapper.Mapper.Map<team, TeamDTO>(_team),
                        GamesDrawn = st.GamesDrawn.Value,
                        GamesLost = st.GamesLost.Value,
                        GamesPlayed = st.GamesPlayed.Value,
                        GamesWined = st.GamesWon.Value,
                        //Cambiar esto a hacerlo dinamico, no solo para el fut
                        Points = st.GamesWon.Value * 3 + st.GamesDrawn.Value * 1,
                        ScoreAgainst = (long)st.ScoreAgainst.Value,
                        ScoreDifference = (long)st.ScoreDifference.Value,
                        ScoreFavor = (long)st.ScoreFavor.Value,
                        League = leagueName
                    };

                    scoreTable.Add(result);
                }
            }
            if (_season == null)
            {
                return Ok();
            }

            scoreTable.OrderBy(r => r.Points);

            SeasonDTO season = new SeasonDTO
            {
                DateFrom = _season.dateFrom.ToString(),
                DateTo = _season.dateTo.ToString(),
                Description = _season.description,
                Id = _season.id,
                League = _season.league,
                Title = _season.title
            };

            season.ScoreTableResult.AddRange(scoreTable);
            return Ok(season);
        }