Пример #1
0
class GameModel(mlbdb.Model):
    __tablename__ = 'games'
    game_id = mlbdb.Column(UUID(as_uuid=True), primary_key=True)
    date = mlbdb.Column(mlbdb.String, unique=False, nullable=False)
    winner_id = mlbdb.Column(UUID(as_uuid=True),
                             mlbdb.ForeignKey('teams.team_id'),
                             nullable=False)
    loser_id = mlbdb.Column(UUID(as_uuid=True),
                            mlbdb.ForeignKey('teams.team_id'),
                            nullable=False)
    winner_score = mlbdb.Column(mlbdb.Integer, unique=False, nullable=False)
    loser_score = mlbdb.Column(mlbdb.Integer, unique=False, nullable=False)
    winning_pitcher_id = mlbdb.Column(UUID(as_uuid=True),
                                      mlbdb.ForeignKey('players.player_id'),
                                      nullable=False)
    losing_pitcher_id = mlbdb.Column(UUID(as_uuid=True),
                                     mlbdb.ForeignKey('players.player_id'),
                                     nullable=False)
    weather_report_id = mlbdb.Column(
        UUID(as_uuid=True),
        mlbdb.ForeignKey('weather_reports.weather_report_id'),
        nullable=False)
    expectation_id = mlbdb.Column(
        UUID(as_uuid=True),
        mlbdb.ForeignKey('expectations.expectation_id'),
        nullable=False)
    injury_reports_away_team = mlbdb.Column(
        UUID(as_uuid=True),
        mlbdb.ForeignKey('injury_reports.injury_report_id'),
        nullable=False)
    injury_reports_home_team = mlbdb.Column(
        UUID(as_uuid=True),
        mlbdb.ForeignKey('injury_reports.injury_report_id'),
        nullable=False)
    away_team_id = mlbdb.Column(UUID(as_uuid=True),
                                mlbdb.ForeignKey('teams.team_id'),
                                nullable=False)
    home_team_id = mlbdb.Column(UUID(as_uuid=True),
                                mlbdb.ForeignKey('teams.team_id'),
                                nullable=False)
    dome = mlbdb.Column(mlbdb.Boolean, unique=False, nullable=False)

    def as_dict(self):
        return {
            'game_id': self.game_id,
            'date': self.date,
            'dome': self.dome,
            'away_team_id': self.away_team_id,
            'home_team_id': self.home_team_id,
            'winner_id': self.winner_id,
            'loser_id': self.loser_id,
            'winner_score': self.winner_score,
            'loser_score': self.loser_score,
            'winning_pitcher_id': self.winning_pitcher_id,
            'losing_pitcher_id': self.losing_pitcher_id,
            'weather_report_id': self.weather_report_id,
            'expectation_id': self.expectation_id,
            'injury_reports_away_team': self.injury_reports_away_team,
            'injury_reports_home_team': self.injury_reports_home_team
        }

    @staticmethod
    def get_all_games(**filters):
        return_as_model = False

        if filters.get('return_as_model', None) is not None:
            return_as_model = filters.get('return_as_model', False)
            del filters['return_as_model']

        return GameModel.query.filter_by(
            filters).all() if return_as_model is False else [
                game_model.as_dict()
                for game_model in GameModel.query.filter_by(filters).all()
            ]

    @staticmethod
    def get_game_by_id(game_id, return_as_model=False):
        game_model = GameModel.query.filter_by(game_id=game_id).first()

        if return_as_model is False:
            return game_model.as_dict() if game_model else None

        else:
            return game_model if game_model else None

    @staticmethod
    def update_game(game_id,
                    date=None,
                    dome=None,
                    away_team_id=None,
                    home_team_id=None,
                    winner_id=None,
                    loser_id=None,
                    winner_score=None,
                    loser_score=None,
                    winning_pitcher_id=None,
                    losing_pitcher_id=None,
                    weather_report_id=None,
                    expectation_id=None,
                    injury_reports_away_team=None,
                    injury_reports_home_team=None,
                    return_as_model=False):
        game_model = GameModel.get_game_by_id(game_id=game_id,
                                              return_as_model=True)

        if game_model:
            if date is not None:
                game_model.date = date

            if dome is not None:
                game_model.dome = dome

            if away_team_id is not None:
                game_model.away_team_id = away_team_id

            if home_team_id is not None:
                game_model.home_team_id = home_team_id

            if winner_id is not None:
                game_model.winner_id = winner_id

            if loser_id is not None:
                game_model.loser_id = loser_id

            if winner_score is not None:
                game_model.winner_score = winner_score

            if loser_score is not None:
                game_model.loser_score = loser_score

            if winning_pitcher_id is not None:
                game_model.winning_pitcher_id = winning_pitcher_id

            if losing_pitcher_id is not None:
                game_model.losing_pitcher_id = losing_pitcher_id

            if weather_report_id is not None:
                game_model.weather_report_id = weather_report_id

            if expectation_id is not None:
                game_model.expectation_id = expectation_id

            if injury_reports_home_team is not None:
                game_model.injury_reports_home_team = injury_reports_home_team

            if injury_reports_away_team is not None:
                game_model.injury_reports_away_team = injury_reports_away_team

            game_model.commit()
            return game_model if return_as_model else game_model.as_dict()

        return None

    @staticmethod
    def create_game(date, dome, away_team_id, home_team_id, winner_id,
                    loser_id, winner_score, loser_score, winning_pitcher_id,
                    losing_pitcher_id, weather_report_id, expectation_id,
                    injury_reports_away_team, injury_reports_home_team):
        game_models = GameModel.get_all_games(
            date=date,
            away_team_id=away_team_id,
            home_team_id=home_team_id,
            winner_score=winner_score,
            loser_score=loser_score,
            winner_id=winner_id,
            loser_id=loser_id,
            winning_pitcher_id=winning_pitcher_id,
            losing_pitcher_id=losing_pitcher_id)

        if game_models:
            return [game_model.as_dict() for game_model in game_models]

        game_id = str(uuid4())
        game_model = GameModel(
            game_id=game_id,
            date=date,
            dome=dome,
            away_team_id=away_team_id,
            home_team_id=home_team_id,
            winner_id=winner_id,
            loser_id=loser_id,
            winner_score=winner_score,
            loser_score=loser_score,
            winning_pitcher_id=winning_pitcher_id,
            losing_pitcher_id=losing_pitcher_id,
            weather_report_id=weather_report_id,
            expectation_id=expectation_id,
            injury_reports_away_team=injury_reports_away_team,
            injury_reports_home_team=injury_reports_home_team)

        mlbdb.add(game_model)
        mlbdb.commit()

        return game_model.as_dict()

    @staticmethod
    def delete_game(game_id):
        game_model = GameModel.get_game_by_id(game_id=game_id,
                                              return_as_model=True)

        if game_model:
            mlbdb.delete(game_model)
            mlbdb.commit()

            return game_model.as_dict()

        return None
