def put(self, league_id): """ PUT request for league Route: Route['league']/<league_id: int> Parameters : league_name: The league's name (string) Returns: if found and successful status: 200 mimetype: application/json data: None if found but not successful status: IFSC mimetype: application/json data: None otherwise status: 404 mimetype: application/json data: None """ # update a single user args = parser.parse_args() league = League.query.get(league_id) league_name = None if league is None: raise LeagueDoesNotExist(payload={'details': league_id}) if args['league_name']: league_name = args['league_name'] league.update(league_name) DB.session.commit() response = Response(dumps(None), status=200, mimetype="application/json") handle_table_change(Tables.LEAGUE, item=league.json()) return response
def delete(self, espy_id): """ DELETE request for Espy Route: Routes['espy']/<espy_id:int> Returns: if found status: 200 mimetype: application/json data: None otherwise status: 404 mimetype: application/json data: None """ espy = Espys.query.get(espy_id) if espy is None: raise EspysDoesNotExist(payload={'details': espy_id}) # delete a single espy DB.session.delete(espy) DB.session.commit() response = Response(dumps(None), status=200, mimetype="application/json") handle_table_change(Tables.ESPYS, item=espy.json()) return response
def post(self): """ POST request for League List Route: Route['league'] Parameters : league_name: The league's name (string) Returns: if successful status: 200 mimetype: application/json data: the created user league id (int) if missing required parameter status: 400 mimetype: application/json data: the created user league id (int) if invalid parameter status: IFSC mimetype: application/json data: the created user league id (int) """ # create a new user args = post_parser.parse_args() league_name = None if args['league_name']: league_name = args['league_name'] league = League(league_name) DB.session.add(league) DB.session.commit() result = league.id handle_table_change(Tables.LEAGUE, item=league.json()) return Response(dumps(result), status=201, mimetype="application/json")
def delete(self, team_id): """ DELETE request for Team Route: Routes['team']/<team_id:int> Returns: if found status: 200 mimetype: application/json data: None otherwise status: 404 mimetype: application/json data: None """ team = Team.query.get(team_id) if team is None: raise TeamDoesNotExist(payload={'details': team_id}) # delete a single team team_json = team.json() DB.session.delete(team) DB.session.commit() response = Response(dumps(None), status=200, mimetype="application/json") handle_table_change(Tables.TEAM, item=team_json) return response
def delete(self, player_id): """ DELETE request for Player Route: Routes['player']/<player_id: int> Returns: if found status: 200 mimetype: application/json data: None otherwise status: 404 mimetype: application/json data: None """ player = Player.query.get(player_id) if player is None: raise PlayerDoesNotExist(payload={'details': player_id}) oauths = OAuth.query.filter(OAuth.player_id == player_id).all() for oauth in oauths: DB.session.delete(oauth) query = TeamRequest.query.filter(TeamRequest.email == player.email) for team_request in query.all(): DB.session.delete(team_request) # delete a single user DB.session.delete(player) DB.session.commit() response = Response(dumps(None), status=200, mimetype="application/json") handle_table_change(Tables.PLAYER, item=player.json()) return response
def delete(self, league_id): """ DELETE request for League Route: Route['league']/<league_id: int> Returns: if found status: 200 mimetype: application/json data: None otherwise status: 404 mimetype: application/json data: None """ # delete a single user league = League.query.get(league_id) if league is None: raise LeagueDoesNotExist(payload={'details': league_id}) DB.session.delete(league) DB.session.commit() response = Response(dumps(None), status=200, mimetype="application/json") handle_table_change(Tables.LEAGUE, item=league.json()) return response
def post(self): """ POST request for Sponsor List Route: Routes['fun'] Parameters : year: The Sponsor's name (string) count: A link to sponsors website (string) description: a description of the sponsor (string) Returns: if successful status: 200 mimetype: application/json sponsor_id: the create sponsor_id else status: 409 mimetype: application/json data: the create sponsor_id (int) """ # create a new user args = post_parser.parse_args() count = None year = None if args['count']: count = args['count'] if args['year']: year = args['year'] fun = Fun(year=year, count=count) DB.session.add(fun) DB.session.commit() fun_id = fun.year handle_table_change(Tables.FUN, item=fun.json()) return Response(dumps(fun_id), status=201, mimetype="application/json")
def submit_bats(bats: List[Bat]) -> bool: """Submits a list of bats""" for bat in bats: DB.session.add(bat) DB.session.commit() # good to add the submission handle_table_change(Tables.GAME) return True
def put(self, espy_id): """ PUT request for espy Route: Routes['team']/<espy_id:int> Parameters : espy_id: The espy's id (int) description: The description of the transaction (string) sponsor_id: The sponsor's id (int) team_id: The team's id (int) points: The points awarded (int) receipt: the receipt number (string) Returns: if found and updated successfully status: 200 mimetype: application/json data: None otherwise possible errors are status: 404, IFSC, TDNESC, SDNESC mimetype: application/json data: None """ # update a single user espy = Espys.query.get(espy_id) args = parser.parse_args() description = None sponsor_id = None team_id = None points = None receipt = None date = None time = None if espy is None: raise EspysDoesNotExist(payload={'details': espy_id}) if args['description']: description = args['description'] if args['sponsor_id']: sponsor_id = args['sponsor_id'] if args['team_id']: team_id = args['team_id'] if args['points']: points = args['points'] if args['receipt']: receipt = args['receipt'] if args['date'] and args['time']: date = args['date'] time = args['time'] espy.update(sponsor_id=sponsor_id, team_id=team_id, description=description, points=points, receipt=receipt, date=date, time=time) DB.session.commit() response = Response(dumps(None), status=200, mimetype="application/json") handle_table_change(Tables.ESPYS, item=espy.json()) return response
def post(self): """ POST request for Espys List Route: Routes['team'] Parameters : league_id: the league's id (int) sponsor_id: the sponsor's id (int) color: the color of the team (string) year: the year the team is playing in (int) espys: the team espys points (int) Returns: if successful status: 200 mimetype: application/json data: the create team id (int) possible errors status: 400, IFSC, LDNESC, PDNESC or SDNESC mimetype: application/json data: the create team id (int) """ # create a new user args = post_parser.parse_args() description = None sponsor_id = None team_id = None points = None receipt = None date = None time = None if args['description']: description = args['description'] if args['sponsor_id']: sponsor_id = args['sponsor_id'] if args['team_id']: team_id = args['team_id'] if args['points']: points = args['points'] if args['receipt']: points = args['receipt'] if args['date'] and args['time']: date = args['date'] time = args['time'] espy = Espys(sponsor_id=sponsor_id, team_id=team_id, description=description, points=points, receipt=receipt, date=date, time=time) DB.session.add(espy) DB.session.commit() result = espy.id handle_table_change(Tables.ESPYS, item=espy.json()) return Response(dumps(result), status=201, mimetype="application/json")
def put(self, bat_id): """ PUT request for Bat Route: Routes['bat']/<bat_id: int> Parameters : game_id: the id of the game (int) player_id: the id of the batter (int) rbi: the number of runs batted in (int) hit: the type of hit (string) inning: the inning the hit occurred (int) team_id: the id of the team (int) Returns: status: 200 mimetype: application/json data: success: tells if request was successful (boolean) message: the status message (string) failures: a list of parameters that failed to update (list of string) """ # update a single bat args = parser.parse_args() bat = Bat.query.get(bat_id) player_id = None team_id = None game_id = None rbi = None hit = None inning = None if bat is None: raise BatDoesNotExist(payload={'details': bat_id}) if args['team_id']: team_id = args['team_id'] if args['game_id']: game_id = args['game_id'] if args['player_id']: player_id = args['player_id'] if args['rbi']: rbi = args['rbi'] if args['hit']: hit = args['hit'] if args['inning']: inning = args['inning'] bat.update(player_id=player_id, team_id=team_id, game_id=game_id, rbi=rbi, hit=hit, inning=inning) DB.session.commit() response = Response(dumps(None), status=200, mimetype="application/json") handle_table_change(Tables.BAT, item=bat.json()) return response
def put(self, player_id): """ PUT request for Player Route: Routes['player']/<player_id: int> Parameters : player_name: The player's name (string) gender: a one letter character representing gender (string) email: the players email (string) active: 1 if player is active and 0 otherwise (int) Returns: if found and successful status: 200 mimetype: application/json data: None if found but new email is duplicate status: NUESC mimetype: application/json data: None if found but invalid field status: IFSC mimetype: application/json data: None othwerwise status: 404 mimetype: application/json data: None """ # update a single user player = DB.session.query(Player).get(player_id) args = parser.parse_args() if player is None: raise PlayerDoesNotExist(payload={'details': player_id}) player_name = None gender = None email = None active = True if args['player_name']: player_name = args['player_name'] if args['gender']: gender = args['gender'] if args['email']: email = args['email'] if args['active']: active = args['active'] == 1 if True else False player.update(name=player_name, gender=gender, email=email, active=active) DB.session.commit() response = Response(dumps(None), status=200, mimetype="application/json") handle_table_change(Tables.PLAYER, item=player.json()) return response
def post(self): """ POST request for Bats List Route: Routes['bat'] Parameters : game_id: the id of the game (int) player_id: the id of the batter (int) rbi: the number of runs batted in (int) hit: the type of hit (string) inning: the inning the hit occurred (int) team_id: the id of the team (int) Returns: if successful status: 200 mimetype: application/json data: the created bat id (int) otherwise possible errors status: 400, GDNESC, PDNESC, TDNESC mimetype: application/json data: None """ # create a new bat args = post_parser.parse_args() game_id = None player_id = None team_id = None rbi = 0 hit = None inning = 1 # just assume some first inning if args['game_id']: game_id = args['game_id'] if args['player_id']: player_id = args['player_id'] if args['team_id']: team_id = args['team_id'] if args['hit']: hit = args['hit'] if args['rbi']: rbi = args['rbi'] if args['inning']: inning = args['inning'] bat = Bat(player_id, team_id, game_id, hit, inning=inning, rbi=rbi) DB.session.add(bat) DB.session.commit() bat_id = bat.id resp = Response(dumps(bat_id), status=201, mimetype="application/json") handle_table_change(Tables.BAT, item=bat.json()) return resp
def submit_score(game_id: int, captain_id: int, score: int, homeruns: List[int], ss: List[int]) -> bool: """Captain submit a score""" unassigned_player = Player.query.filter_by(email=UNASSIGNED_EMAIL).first() unassigned_id = UNASSIGNED if unassigned_player is not None: unassigned_id = unassigned_player.id game = Game.query.get(game_id) captain = Player.query.get(captain_id) if captain is None: raise PlayerNotSubscribed(payload={'details': captain_id}) if game is None: raise GameDoesNotExist(payload={'details': game_id}) # find the team away_team = Team.query.get(game.away_team_id) home_team = Team.query.get(game.home_team_id) team = None if away_team.player_id == captain.id: team = away_team # captain of the squad elif home_team.player_id == captain.id: team = home_team # captain of the away squad else: # not a captain of a team raise NotTeamCaptain(payload={'details': captain_id}) if score <= 0: # hmm that is so sad DB.session.add( Bat(unassigned_id, team.id, game.id, "fo", inning=1, rbi=0)) if homeruns is not None: for player_id in homeruns: # add the homeruns DB.session.add( Bat(player_id, team.id, game.id, "hr", inning=1, rbi=1)) score -= 1 if ss is not None: for player_id in ss: # add the special singles try: bat = Bat(player_id, team.id, game.id, "ss", inning=1, rbi=0) DB.session.add(bat) except InvalidField: pass if score < 0: raise InvalidField(payload={'details': "More hr than score"}) while score > 0: bat = Bat(unassigned_id, team.id, game.id, "s", inning=1, rbi=1) DB.session.add(bat) score -= 1 DB.session.commit() # good to add the submission handle_table_change(Tables.GAME) return True
def post(self): """ POST request for Player List Route: Routes['player'] Parameters : player_name: The player's name (string) gender: a one letter character representing gender (string) email: the email of the player (string) password: the password of the player(string) active: 1 if player is active and 0 otherwise (int) Returns: if successful status: 200 mimetype: application/json data: the created player id (int) if email is duplicate status: NUESC mimetype: application/json data: None if invalid field status: IFSC mimetype: application/json data: None othwerwise status: 404 mimetype: application/json data: None """ # create a new user args = post_parser.parse_args() gender = None player_name = None email = None password = "******" active = True if args['player_name']: player_name = args['player_name'] if args['gender']: gender = args['gender'] if args['email']: email = args['email'] if args['password']: password = args['password'] if args['active']: active = args['active'] == 1 if True else False player = Player(player_name, email, gender, password, active=active) DB.session.add(player) DB.session.commit() result = player.id handle_table_change(Tables.PLAYER, item=player.json()) return Response(dumps(result), status=201, mimetype="application/json")
def put(self, sponsor_id): """ PUT request for Sponsor Route: Routes['sponsor']/<sponsor_id:int> Parameters: sponsor_name: The Sponsor's name (string) link: the link to the sponsor (string) description: a description of the sponsor (string) active: 1 if the sponsor is active otherwise 0 (int) Returns: if found and successful status: 200 mimetype: application/json data: None if found but not successful status: 409 mimetype: application/json data: None otherwise status: 404 mimetype: application/json data: None """ # update a single user sponsor = Sponsor.query.get(sponsor_id) link = None description = None name = None active = True if sponsor is None: raise SponsorDoesNotExist(payload={'details': sponsor_id}) args = parser.parse_args() if args['sponsor_name']: name = args['sponsor_name'] if args['link']: link = args['link'] if args['description']: description = args['description'] if args['active']: active = args['active'] == 1 if True else False sponsor.update(name=name, link=link, description=description, active=active) DB.session.commit() response = Response(dumps(None), status=200, mimetype="application/json") handle_table_change(Tables.SPONSOR, item=sponsor.json()) return response
def post(self): """ POST request for submitting a transaction Route: Route['bottransaction'] Parameters: player_id: the id of the player submitting the receipt (int) amount: the amount spent (int) sponsor: the name of the sponsor (str) team_id: the id of the team (int) Returns: status: 200 mimetype: application/json data: True """ args = parser.parse_args() player_id = args['player_id'] team_id = args['team_id'] amount = args['amount'] sponsor_name = args['sponsor'] # ensure the player exists player = Player.query.get(player_id) if player is None: raise PlayerDoesNotExist(payload={'details': player_id}) # ensure the team exist team = Team.query.get(team_id) if team is None: raise TeamDoesNotExist(payload={'details': team_id}) # ensure the sponsor exists sponsor = Sponsor.query.filter_by(name=sponsor_name).first() if sponsor is None: raise SponsorDoesNotExist(payload={'details': sponsor_name}) # player can only submit receipts to their own team if not team.is_player_on_team(player): raise PlayerNotOnTeam(payload={'details': player_id}) # should be good to add the espy now espy = Espys(team_id=team.id, sponsor_id=sponsor.id, points=amount, description="Bot transaction") DB.session.add(espy) DB.session.commit() handle_table_change(Tables.ESPYS) return Response(dumps(espy.id), status=200, mimetype="application/json")
def put(self, team_id): """ PUT request for team Route: Routes['team']/<team_id:int> Parameters : team_id: The team's id (int) team_name: The team's name (string) sponsor_id: The sponsor's id (int) league_id: The league's id (int) color: the color of the team (string) year: the year of the team (int) espys: the total espys points of the team (int) Returns: if found and updated successfully status: 200 mimetype: application/json data: None otherwise possible errors are status: 404, IFSC, LDNESC, PDNESC or SDNESC mimetype: application/json data: None """ # update a single user team = Team.query.get(team_id) args = parser.parse_args() color = None sponsor_id = None league_id = None year = None if team is None: raise TeamDoesNotExist(payload={'details': team_id}) if args['color']: color = args['color'] if args['sponsor_id']: sponsor_id = args['sponsor_id'] if args['league_id']: league_id = args['league_id'] if args['year']: year = args['year'] team.update(color=color, sponsor_id=sponsor_id, league_id=league_id, year=year) DB.session.commit() response = Response(dumps(None), status=200, mimetype="application/json") handle_table_change(Tables.TEAM, item=team.json()) return response
def post(self): """ POST request for Sponsor List Route: Routes['sponsor']/<sponsor_id:int> Parameters: sponsor_name: The Sponsor's name (string) link: A link to sponsors website (string) description: a description of the sponsor (string) active: 1 if the sponsor if active otherwise 0 Returns: if successful status: 200 mimetype: application/json sponsor_id: the create sponsor_id else status: 409 mimetype: application/json data: the create sponsor_id (int) """ # create a new user args = post_parser.parse_args() sponsor_name = None description = None link = None active = True if args['sponsor_name']: sponsor_name = args['sponsor_name'] if args['description']: description = args['description'] if args['link']: link = args['link'] if args['active']: active = args['active'] == 1 if True else False sponsor = Sponsor(sponsor_name, link=link, description=description, active=active) DB.session.add(sponsor) DB.session.commit() sponsor_id = sponsor.id handle_table_change(Tables.SPONSOR, item=sponsor.json()) return Response(dumps(sponsor_id), status=201, mimetype="application/json")
def post(self): """ POST request for Teams List Route: Routes['team'] Parameters : league_id: the league's id (int) sponsor_id: the sponsor's id (int) color: the color of the team (string) year: the year the team is playing in (int) espys: the team espys points (int) Returns: if successful status: 200 mimetype: application/json data: the create team id (int) possible errors status: 400, IFSC, LDNESC, PDNESC or SDNESC mimetype: application/json data: the create team id (int) """ # create a new user args = post_parser.parse_args() color = None sponsor_id = None league_id = None year = date.today().year if args['color']: color = args['color'] if args['sponsor_id']: sponsor_id = args['sponsor_id'] if args['league_id']: league_id = args['league_id'] if args['year']: year = args['year'] t = Team(color=color, sponsor_id=sponsor_id, league_id=league_id, year=year) DB.session.add(t) DB.session.commit() result = t.id handle_table_change(Tables.TEAM, item=t.json()) return Response(dumps(result), status=201, mimetype="application/json")
def post(self): """ POST request for Games List Route: Routes['game'] Parameters : home_team_id: The home team id (int) away_team_id: The away team id (int) date: The date of the game with the format YYYY-MM-DD (string) time: The time of the game in the format HH:MM (string) league_id: The league this game belongs to (int) status: the game status (string) field: the field the game is being played on (string) Returns: if successful status: 200 mimetype: application/json data: the created game id (int) """ # create a new game args = post_parser.parse_args() date = None time = None if args['date'] and args['time']: date = args['date'] time = args['time'] status = args.get('status') if args.get('status') is not None else '' field = args.get('field') if args.get('field') is not None else '' game = Game(date, time, args.get('home_team_id'), args.get('away_team_id'), args.get('league_id'), args.get('division_id'), status=status, field=field) DB.session.add(game) DB.session.commit() result = game.id handle_table_change(Tables.GAME, item=game.json()) return Response(dumps(result), status=201, mimetype="application/json")
def put(self, game_id): """ PUT request for game Route: Routes['game']/<game_id: int> Parameters : home_team_id: The home team id (int) away_team_id: The away team id (int) date: The date of the game with the format YYYY-MM-DD (string) time: The time of the game in the format HH:MM (string) league_id: The league this game belongs to (int), status: the game's status (string) field: the game's field (string) Returns: if found and successful status: 200 mimetype: application/json data: None otherwise possible errors status: 404, IFSC, TDNESC, LDNESC mimetype: application/json data: None """ game = Game.query.get(game_id) args = parser.parse_args() if game is None: raise GameDoesNotExist(payload={'details': game_id}) game.update(date=args.get('date', None), time=args.get('time', None), home_team_id=args.get('home_team_id', None), away_team_id=args.get('away_team_id', None), league_id=args.get('league_id', None), division_id=args.get('division_id', None), status=args.get('status', None), field=args.get('field', None)) DB.session.commit() response = Response(dumps(None), status=200, mimetype="application/json") handle_table_change(Tables.GAME, item=game.json()) return response
def delete(self, bat_id): """ DELETE request for Bat Route: Routes['bat']/<bat_id: int> Returns: status: 200 mimetype: application/json data: success: tells if request was successful (boolean) message: the status message (string) """ bat = Bat.query.get(bat_id) if bat is None: raise BatDoesNotExist(payload={'details': bat_id}) # delete a single bat DB.session.delete(bat) DB.session.commit() response = Response(dumps(None), status=200, mimetype="application/json") handle_table_change(Tables.BAT, item=bat.json()) return response
def delete(self, sponsor_id): """ DELETE request for Sponsor Route: Routes['sponsor']/<sponsor_id:int> Returns: if found status: 200 mimetype: application/json data: None otherwise status: 404 mimetype: application/json data: None """ # delete a single user sponsor = Sponsor.query.get(sponsor_id) if sponsor is None: # Sponsor is not in the table raise SponsorDoesNotExist(payload={'details': sponsor_id}) DB.session.delete(sponsor) DB.session.commit() handle_table_change(Tables.SPONSOR, item=sponsor.json()) return Response(dumps(None), status=200, mimetype="application/json")
def delete(self, game_id): """ DELETE request for Game Route: Routes['game']/<game_id: int> Returns: if found status: 200 mimetype: application/json data: None otherwise status: 404 mimetype: application/json data: None """ game = Game.query.get(game_id) if game is None: raise GameDoesNotExist(payload={'details': game_id}) DB.session.delete(game) DB.session.commit() response = Response(dumps(None), status=200, mimetype="application/json") handle_table_change(Tables.GAME, item=game.json()) return response
def put(self, year): """ PUT request for Sponsor Route: Routes['fun']/<year:int> Parameters : count: The number of fun (int) Returns: if found and successful status: 200 mimetype: application/json data: None if found but not successful status: 409 mimetype: application/json data: None otherwise status: 404 mimetype: application/json data: None """ # update a single user fun_year = Fun.query.filter(Fun.year == year).first() count = None if fun_year is None: raise FunDoesNotExist(payload={'details': year}) args = parser.parse_args() if args['count']: count = args['count'] fun_year.update(count=count) DB.session.commit() response = Response(dumps(None), status=200, mimetype="application/json") handle_table_change(Tables.FUN, item=fun_year.json()) return response
def delete(self, year): """ DELETE request for Fun Route: Routes['fun']/<year:int> Returns: if found status: 200 mimetype: application/json data: None otherwise status: 404 mimetype: application/json data: None """ # delete a single user fun_year = Fun.query.filter(Fun.year == year).first() if fun_year is None: # Sponsor is not in the table raise FunDoesNotExist(payload={'details': year}) fun_json = fun_year.json() DB.session.delete(fun_year) DB.session.commit() handle_table_change(Tables.FUN, item=fun_json) return Response(dumps(None), status=200, mimetype="application/json")