private static async Task <Models.Db.Team> CreateTeam(IDbConnection c, IDbTransaction t, IStorageProvider storage, GroupCoords coords, FootballData.Team team, int sequenceOrder)
        {
            var logoImgUrl = ImportImage(c, t, storage, team.CrestUrl);

            var dbTeam = new Models.Db.Team
            {
                Name       = team.Name,
                KeyName    = String.IsNullOrWhiteSpace(team.Code) ? team.Name.Substring(0, 3) : team.Code,
                LogoImgUrl = logoImgUrl,
                Status     = (int)TeamStatus.Inscribed
            };

            dbTeam.Id = await c.InsertAsync(dbTeam, t);

            await c.InsertAsync(new TournamentTeam
            {
                IdTeam       = dbTeam.Id,
                IdTournament = coords.IdTournament
            }, t);

            var teamGroup = new TeamGroup
            {
                IdTeam        = dbTeam.Id,
                IdGroup       = coords.IdGroup,
                IdStage       = coords.IdStage,
                IdTournament  = coords.IdTournament,
                SequenceOrder = sequenceOrder
            };

            await c.InsertAsync(teamGroup, t);

            return(dbTeam);
        }
        private async Task CreateTeams(IDbConnection c, IDbTransaction t, IStorageProvider storage, Competition competition, GroupCoords coords)
        {
            var teams = new List <Models.Db.Team>();

            var sequenceOrder = 1;

            foreach (var team in competition.Teams)
            {
                Models.Db.Team dbTeam = await CreateTeam(c, t, storage, coords, team, sequenceOrder ++);

                teams.Add(dbTeam);

                await CreateTeamPlayers(c, t, team, dbTeam, coords.IdTournament);
            }

            coords.Tournament.Teams = teams;
        }
        private async Task <Models.Db.Player> CreatePlayer(IDbConnection c, IDbTransaction t, FootballData.Player player, Models.Db.Team dbTeam, long idTournament)
        {
            var(name, surname) = SplitPlayerName(player.Name);

            var dbUser = new Models.Db.User
            {
                Name     = player.Name,
                Level    = (int)UserLevel.Player,
                Salt     = "",
                Password = "",
                Email    = "*****@*****.**",
                Mobile   = "123456789"
            };

            dbUser.Id = await c.InsertAsync(dbUser, t);

            var(fieldPosition, fieldSide) = GetPlayerPosition(player.Position);

            var dbPlayer = new Models.Db.Player
            {
                Name      = name,
                Surname   = surname,
                IdUser    = dbUser.Id,
                BirthDate = player.DateOfBirth.GetValueOrDefault().DateTime,
                Country   = player.Nationality,
                UserData  = dbUser
            };

            dbPlayer.Id = await c.InsertAsync(dbPlayer, t);

            player.DbId = dbPlayer.Id;

            var dbTeamPlayer = new TeamPlayer
            {
                IdPlayer      = dbPlayer.Id,
                IdTeam        = dbTeam.Id,
                ApparelNumber = player.JerseyNumber.GetValueOrDefault(),
                FieldPosition = fieldPosition,
                FieldSide     = fieldSide,
                IsTeamAdmin   = false,
                Status        = 256
            };

            dbPlayer.TeamData = dbTeamPlayer;

            await c.InsertAsync(dbTeamPlayer, t);

            return(dbPlayer);
        }
        private async Task CreateTeamPlayers(IDbConnection c, IDbTransaction t, FootballData.Team team, Models.Db.Team dbTeam, long idTournament)
        {
            var players = new List <Models.Db.Player>();

            if (team.Players != null)
            {
                foreach (var p in team.Players)
                {
                    var dbPlayer = await CreatePlayer(c, t, p, dbTeam, idTournament);

                    players.Add(dbPlayer);
                }
            }
            else
            {
                Console.WriteLine("Warning: team without players: " + team.Name);
            }

            dbTeam.Players = players;
        }
        private static async Task CreateEvent(IDbConnection c, IDbTransaction t, Match dbMatch, List <MatchEvent> events, Models.Db.Team team, Models.Db.Player player, MatchEventType type, int minuteOffset)
        {
            var ev = new MatchEvent
            {
                IdDay       = dbMatch.IdDay,
                IdMatch     = dbMatch.Id,
                IdTeam      = (team != null ? team.Id : 0),
                IdPlayer    = (player != null ? player.Id : 0),
                MatchMinute = new Random().Next(0 + minuteOffset, 45 + minuteOffset),
                Type        = (int)type,
            };

            ev.Id = await c.InsertAsync(ev, t);

            events.Add(ev);
        }
        private static async Task CreateMatchPlayer(IDbConnection c, IDbTransaction t, Match dbMatch, Models.Db.Team team, Models.Db.Player player)
        {
            // Check apparelnumber
            var dbMatchPlayer = new MatchPlayer
            {
                IdMatch       = dbMatch.Id,
                IdPlayer      = player.Id,
                IdTeam        = team.Id,
                IdUser        = player.IdUser,
                ApparelNumber = player.TeamData.ApparelNumber,
                IdDay         = dbMatch.IdDay,
                Player        = player,
                Status        = 1
            };

            await c.InsertAsync(dbMatchPlayer, t);
        }