Commit inicial
This commit is contained in:
7
app/__init__.py
Normal file
7
app/__init__.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from flask import Flask
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
app.debug = True
|
||||
|
||||
from .routes import routes
|
55
app/database.py
Normal file
55
app/database.py
Normal file
@@ -0,0 +1,55 @@
|
||||
from .trainer import Trainer
|
||||
import sqlite3
|
||||
|
||||
class Database:
|
||||
def __init__(self, db_file):
|
||||
self.db_file = db_file
|
||||
|
||||
def create_trainers_table(self):
|
||||
con = sqlite3.connect(self.db_file)
|
||||
|
||||
con.execute("""
|
||||
CREATE TABLE IF NOT EXISTS Trainers
|
||||
(
|
||||
id INT UNSIGNED UNIQUE,
|
||||
nickname TINYTEXT PRIMARY KEY,
|
||||
first_name TINYTEXT,
|
||||
last_name TINYTEXT,
|
||||
email TINYTEXT,
|
||||
password TINYTEXT,
|
||||
team TINYTEXT,
|
||||
pokemons_owned INT UNSIGNED
|
||||
)
|
||||
""")
|
||||
|
||||
con.commit()
|
||||
con.close()
|
||||
|
||||
def __get_trainers(self, sql):
|
||||
con = sqlite3.connect(self.db_file)
|
||||
trainers = []
|
||||
for row in con.execute(*sql):
|
||||
trainers.append(Trainer(*row).__dict__)
|
||||
con.close()
|
||||
return trainers
|
||||
|
||||
def get_trainer_by_nickname(self, nickname, limit, offset):
|
||||
return self.__get_trainers(("SELECT * FROM Trainers WHERE nickname = ? ? OFFSET ?", (nickname, limit, offset)))
|
||||
|
||||
def get_trainers_by_nickname_contains(self, contains, limit, offset):
|
||||
return self.__get_trainers(("SELECT * FROM Trainers WHERE nickname LIKE ? ? OFFSET", ("%" + contains + "%", limit, offset)))
|
||||
|
||||
def insert_trainer(self, trainer):
|
||||
con = sqlite3.connect(self.db_file)
|
||||
|
||||
con.execute("INSERT INTO Trainers VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
(trainer.id, trainer.nickname,
|
||||
trainer.first_name, trainer.last_name,
|
||||
trainer.email, trainer.password,
|
||||
trainer.team, trainer.pokemons_owned)
|
||||
)
|
||||
|
||||
con.commit()
|
||||
con.close()
|
||||
|
||||
db = Database("database.db")
|
0
app/routes/__init__.py
Normal file
0
app/routes/__init__.py
Normal file
41
app/routes/routes.py
Normal file
41
app/routes/routes.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from flask import request, jsonify
|
||||
from app import app
|
||||
from app.database import db
|
||||
|
||||
@app.route('/trainer', methods=['GET'])
|
||||
def route_get_trainers():
|
||||
args = request.args
|
||||
|
||||
try:
|
||||
limit = int(args.get("limit", 0))
|
||||
offset = int(args.get("offset", 0))
|
||||
except ValueError:
|
||||
return {
|
||||
"code": 1,
|
||||
"type": "Integer parsing error",
|
||||
"message": "Couldn't parse parameter as integer"
|
||||
}, 500
|
||||
|
||||
if limit < 0 or offset < 0:
|
||||
return {
|
||||
"code": 2,
|
||||
"type": "Integer parsing error",
|
||||
"message": "Expected positive integer as parameter"
|
||||
}, 500
|
||||
|
||||
limit_str = "LIMIT %d" % limit if "limit" in args else "" # gambiarra pra tornar o limite infinito por padrão
|
||||
|
||||
nickname = args.get("nickname", "")
|
||||
nickname_contains = args.get("nickname_contains", "")
|
||||
|
||||
if nickname and nickname_contains:
|
||||
return {
|
||||
"code": 3,
|
||||
"type": "Invalid parameter",
|
||||
"message": "Parameters \"nickname\" and \"nickname_contains\" are mutually exclusive"
|
||||
}, 500
|
||||
|
||||
if nickname:
|
||||
return jsonify(db.get_trainer_by_nickname(nickname, limit_str, offset))
|
||||
else:
|
||||
return jsonify(db.get_trainers_by_nickname_contains(nickname_contains, limit_str, offset))
|
18
app/trainer.py
Normal file
18
app/trainer.py
Normal file
@@ -0,0 +1,18 @@
|
||||
class Trainer:
|
||||
def __init__(self,
|
||||
id,
|
||||
nickname,
|
||||
first_name,
|
||||
last_name,
|
||||
email,
|
||||
password,
|
||||
team,
|
||||
pokemons_owned):
|
||||
self.id = id
|
||||
self.nickname = nickname
|
||||
self.first_name = first_name
|
||||
self.last_name = last_name
|
||||
self.email = email
|
||||
self.password = password
|
||||
self.team = team
|
||||
self.pokemons_owned = pokemons_owned
|
Reference in New Issue
Block a user