Skip to content

Commit c0c6f02

Browse files
Merge pull request #11 from C216-Distribuid-System-Project/develop
Backend To-Do List Setup and Authentication
2 parents db4dcde + fd67f1e commit c0c6f02

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2191
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,4 @@ cython_debug/
205205
marimo/_static/
206206
marimo/_lsp/
207207
__marimo__/
208+
ToDovenv/

database/02_seed.sql

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
-- insert users
2+
INSERT INTO users (name, email, phone, birth_date, password_hash)
3+
VALUES
4+
('Alice Silva', 'alice@example.com', '11999999999', '1995-05-10', '$2b$12$hashfakealice'),
5+
('Bruno Costa', 'bruno@example.com', '21988888888', '1990-07-22', '$2b$12$hashfakebruno'),
6+
('Carla Mendes', 'carla@example.com', '31977777777', '1988-11-03', '$2b$12$hashfakecarla');
7+
8+
-- insert projects
9+
INSERT INTO projects (user_id, title, description)
10+
VALUES
11+
(1, 'Projeto Casa', 'Organizar tarefas domésticas'),
12+
(1, 'Trabalho', 'Atividades da empresa'),
13+
(2, 'Viagem', 'Planejamento da viagem para 2025');
14+
15+
-- insert tasks
16+
INSERT INTO tasks (user_id, project_id, title, description, due_date, reminder, completed)
17+
VALUES
18+
(1, 1, 'Lavar roupa', 'Separar roupas coloridas e brancas', '2025-09-30 18:00:00', TRUE, FALSE),
19+
(1, 1, 'Comprar mantimentos', 'Ir ao mercado comprar frutas e verduras', '2025-09-25 20:00:00', FALSE, FALSE),
20+
(1, 2, 'Revisar relatório', 'Revisão do relatório trimestral', '2025-09-28 10:00:00', TRUE, FALSE),
21+
(2, 3, 'Reservar hotel', 'Hotel em Florianópolis', '2025-10-10 12:00:00', TRUE, FALSE),
22+
(2, 3, 'Comprar passagens', 'Passagens aéreas ida e volta', '2025-09-27 15:00:00', FALSE, TRUE),
23+
(3, NULL, 'Estudar Python', 'Praticar FastAPI para o backend', '2025-09-26 19:00:00', FALSE, FALSE);

database/database_schema.sql

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
-- Create the database if it doesn't exist
2+
-- and set the character set and collation
3+
CREATE DATABASE IF NOT EXISTS todolist
4+
CHARACTER SET utf8mb4
5+
COLLATE utf8mb4_0900_ai_ci;
6+
7+
USE todolist;
8+
9+
-- 1) USERS
10+
CREATE TABLE IF NOT EXISTS users (
11+
id INT AUTO_INCREMENT PRIMARY KEY,
12+
name VARCHAR(255) NOT NULL,
13+
email VARCHAR(255) NOT NULL UNIQUE,
14+
phone VARCHAR(20) NULL,
15+
birth_date DATE NULL,
16+
password_hash VARCHAR(255) NOT NULL,
17+
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
18+
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
19+
) ENGINE=InnoDB;
20+
21+
-- 2) PROJECTS
22+
CREATE TABLE IF NOT EXISTS projects (
23+
id INT AUTO_INCREMENT PRIMARY KEY,
24+
user_id INT NOT NULL,
25+
title VARCHAR(255) NOT NULL,
26+
description TEXT NULL,
27+
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
28+
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
29+
CONSTRAINT fk_projects_user
30+
FOREIGN KEY (user_id) REFERENCES users(id)
31+
ON DELETE CASCADE
32+
ON UPDATE CASCADE
33+
) ENGINE=InnoDB;
34+
35+
-- 3) TASKS
36+
CREATE TABLE IF NOT EXISTS tasks (
37+
id INT AUTO_INCREMENT PRIMARY KEY,
38+
user_id INT NOT NULL,
39+
project_id INT NULL,
40+
title VARCHAR(255) NOT NULL,
41+
description TEXT NULL,
42+
due_date DATETIME NULL,
43+
reminder BOOLEAN NOT NULL DEFAULT FALSE,
44+
completed BOOLEAN NOT NULL DEFAULT FALSE,
45+
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
46+
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
47+
CONSTRAINT fk_tasks_user
48+
FOREIGN KEY (user_id) REFERENCES users(id)
49+
ON DELETE CASCADE
50+
ON UPDATE CASCADE,
51+
CONSTRAINT fk_tasks_project
52+
FOREIGN KEY (project_id) REFERENCES projects(id)
53+
ON DELETE SET NULL
54+
ON UPDATE CASCADE
55+
) ENGINE=InnoDB;
56+
57+
-- Indexes for performance optimization
58+
CREATE INDEX idx_users_email ON users(email);
59+
CREATE INDEX idx_users_phone ON users(phone);
60+
CREATE INDEX idx_projects_user ON projects(user_id);
61+
CREATE INDEX idx_tasks_user ON tasks(user_id);
62+
CREATE INDEX idx_tasks_project ON tasks(project_id);
63+
CREATE INDEX idx_tasks_due_date ON tasks(due_date);
64+
CREATE INDEX idx_tasks_completed ON tasks(completed);
65+
CREATE INDEX idx_tasks_reminder ON tasks(reminder);
66+
CREATE INDEX idx_tasks_user_completed ON tasks(user_id, completed);