Пример #2
0
class WeatherReportModel(mlbdb.Model):
    __tablename__ = 'weather_reports'
    weather_report_id = mlbdb.Column(UUID(as_uuid=True), primary_key=True)
    temperature = mlbdb.Column(mlbdb.String, nullable=False)
    wind = mlbdb.Column(mlbdb.String, nullable=False)
    precipitation = mlbdb.Column(mlbdb.String, nullable=False)
    location = mlbdb.Column(mlbdb.String, nullable=False)
    date = mlbdb.Column(mlbdb.String, nullable=False)

    def as_dict(self):
        return {
            'weather_report_id': self.weather_report_id,
            'temperature': self.temperature,
            'wind': self.wind,
            'precipitation': self.precipitation,
            'location': self.location,
            'date': self.date
        }

    @staticmethod
    def get_all_weather_reports(**filters):
        return_as_model = False

        if filters.get('return_as_model', None) is not None:
            return_as_model = filters.get('return_as_model', False)
            del filters['return_as_model']

        return WeatherReportModel.query.filter_by(
            filters).all() if return_as_model is False else [
                weather_report_model.as_dict() for weather_report_model in
                WeatherReportModel.query.filter_by(filters).all()
            ]

    @staticmethod
    def get_weather_report_by_id(weather_report_id, return_as_model=False):
        weather_report_model = WeatherReportModel.query.filter_by(
            weather_report_id=weather_report_id).first()

        if return_as_model is False:
            return weather_report_model.as_dict(
            ) if weather_report_model else None

        else:
            return weather_report_model if weather_report_model else None

    @staticmethod
    def update_weather_report(weather_report_id,
                              temperature,
                              wind,
                              precipitation,
                              location,
                              date,
                              return_as_model=False):
        weather_report_model = WeatherReportModel.get_weather_report_by_id(
            weather_report_id=weather_report_id,
            temperature=temperature,
            wind=wind,
            precipitation=precipitation,
            location=location,
            return_as_model=True)

        if weather_report_model:
            if temperature is not None:
                weather_report_model.temperature = temperature

            if wind is not None:
                weather_report_model.wind = wind

            if precipitation is not None:
                weather_report_model.precipitation = precipitation

            if location is not None:
                weather_report_model.location = location

            if date is not None:
                weather_report_model.date = date

            weather_report_model.commit()
            return weather_report_model if return_as_model else weather_report_model.as_dict(
            )

        return None

    @staticmethod
    def create_weather_report(temperature, wind, precipitation, location,
                              date):
        weather_report_models = WeatherReportModel.get_all_weather_reports(
            temperature=temperature,
            wind=wind,
            precipitation=precipitation,
            location=location,
            date=date)

        if weather_report_models:
            return [
                weather_report_model.as_dict()
                for weather_report_model in weather_report_models
            ]

        weather_report_id = str(uuid4())
        weather_report_model = WeatherReportModel(
            weather_report_id=weather_report_id,
            temperature=temperature,
            wind=wind,
            precipitation=precipitation,
            location=location,
            date=date)

        mlbdb.add(weather_report_model)
        mlbdb.commit()

        return weather_report_model.as_dict()

    @staticmethod
    def delete_weather_report(weather_report_id):
        weather_report_model = WeatherReportModel.get_weather_report_by_id(
            weather_report_id=weather_report_id, return_as_model=True)

        if weather_report_model:
            mlbdb.delete(weather_report_model)
            mlbdb.commit()

            return weather_report_model.as_dict()

        return None
