Adicionar 2 rotas

This commit is contained in:
Augusto Gunsch
2021-10-18 16:49:47 -03:00
parent b905a812b9
commit 5fc179d389
11 changed files with 230 additions and 36 deletions

0
api/models/__init__.py Normal file
View File

View File

@@ -0,0 +1,24 @@
from api.app import db, ma
from .trainer import *
class PokemonOwned(db.Model):
__tablename__ = "pokemons_owned"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.Text(50), unique=True, index=True, nullable=False)
level = db.Column(db.Integer)
pokemon_id = db.Column(db.Integer)
trainer_id = db.Column(db.Integer, db.ForeignKey("trainers.id"), index=True)
def __init__(self, name, level, pokemon_id, trainer_id):
self.name = name
self.level = level
self.pokemon_id = pokemon_id
self.trainer_id = trainer_id
class PokemonOwnedSchema(ma.Schema):
class Meta:
fields = ('id', 'name', 'level', 'pokemon_data')
pokemon_owned_schema = PokemonOwnedSchema()
pokemon_owned_schemas = PokemonOwnedSchema(many=True)

View File

@@ -14,7 +14,7 @@ class InvalidTeam(Exception):
class Trainer(db.Model):
__tablename__ = "trainers"
id = db.Column(db.Integer, primary_key=True, unique=True, autoincrement=True)
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
nickname = db.Column(db.String(20), unique=True, nullable=False, index=True)
first_name = db.Column(db.String(30), nullable=False)
last_name = db.Column(db.String(30), nullable=False)
@@ -22,6 +22,7 @@ class Trainer(db.Model):
password = db.Column(db.String(200), nullable=False)
team = db.Column(db.String(10), nullable=False)
pokemons_owned = db.Column(db.Integer, default=0)
pokemons_list = db.relationship("PokemonOwned", lazy="dynamic")
def __init__(self, nickname, first_name, last_name, email, password, team):
if team not in teams: