Skip to content

Commit 7c6de09

Browse files
feat: add todo properties and routes. Also add simple middleware implementation
1 parent 22a731b commit 7c6de09

14 files changed

+312
-41
lines changed

Insomnia_2023-04-22.json

-1
This file was deleted.

Insomnia_2023-05-12.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"_type":"export","__export_format":4,"__export_date":"2023-05-12T02:50:24.989Z","__export_source":"insomnia.desktop.app:v2022.7.0","resources":[{"_id":"req_5e55876b9bd6473faeff2baa58a58a69","parentId":"wrk_6c9f65a7b59d4ccc8a6e6b0d9c76c6bd","modified":1683858668871,"created":1682099971398,"url":"http://127.0.0.1:5000/users","name":"Users","description":"","method":"GET","body":{"mimeType":"application/json","text":"{\n\t\"accessToken\": \"10b644a4cfadfef763de9828ab0761356e9219e3fe13a28703a21ff49e504004c7c94e6b0b2928ba\"\n}"},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json"}],"authentication":{},"metaSortKey":-1682099971398,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"wrk_6c9f65a7b59d4ccc8a6e6b0d9c76c6bd","parentId":null,"modified":1682099964770,"created":1682099964770,"name":"User API","description":"","scope":"collection","_type":"workspace"},{"_id":"req_936f1b1a8e1446f6a14bfa036d3e690d","parentId":"wrk_6c9f65a7b59d4ccc8a6e6b0d9c76c6bd","modified":1683858308509,"created":1683852543113,"url":"http://127.0.0.1:5000/todos","name":"Todo","description":"","method":"GET","body":{"mimeType":"application/json","text":"{\n\t\"userId\": 2,\n\t\"accessToken\": \"10b644a4cfadfef763de9828ab0761356e9219e3fe13a28703a21ff49e504004c7c94e6b0b2928ba\"\n}"},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json","id":"pair_b590039578ca49849179401a6eae2135"},{"id":"pair_a675eb4ffe0d48ee8c9287595b026a8a","name":"","value":"","description":""}],"authentication":{},"metaSortKey":-1680869538582,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_ab6d4be905e4420c9dfa670fcd47fa0c","parentId":"wrk_6c9f65a7b59d4ccc8a6e6b0d9c76c6bd","modified":1683859752494,"created":1682100405821,"url":"http://127.0.0.1:5000/user/1","name":"User","description":"","method":"GET","body":{"mimeType":"application/json","text":"{\n\t\"accessToken\": \"10b644a4cfadfef763de9828ab0761356e9219e3fe13a28703a21ff49e504004c7c94e6b0b2928ba\"\n}"},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json"}],"authentication":{},"metaSortKey":-1679639105766,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_bb3c0fb8e0ec425cbff91b966fed94a5","parentId":"wrk_6c9f65a7b59d4ccc8a6e6b0d9c76c6bd","modified":1683857083056,"created":1682100034926,"url":"http://127.0.0.1:5000/users","name":"User","description":"","method":"POST","body":{"mimeType":"application/json","text":"{\n\t\"firstName\": \"Roberto\",\n \"lastName\": \"Medina\",\n \"email\": \"text1@gmail.com\",\n \"password\": \"1234\"\n}"},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json"}],"authentication":{},"metaSortKey":-1677178240134,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_6c48ad94fe1445ca870d7a006c0de8b0","parentId":"wrk_6c9f65a7b59d4ccc8a6e6b0d9c76c6bd","modified":1683857314170,"created":1683853137362,"url":"http://127.0.0.1:5000/todos","name":"Todo","description":"","method":"POST","body":{"mimeType":"application/json","text":"{\n\t\"accessToken\": \"68aa07822574b1a5c3534e2a70c9bbb589c9a074de8fb2c8c3b7d7e6e4ce9272dab4de87ba2130a8\",\n\t\"userId\": 2,\n\t\"title\": \"Ok\",\n\t\"todoDescription\": \"ok ok\"\n}"},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json"}],"authentication":{},"metaSortKey":-1676870631930,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_c938854a45a942f6bc969f6af4b161f1","parentId":"wrk_6c9f65a7b59d4ccc8a6e6b0d9c76c6bd","modified":1683857278112,"created":1682123992625,"url":"http://127.0.0.1:5000/sign-in","name":"Auth User","description":"","method":"POST","body":{"mimeType":"application/json","text":"{\n\t\"email\": \"text1@gmail.com\",\n \"password\": \"12345\"\n}"},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json"}],"authentication":{},"metaSortKey":-1676563023726,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_2a192ba2de2443328fc58474191a2445","parentId":"wrk_6c9f65a7b59d4ccc8a6e6b0d9c76c6bd","modified":1682127608777,"created":1682126337329,"url":"http://127.0.0.1:5000/sign-in","name":"Auth User Access Token","description":"","method":"POST","body":{"mimeType":"application/json","text":"{\n\t\"accessToken\": null\n}"},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json"}],"authentication":{},"metaSortKey":-1676255415522,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_e6bb773ff2c549e7909c43f7da1850a0","parentId":"wrk_6c9f65a7b59d4ccc8a6e6b0d9c76c6bd","modified":1683859783377,"created":1682101328360,"url":"http://127.0.0.1:5000/user/2","name":"User","description":"","method":"PUT","body":{"mimeType":"application/json","text":"{\n\t\"accessToken\": \"10b644a4cfadfef763de9828ab0761356e9219e3fe13a28703a21ff49e504004c7c94e6b0b2928ba\",\n\t\"newPassword\": \"new\",\n\t\"oldPassword\": \"1111\"\n}\n"},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json"}],"authentication":{},"metaSortKey":-1675947807318,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_e03f156f94d64eb79924b88770dafabb","parentId":"wrk_6c9f65a7b59d4ccc8a6e6b0d9c76c6bd","modified":1683856470166,"created":1683856368102,"url":"http://127.0.0.1:5000/todo/2","name":"Todo","description":"","method":"PUT","body":{"mimeType":"application/json","text":"{\n\t\"accessToken\": \"10b644a4cfadfef763de9828ab0761356e9219e3fe13a28703a21ff49e504004c7c94e6b0b2928ba\",\n\t\"title\": \"Ok 2\",\n\t\"todoDescription\": \"ok ok\"\n}"},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json"}],"authentication":{},"metaSortKey":-1675332590910,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_8ce3ce223a7d48c0ba432e27d9673c4e","parentId":"wrk_6c9f65a7b59d4ccc8a6e6b0d9c76c6bd","modified":1683859793001,"created":1682100485719,"url":"http://127.0.0.1:5000/user/1","name":"User","description":"","method":"DELETE","body":{"mimeType":"application/json","text":"{\n\t\"accessToken\": \"10b644a4cfadfef763de9828ab0761356e9219e3fe13a28703a21ff49e504004c7c94e6b0b2928ba\",\n\t\"password\": \"1111\"\n}"},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json"}],"authentication":{},"metaSortKey":-1674717374502,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_73550ab127c04f4ba34b268caac66809","parentId":"wrk_6c9f65a7b59d4ccc8a6e6b0d9c76c6bd","modified":1683855818772,"created":1683855802460,"url":"http://127.0.0.1:5000/todo/1","name":"Todo","description":"","method":"DELETE","body":{"mimeType":"application/json","text":"{\n\t\"accessToken\": \"10b644a4cfadfef763de9828ab0761356e9219e3fe13a28703a21ff49e504004c7c94e6b0b2928ba\"\n}"},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json"}],"authentication":{},"metaSortKey":-1673486941686,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"env_7a99e4c0ee6c99fe4713a4ebe2bc9dbc5f0b810c","parentId":"wrk_6c9f65a7b59d4ccc8a6e6b0d9c76c6bd","modified":1682099964776,"created":1682099964776,"name":"Base Environment","data":{},"dataPropertyOrder":null,"color":null,"isPrivate":false,"metaSortKey":1682099964776,"_type":"environment"},{"_id":"jar_7a99e4c0ee6c99fe4713a4ebe2bc9dbc5f0b810c","parentId":"wrk_6c9f65a7b59d4ccc8a6e6b0d9c76c6bd","modified":1682099964778,"created":1682099964778,"name":"Default Jar","cookies":[],"_type":"cookie_jar"},{"_id":"spc_666bd4d144914c4c890db6f79e25ce23","parentId":"wrk_6c9f65a7b59d4ccc8a6e6b0d9c76c6bd","modified":1682099964771,"created":1682099964771,"fileName":"User API","contents":"","contentType":"yaml","_type":"api_spec"}]}

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
# bank-simulator-api
2-
Flask API for a Bank Simulator App
2+
Flask API for a user Todo app
3+
4+
# Install dependencies
5+
6+
```shell
7+
pip install -r requirements.txt
8+
```

controllers/todo.py

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
from database.db_connector import SQLite_Connector
2+
from schemas.schemas import Schema
3+
from src.error_message import Error_Message, get_error
4+
from src.success_message import Success_Message
5+
6+
7+
class Todo_Controller(SQLite_Connector):
8+
def __init__(self) -> None:
9+
super().__init__()
10+
11+
def get_todos(self, todoId=None) -> dict:
12+
13+
todos = None
14+
15+
if todoId:
16+
todos = self.execute_sql_query(f"SELECT * FROM todo WHERE id={todoId}", Schema.todo)
17+
else:
18+
todos = self.execute_sql_query(f"SELECT * FROM todo", Schema.todo)
19+
20+
if todoId and not todos:
21+
return Schema.api_response(status=200, error_message=[
22+
Error_Message.todo_not_exist.value])
23+
elif not todoId and not todos:
24+
return Schema.api_response(status=200,
25+
error_message=[Error_Message.todo_not_exist.value])
26+
return Schema.api_response(status=200, data=todos)
27+
28+
def get_todo_with_userId(self, userId) -> dict:
29+
todos = self.execute_sql_query(f"SELECT * FROM todo WHERE userId={userId}", Schema.todo)
30+
31+
if len(todos):
32+
return Schema.api_response(status=400, data=todos)
33+
else:
34+
return Schema.api_response(status=400,
35+
error_message=[Error_Message.there_not_existent_todos.value])
36+
37+
def create(self, userId: int, title: str, todo_description: str) -> dict:
38+
sql_query = f"""
39+
INSERT INTO todo (
40+
userId, title, todo_description
41+
) VALUES ('{userId}', '{title}', '{todo_description}');"""
42+
43+
try:
44+
self.execute_sql_query(sql_query, Schema.todo)
45+
except Exception as error:
46+
error_suf = f"{error}".split(".")
47+
print(error_suf)
48+
return Schema.api_response(
49+
status=500,
50+
error_message=[f"{get_error(error_suf[1])}"]
51+
)
52+
todo = self.get_todos()["data"][-1]
53+
return Schema.api_response(status=200, success_message=[Success_Message.new_todo.value], data=todo)
54+
55+
def update(self, todoId: int, title: str, todo_description: str) -> dict:
56+
todo = self.get_todos(todoId=todoId)["data"]
57+
if len(todo):
58+
sql_query = f"""
59+
UPDATE todo SET title='{title}', todo_description='{todo_description}'
60+
WHERE id={todoId}
61+
"""
62+
try:
63+
self.execute_sql_query(sql_query, Schema.user)
64+
return Schema.api_response(
65+
status=200,
66+
data={"id": todoId, "userId": todo[0]["userId"], "title": title, "todo_description": todo_description},
67+
success_message=[Success_Message.todo_updated.value]
68+
)
69+
except Exception as error:
70+
error_suf = f"{error}".split(".")
71+
return Schema.api_response(
72+
status=500,
73+
error_message=[f"{get_error(error_suf[1])}"]
74+
)
75+
else:
76+
return Schema.api_response(
77+
status=404,
78+
error_message=[Error_Message.todo_not_exist.value]
79+
)
80+
81+
def delete(self, todoId: int) -> dict:
82+
if len(self.get_todos(todoId=todoId)["data"]):
83+
return Schema.api_response(
84+
status=200,
85+
data=self.execute_sql_query(
86+
f"""DELETE FROM todo WHERE id={todoId}""", Schema.todo),
87+
success_message=[Success_Message.deleted_todo.value]
88+
)
89+
else:
90+
return Schema.api_response(
91+
status=404,
92+
error_message=[Error_Message.todo_not_exist.value]
93+
)
94+

controllers/user.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,14 @@ def get_user(self, id=None) -> list:
3232
return Schema.api_response(status=200, data=users)
3333

3434
def create_new(
35-
self, first_name: str, last_name: str, email: str, password: str, user_type="Stander") -> list:
35+
self, first_name: str, last_name: str, email: str, password: str) -> list:
3636

3737
# hash_password = bcrypt.hashpw(
3838
# password.encode("utf-8"), bcrypt.gensalt())
3939

4040
sql_query = f"""
41-
INSERT INTO user (
42-
first_name, last_name, email, hash_password, user_type, account_state
43-
) VALUES (
44-
'{first_name}', '{last_name}', '{email}', '{password}', '{user_type}', 1
45-
);
41+
INSERT INTO user (first_name, last_name, email, hash_password)
42+
VALUES ('{first_name}', '{last_name}', '{email}', '{password}');
4643
"""
4744

4845
try:

database/user.sql

+9-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ CREATE TABLE IF NOT EXISTS user(
44
last_name VARCHAR(15) NOT NULL,
55
email VARCHAR(75) NOT NULL UNIQUE,
66
hash_password VARCHAR(75) NOT NULL,
7-
access_token VARCHAR(50),
8-
user_type VARCHAR(10) NOT NULL,
9-
account_state BOOLEAN NOT NULL
7+
access_token VARCHAR(50)
8+
);
9+
10+
CREATE TABLE IF NOT EXISTS todo (
11+
id INTEGER PRIMARY KEY,
12+
userId INTEGER NOT NULL,
13+
title VARCHAR(15) NOT NULL,
14+
todo_description VARCHAR(200) NOT NULL,
15+
FOREIGN KEY (userId) REFERENCES user (id)
1016
);

database/users.db

4 KB
Binary file not shown.

0 commit comments

Comments
 (0)