Пример #3
0
class ExpectationModel(mlbdb.Model):
    __tablename__ = 'expectations'
    expectation_id = mlbdb.Column(UUID(as_uuid=True), primary_key=True)
    away_team_id = mlbdb.Column(UUID(as_uuid=True),
                                mlbdb.ForeignKey('teams.team_id'),
                                nullable=False)
    home_team_id = mlbdb.Column(UUID(as_uuid=True),
                                mlbdb.ForeignKey('teams.team_id'),
                                nullable=False)
    winner_id = mlbdb.Column(UUID(as_uuid=True),
                             mlbdb.ForeignKey('teams.team_id'),
                             nullable=False)
    loser_id = mlbdb.Column(UUID(as_uuid=True),
                            mlbdb.ForeignKey('teams.team_id'),
                            nullable=False)
    winner_score = mlbdb.Column(mlbdb.Integer, nullable=False)
    loser_score = mlbdb.Column(mlbdb.Integer, nullable=False)
    winning_pitcher_id = mlbdb.Column(UUID(as_uuid=True), nullable=False)
    losing_pitcher_id = mlbdb.Column(UUID(as_uuid=True), nullable=False)
    date = mlbdb.Column(mlbdb.String, nullable=False)

    def as_dict(self):
        return {
            'expectation_id': self.expectation_id,
            'away_team_id': self.away_team_id,
            'home_team_id': self.home_team_id,
            'winner_id': self.winner_id,
            'loser_id': self.loser_id,
            'winner_score': self.winner_score,
            'loser_score': self.loser_score,
            'winning_pitcher_id': self.winning_pitcher_id,
            'losing_pitcher_id': self.losing_pitcher_id,
            'date': self.date
        }

    @staticmethod
    def get_all_expectations(**filters):
        return_as_model = False

        if filters.get('return_as_model', None) is not None:
            return_as_model = filters.get('return_as_model', False)
            del filters['return_as_model']

        return ExpectationModel.query.filter_by(
            filters).all() if return_as_model is False else [
                expectation_model.as_dict() for expectation_model in
                ExpectationModel.query.filter_by(filters).all()
            ]

    @staticmethod
    def get_expectation_by_id(expectation_id, return_as_model=False):
        expectation_model = ExpectationModel.query.filter_by(
            expectation_id=expectation_id).first()

        if return_as_model is False:
            return expectation_model.as_dict() if expectation_model else None

        else:
            return expectation_model if expectation_model else None

    @staticmethod
    def update_expectation(expectation_id,
                           away_team_id,
                           home_team_id,
                           winner_id,
                           loser_id,
                           winner_score,
                           loser_score,
                           winning_pitcher_id,
                           losing_pitcher_id,
                           date,
                           return_as_model=False):
        expectation_model = ExpectationModel.get_expectation_by_id(
            expectation_id=expectation_id, return_as_model=True)

        if expectation_model:
            if away_team_id is not None:
                expectation_model.away_team_id = away_team_id

            if home_team_id is not None:
                expectation_model.home_team_id = home_team_id

            if winner_id is not None:
                expectation_model.winner_id = winner_id

            if loser_id is not None:
                expectation_model.loser_id = loser_id

            if winner_score is not None:
                expectation_model.winner_score = winner_score

            if loser_score is not None:
                expectation_model.loser_score = loser_score

            if winning_pitcher_id is not None:
                expectation_model.winning_pitcher_id = winning_pitcher_id

            if losing_pitcher_id is not None:
                expectation_model.losing_pitcher_id = losing_pitcher_id

            if date is not None:
                expectation_model.date = date

            expectation_model.commit()
            return expectation_model if return_as_model else expectation_model.as_dict(
            )

        return None

    @staticmethod
    def create_expectation(away_team_id, home_team_id, winner_id, loser_id,
                           winner_score, loser_score, winning_pitcher_id,
                           losing_pitcher_id, date):
        expectation_models = ExpectationModel.get_all_expectations(
            away_team_id=away_team_id,
            home_team_id=home_team_id,
            winner_id=winner_id,
            loser_id=loser_id,
            winner_score=winner_score,
            loser_score=loser_score,
            winning_pitcher_id=winning_pitcher_id,
            losing_pitcher_id=losing_pitcher_id,
            date=date)

        if expectation_models:
            return [
                expectation_model.as_dict()
                for expectation_model in expectation_models
            ]

        expectation_id = str(uuid4())
        expectation_model = ExpectationModel(
            expectation_id=expectation_id,
            away_team_id=away_team_id,
            home_team_id=home_team_id,
            winner_id=winner_id,
            loser_id=loser_id,
            winner_score=winner_score,
            loser_score=loser_score,
            winning_pitcher_id=winning_pitcher_id,
            losing_pitcher_id=losing_pitcher_id,
            date=date)

        mlbdb.add(expectation_model)
        mlbdb.commit()

        return expectation_model.as_dict()

    @staticmethod
    def delete_expectation(expectation_id):
        expectation_model = ExpectationModel.get_expectation_by_id(
            expectation_id=expectation_id, return_as_model=True)

        if expectation_model:
            mlbdb.delete(expectation_model)
            mlbdb.commit()

            return expectation_model.as_dict()

        return None
