-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
74 lines (54 loc) · 2.12 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from flask import Flask, json, request
import sqlite3
app = Flask(__name__)
def get_db_connection():
conn = sqlite3.connect("database/pokedb")
conn.row_factory = sqlite3.Row # Facilita conversión a JSON
return conn
@app.route('/')
def getHolaMundo():
return json.dumps({"message": 'Hola Mundo Python!'}), 200
# En este caso no se necesita de la declaración del método GET, por defecto Flask lo asume.
@app.route('/getAllPokemon')
def getAllPokemon():
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute("SELECT * FROM pokemon;")
rows = cursor.fetchall()
conn.close()
res = json.dumps([dict(row) for row in rows])
return res
@app.route('/insertPokemon', methods=['POST'])
def insertPokemon():
data = request.get_json() # Obtener los datos del cuerpo de la solicitud
name = data.get('name')
type_primary = data.get('type_primary')
type_secondary = data.get('type_secondary')
if not name or not type_primary:
return json.dumps({"error": "Faltan campos requeridos"}), 400
conn = get_db_connection()
cursor = conn.cursor()
try:
cursor.execute('''INSERT INTO pokemon (name, type_primary, type_secondary)
VALUES (?, ?, ?)''', (name, type_primary, type_secondary))
conn.commit()
conn.close()
return json.dumps({"message": "Pokémon insertado con éxito"}), 201
except sqlite3.Error as e:
conn.close()
return json.dumps({"error": str(e)}), 500
@app.route('/deletePokemon/<int:id>', methods=['DELETE'])
def deletePokemon(id):
conn = get_db_connection()
cursor = conn.cursor()
try:
cursor.execute('DELETE FROM pokemon WHERE id = ?', (id,))
conn.commit()
if cursor.rowcount == 0:
conn.close()
return json.dumps({"error": "Pokémon no encontrado"}), 404
conn.close()
return json.dumps({"message": "Pokémon eliminado con éxito"}), 200
except sqlite3.Error as e:
conn.close()
return json.dumps({"error": str(e)}), 500