docker/API/Dockerfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM python:3.11-slim
2+
3+
# Define the working directory inside the container
4+
WORKDIR /app
5+
6+
# Copy the project files into the container
7+
COPY . .
8+
9+
# Install dependencies
10+
RUN pip install --no-cache-dir -r requirements.txt
11+
12+
# Default command to run the API
13+
CMD ["python", "-m", "src.main"]

docker/MySQL/dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM mysql:8.0
2+
3+
# Pattern values for development; use secrets or env vars in production setups
4+
ENV MYSQL_DATABASE=todolist
5+
ENV MYSQL_USER=app
6+
ENV MYSQL_PASSWORD=app_pass
7+
ENV MYSQL_ROOT_PASSWORD=root_pass
8+
9+
# Copy schema SQL file to the container's initialization directory
10+
COPY ../../database/database_schema.sql /docker-entrypoint-initdb.d/01_schema.sql
11+
COPY ../../database/02_seed.sql /docker-entrypoint-initdb.d/02_seed.sql

docker/docker-compose.yaml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
services:
2+
mysql:
3+
build:
4+
context: ..
5+
dockerfile: docker/MySQL/dockerfile
6+
image: todolist-mysql
7+
container_name: todolist-mysql
8+
environment:
9+
MYSQL_ROOT_PASSWORD: root_pass
10+
MYSQL_DATABASE: todolist
11+
MYSQL_USER: app
12+
MYSQL_PASSWORD: app_pass
13+
ports:
14+
- "3307:3306"
15+
volumes:
16+
- mysql_data:/var/lib/mysql
17+
18+
mysql-test:
19+
build:
20+
context: ..
21+
dockerfile: docker/MySQL/dockerfile
22+
image: todolist-mysql
23+
container_name: todolist-mysql-test
24+
environment:
25+
MYSQL_ROOT_PASSWORD: root_pass
26+
MYSQL_DATABASE: todolist
27+
MYSQL_USER: app_test
28+
MYSQL_PASSWORD: app_pass_test
29+
ports:
30+
- "3308:3306"
31+
volumes:
32+
- mysql_data_test:/var/lib/mysql
33+
34+
api:
35+
build:
36+
context: ..
37+
dockerfile: docker/API/Dockerfile
38+
image: todolist-api
39+
container_name: api-container
40+
depends_on:
41+
- mysql
42+
ports:
43+
- "8000:8000"
44+
environment:
45+
DATABASE_URL: "mysql+pymysql://app:app_pass@mysql:3306/todolist"
46+
47+
volumes:
48+
mysql_data:
49+
mysql_data_test:
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Docker for the API
2+
3+
## How the Dockerfile Works
4+
5+
The file [`docker/API/dockerfile`](../../docker/API/dockerfile) is responsible for creating a custom image of the **API** built with **FastAPI**.
6+
7+
### Step by step of what it does:
8+
9+
1. **Base image**
10+
```dockerfile
11+
FROM python:3.11-slim
12+
```
13+
Uses the official Python 3.11 slim image (lighter version).
14+
15+
2. **Working directory**
16+
```dockerfile
17+
WORKDIR /app
18+
```
19+
Sets `/app` as the working directory inside the container.
20+
21+
3. **Copy project files**
22+
```dockerfile
23+
COPY . .
24+
```
25+
Copies the entire project into the container.
26+
27+
4. **Install dependencies**
28+
```dockerfile
29+
RUN pip install --no-cache-dir -r requirements.txt
30+
```
31+
Installs all dependencies listed in `requirements.txt`.
32+
33+
5. **Run the API**
34+
```dockerfile
35+
CMD ["python", "src/main.py"]
36+
```
37+
Starts the API using the `src/main.py` file.
38+
39+
---
40+
41+
## How to Run the API
42+
43+
### 1. Build the image
44+
At the root of the project, run:
45+
```bash
46+
docker build -f docker/API/Dockerfile -t api-fastapi .
47+
```
48+
49+
This creates the image `api-fastapi`.
50+
51+
---
52+
53+
### 2. Run the container
54+
```bash
55+
docker run -d --name api-container -p 8000:8000 api-fastapi
56+
```
57+
58+
Explanation of the parameters:
59+
- `-d` → runs in background
60+
- `--name api-container` → container name
61+
- `-p 8000:8000` → maps local port 8000 to container port 8000
62+
- `api-fastapi` → name of the image created
63+
64+
---
65+
66+
## Accessing the API
67+
68+
Once the container is running, the API will be available at:
69+
70+
```
71+
http://localhost:8000
72+
```
73+
74+
If Swagger is enabled in your FastAPI project, the interactive documentation will be available at:
75+
76+
```
77+
http://localhost:8000/docs
78+
```
79+
80+
---
81+
82+
## Useful Commands
83+
84+
### Access container terminal
85+
```bash
86+
docker exec -it api-container bash
87+
```
88+
89+
### Stop the container
90+
```bash
91+
docker stop api-container
92+
```
93+
94+
### Restart a stopped container
95+
```bash
96+
docker start api-container
97+
```
98+
99+
### Remove the container
100+
```bash
101+
docker rm -f api-container
102+
```
103+

0 commit comments

Comments
 (0)