Пример #4
0
class TeamModel(mlbdb.Model):
    __tablename__ = 'teams'
    team_id = mlbdb.Column(UUID(as_uuid=True), primary_key=True)
    season_id = mlbdb.Column(UUID(as_uuid=True),
                             mlbdb.ForeignKey('seasons.season_id'))
    team_name = mlbdb.Column(mlbdb.String, unique=False, nullable=False)
    team_city = mlbdb.Column(mlbdb.String, unique=False, nullable=False)
    conference_id = mlbdb.Column(UUID(as_uuid=True),
                                 mlbdb.ForeignKey('conferences.conference_id'))
    division_id = mlbdb.Column(UUID(as_uuid=True),
                               mlbdb.ForeignKey('divisions.division_id'))

    def as_dict(self):
        return {
            'team_id': self.team_id,
            'team_name': self.team_name,
            'team_city': self.team_city,
            'season_id': self.season_id,
            'conference_id': self.conference_id,
            'division_id': self.division_id
        }

    @staticmethod
    def get_all_teams(**filters):
        return_as_model = False

        if filters.get('return_as_model', None) is not None:
            return_as_model = filters.get('return_as_model', False)
            del filters['return_as_model']

        return TeamModel.query.filter_by(
            filters).all() if return_as_model is False else [
                team_model.as_dict()
                for team_model in TeamModel.query.filter_by(filters).all()
            ]

    @staticmethod
    def get_team_by_id(team_id, return_as_model=False):
        team_model = TeamModel.query.filter_by(team_id=team_id).first()

        if return_as_model is False:
            return team_model.as_dict() if team_model else None

        else:
            return team_model if team_model else None

    @staticmethod
    def update_team(team_id,
                    team_name=None,
                    team_city=None,
                    conference_id=None,
                    division_id=None,
                    season_id=None,
                    return_as_model=False):
        team_model = TeamModel.get_team_by_id(team_id=team_id,
                                              return_as_model=True)

        if team_model:
            if team_name is not None:
                team_model.team_name = team_name

            if team_city is not None:
                team_model.team_city = team_city

            if conference_id is not None:
                team_model.conference_id = conference_id

            if division_id is not None:
                team_model.division_id = division_id

            if season_id is not None:
                team_model.season_id = season_id

            team_model.commit()
            return team_model if return_as_model else team_model.as_dict()

        return None

    @staticmethod
    def create_team(team_name, team_city, conference_id, division_id,
                    season_id):
        team_models = TeamModel.get_all_teams(team_name=team_name,
                                              team_city=team_city,
                                              season_id=season_id)

        if team_models:
            return [team_model.as_dict() for team_model in team_models]

        team_id = str(uuid4())
        team_model = TeamModel(team_id=team_id,
                               team_name=team_name,
                               team_city=team_city,
                               conference_id=conference_id,
                               division_id=division_id,
                               season_id=season_id)

        mlbdb.add(team_model)
        mlbdb.commit()

        return team_model.as_dict()

    @staticmethod
    def delete_team(team_id):
        team_model = TeamModel.get_team_by_id(team_id=team_id,
                                              return_as_model=True)

        if team_model:
            mlbdb.delete(team_model)
            mlbdb.commit()

            return team_model.as_dict()

        return None
Пример #5
0
class InjuryReportModel(mlbdb.Model):
    __tablename__ = 'injury_reports'
    injury_report_id = mlbdb.Column(UUID(as_uuid=True), primary_key=True)
    players = mlbdb.Column(JSON, nullable=True)
    date = mlbdb.Column(mlbdb.String, nullable=False)

    def as_dict(self):
        return {
            'injury_report_id': self.injury_report_id,
            'players': self.players,
            'date': self.date
        }

    @staticmethod
    def get_all_injury_reports(**filters):
        return_as_model = False

        if filters.get('return_as_model', None) is not None:
            return_as_model = filters.get('return_as_model', False)
            del filters['return_as_model']

        return InjuryReportModel.query.filter_by(
            filters).all() if return_as_model is False else [
                injury_report_model.as_dict() for injury_report_model in
                InjuryReportModel.query.filter_by(filters).all()
            ]

    @staticmethod
    def get_injury_report_by_id(injury_report_id, return_as_model=False):
        injury_report_model = InjuryReportModel.query.filter_by(
            injury_report_id=injury_report_id).first()

        if return_as_model is False:
            return injury_report_model.as_dict(
            ) if injury_report_model else None

        else:
            return injury_report_model if injury_report_model else None

    @staticmethod
    def update_injury_report(injury_report_id,
                             players,
                             date,
                             return_as_model=False):
        injury_report_model = InjuryReportModel.get_injury_report_by_id(
            injury_report_id=injury_report_id, return_as_model=True)

        if injury_report_model:
            if players is not None:
                injury_report_model.players = players

            if date is not None:
                injury_report_model.date = date

            injury_report_model.commit()
            return injury_report_model if return_as_model else injury_report_model.as_dict(
            )

        return None

    @staticmethod
    def create_injury_report(players, date):
        injury_report_models = InjuryReportModel.get_all_injury_reports(
            players=players, date=date)

        if injury_report_models:
            return [
                injury_report_model.as_dict()
                for injury_report_model in injury_report_models
            ]

        injury_report_id = str(uuid4())
        injury_report_model = InjuryReportModel(
            injury_report_id=injury_report_id, players=players, date=date)

        mlbdb.add(injury_report_model)
        mlbdb.commit()

        return injury_report_model.as_dict()

    @staticmethod
    def delete_injury_report(injury_report_id):
        injury_report_model = InjuryReportModel.get_injury_report_by_id(
            injury_report_id=injury_report_id, return_as_model=True)

        if injury_report_model:
            mlbdb.delete(injury_report_model)
            mlbdb.commit()

            return injury_report_model.as_dict()

        return None
Пример #6
0
class ScheduleModel(mlbdb.Model):
    __tablename__ = 'schedules'
    schedule_id = mlbdb.Column(UUID(as_uuid=True), primary_key=True)
    away_team_id = mlbdb.Column(UUID(as_uuid=True), mlbdb.ForeignKey('teams.team_id'), nullable=False)
    home_team_id = mlbdb.Column(UUID(as_uuid=True), mlbdb.ForeignKey('teams.team_id'), nullable=False)
    game_start_time = mlbdb.Column(mlbdb.String, nullable=True)
    date = mlbdb.Column(mlbdb.String, nullable=False)

    def as_dict(self):
        return {
            'schedule_id': self.schedule_id,
            'away_team_id': self.away_team_id,
            'home_team_id': self.home_team_id,
            'game_start_time': self.game_start_time,
            'date': self.date
        }

    @staticmethod
    def get_all_schedules(**filters):
        return_as_model = False

        if filters.get('return_as_model', None) is not None:
            return_as_model = filters.get('return_as_model', False)
            del filters['return_as_model']

        return ScheduleModel.query.filter_by(filters).all() if return_as_model is False else [schedule_model.as_dict() for schedule_model in ScheduleModel.query.filter_by(filters).all()]

    @staticmethod
    def get_schedule_by_id(schedule_id, return_as_model=False):
        schedule_model = ScheduleModel.query.filter_by(schedule_id=schedule_id).first()

        if return_as_model is False:
            return schedule_model.as_dict() if schedule_model else None

        else:
            return schedule_model if schedule_model else None

    @staticmethod
    def update_schedule(schedule_id, away_team_id, home_team_id, game_start_time, date, return_as_model=False):
        schedule_model = ScheduleModel.get_schedule_by_id(schedule_id=schedule_id,
                                                          return_as_model=True)

        if schedule_model:
            if away_team_id is not None:
                schedule_model.away_team_id = away_team_id

            if home_team_id is not None:
                schedule_model.home_team_id = home_team_id

            if game_start_time is not None:
                schedule_model.game_start_time = game_start_time

            if date is not None:
                schedule_model.date = date

            schedule_model.commit()
            return schedule_model if return_as_model else schedule_model.as_dict()

        return None

    @staticmethod
    def create_schedule(away_team_id, home_team_id, game_start_time, date):
        schedule_models = ScheduleModel.get_all_schedules(away_team_id=away_team_id,
                                                          home_team_id=home_team_id,
                                                          game_start_time=game_start_time,
                                                          date=date)

        if schedule_models:
            return [schedule_model.as_dict() for schedule_model in schedule_models]

        schedule_id = str(uuid4())
        schedule_model = ScheduleModel(schedule_id=schedule_id,
                                       away_team_id=away_team_id,
                                       home_team_id=home_team_id,
                                       game_start_time=game_start_time,
                                       date=date)

        mlbdb.add(schedule_model)
        mlbdb.commit()

        return schedule_model.as_dict()

    @staticmethod
    def delete_schedule(schedule_id):
        schedule_model = ScheduleModel.get_schedule_by_id(schedule_id=schedule_id, return_as_model=True)

        if schedule_model:
            mlbdb.delete(schedule_model)
            mlbdb.commit()

            return schedule_model.as_dict()

        return None
Пример #7
0
class PlayerModel(mlbdb.Model):
    __tablename__ = 'players'
    player_id = mlbdb.Column(UUID(as_uuid=True), primary_key=True)
    team_id = mlbdb.Column(UUID(as_uuid=True),
                           mlbdb.ForeignKey('teams.team_id'))
    player_name = mlbdb.Column(mlbdb.String, unique=False, nullable=False)
    bats = mlbdb.Column(mlbdb.String(1), unique=False, nullable=False)
    throws = mlbdb.Column(mlbdb.String(1), unique=False, nullable=False)
    player_position = mlbdb.Column(mlbdb.String(5),
                                   unique=False,
                                   nullable=False)
    starter = mlbdb.Column(mlbdb.Boolean,
                           unique=False,
                           nullable=True,
                           default=False)

    def as_dict(self):
        return {
            'player_id': self.player_id,
            'player_name': self.player_name,
            'bats': self.bats,
            'throws': self.throws,
            'player_position': self.player_position,
            'team_id': self.team_id,
            'starter': self.starter
        }

    @staticmethod
    def get_all_players(**filters):
        return_as_model = False

        if filters.get('return_as_model', None) is not None:
            return_as_model = filters.get('return_as_model', False)
            del filters['return_as_model']

        return PlayerModel.query.filter_by(
            filters).all() if return_as_model is False else [
                player_model.as_dict()
                for player_model in PlayerModel.query.filter_by(filters).all()
            ]

    @staticmethod
    def get_player_by_id(player_id, return_as_model=False):
        player_model = PlayerModel.query.filter_by(player_id=player_id).first()

        if return_as_model is False:
            return player_model.as_dict() if player_model else None

        else:
            return player_model if player_model else None

    @staticmethod
    def update_player(player_id,
                      player_name=None,
                      bats=None,
                      throws=None,
                      position=None,
                      team_id=None,
                      starter=None,
                      return_as_model=False):
        player_model = PlayerModel.get_player_by_id(player_id=player_id,
                                                    return_as_model=True)

        if player_model:
            if player_name is not None:
                player_model.player_name = player_name

            if bats is not None:
                player_model.bats = bats

            if throws is not None:
                player_model.throws = throws

            if position is not None:
                player_model.position = position

            if team_id is not None:
                player_model.team_id = team_id

            if starter is not None:
                player_model.starter = starter

            player_model.commit()
            return player_model if return_as_model else player_model.as_dict()

        return None

    @staticmethod
    def create_player(player_name, bats, throws, player_position, starter,
                      team_id):
        player_models = PlayerModel.get_all_players(
            player_name=player_name,
            player_positon=player_position,
            team_id=team_id)

        if player_models:
            return [player_model.as_dict() for player_model in player_models]

        player_id = str(uuid4())
        player_model = PlayerModel(player_id=player_id,
                                   player_name=player_name,
                                   bats=bats,
                                   throws=throws,
                                   starter=starter,
                                   team_id=team_id)

        mlbdb.add(player_model)
        mlbdb.commit()

        return player_model.as_dict()

    @staticmethod
    def delete_player(player_id):
        player_model = PlayerModel.get_player_by_id(player_id=player_id,
                                                    return_as_model=True)

        if player_model:
            mlbdb.delete(player_model)
            mlbdb.commit()

            return player_model.as_dict()

        return None
Пример #8
0
class SeasonModel(mlbdb.Model):
    __tablename__ = 'seasons'
    season_id = mlbdb.Column(UUID(as_uuid=True), primary_key=True)
    season_start_date = mlbdb.Column(mlbdb.String,
                                     unique=False,
                                     nullable=False)
    season_end_date = mlbdb.Column(mlbdb.String, unique=False, nullable=False)
    champion_id = mlbdb.Column(UUID(as_uuid=True),
                               mlbdb.ForeignKey('teams.team_id'))
    runnerup_id = mlbdb.Column(UUID(as_uuid=True),
                               mlbdb.ForeignKey('teams.team_id'))

    def as_dict(self):
        return {
            'season_id': self.season_id,
            'season_start_date': self.season_start_date,
            'season_end_date': self.season_end_date,
            'champion_id': self.champion_id,
            'runnerup_id': self.runnerup_id
        }

    @staticmethod
    def get_all_seasons(**filters):
        return_as_model = False

        if filters.get('return_as_model', None) is not None:
            return_as_model = filters.get('return_as_model', False)
            del filters['return_as_model']

        return SeasonModel.query.filter_by(
            filters).all() if return_as_model is False else [
                season_model.as_dict()
                for season_model in SeasonModel.query.filter_by(filters).all()
            ]

    @staticmethod
    def get_season_by_id(season_id, return_as_model=False):
        season_model = SeasonModel.query.filter_by(season_id=season_id).first()

        if return_as_model is False:
            return season_model.as_dict() if season_model else None

        else:
            return season_model if season_model else None

    @staticmethod
    def update_season(season_id,
                      season_start_date=None,
                      season_end_date=None,
                      champion_id=None,
                      runnerup_id=None,
                      return_as_model=False):
        season_model = SeasonModel.get_season_by_id(season_id=season_id,
                                                    return_as_model=True)

        if season_model:
            if season_start_date is not None:
                season_model.season_start_date = season_start_date

            if season_end_date is not None:
                season_model.season_end_date = season_end_date

            if champion_id is not None:
                season_model.champion_id = champion_id

            if runnerup_id is not None:
                season_model.runnerup_id = runnerup_id

            if season_id is not None:
                season_model.season_id = season_id

            season_model.commit()
            return season_model if return_as_model else season_model.as_dict()

        return None

    @staticmethod
    def create_season(season_start_date, season_end_date, champion_id,
                      runnerup_id):
        season_models = SeasonModel.get_all_seasons(
            season_start_date=season_start_date,
            season_end_date=season_end_date)

        if season_models:
            return [season_model.as_dict() for season_model in season_models]

        season_id = str(uuid4())
        season_model = SeasonModel(season_id=season_id,
                                   season_start_date=season_start_date,
                                   season_end_date=season_end_date,
                                   champion_id=champion_id,
                                   runnerup_id=runnerup_id)

        mlbdb.add(season_model)
        mlbdb.commit()

        return season_model.as_dict()

    @staticmethod
    def delete_season(season_id):
        season_model = SeasonModel.get_season_by_id(season_id=season_id,
                                                    return_as_model=True)

        if season_model:
            mlbdb.delete(season_model)
            mlbdb.commit()

            return season_model.as_dict()

        return None