Skip to content

Commit 35cac47

Browse files
committed
Create db service and example
1 parent 4f76134 commit 35cac47

File tree

6 files changed

+125
-0
lines changed

6 files changed

+125
-0
lines changed

src/api/controllers/tasks.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { Request, Response } from "express";
2+
import { Task } from "../../models/task";
3+
4+
let tasks: Task[] = [];
5+
6+
export const createTask = (req: Request, res: Response) => {
7+
const task: Task = {
8+
id: tasks.length + 1,
9+
title: req.body.title,
10+
description: req.body.description,
11+
completed: false
12+
};
13+
14+
tasks.push(task);
15+
res.status(201).json(task);
16+
};
17+
18+
export const getAllTasks = (req: Request, res: Response) => {
19+
res.json(tasks);
20+
};
21+
22+
export const getTask = (req: Request, res: Response) => {
23+
const task = tasks.find((t) => t.id === parseInt(req.params.id));
24+
25+
if (!task) {
26+
res.status(404).send('Task not found');
27+
} else {
28+
res.json(task);
29+
}
30+
};
31+
32+
export const updateTask = (req: Request, res: Response) => {
33+
const task = tasks.find((t) => t.id === parseInt(req.params.id));
34+
35+
if (!task) {
36+
res.status(404).send('Task not found');
37+
} else {
38+
task.title = req.body.title || task.title;
39+
task.description = req.body.description || task.description;
40+
task.completed = req.body.completed || task.completed;
41+
42+
res.json(task);
43+
}
44+
};
45+
46+
export const deleteTask = (req: Request, res: Response) => {
47+
const index = tasks.findIndex((t) => t.id === parseInt(req.params.id));
48+
49+
if (index === -1) {
50+
res.status(404).send('Task not found');
51+
} else {
52+
tasks.splice(index, 1);
53+
res.status(204).send();
54+
}
55+
};

src/api/routes/tasks.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Router } from "express";
2+
import { createTask, deleteTask, getAllTasks, getTask, updateTask } from "../controllers/tasks";
3+
4+
const router = Router();
5+
6+
router.post('/', createTask);
7+
router.get('/', getAllTasks);
8+
router.get('/:id', getTask);
9+
router.put('/:id', updateTask);
10+
router.delete('/:id', deleteTask);
11+
12+
export default router;

src/app.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import express, { Request, Response } from 'express';
2+
import tasksRoutes from './api/routes/tasks';
3+
4+
const app = express();
5+
6+
app.use(express.json());
7+
app.use('/tasks', tasksRoutes);
8+
9+
app.get('/', (req: Request, res: Response) => {
10+
res.send('Hello, TypeScript Express!');
11+
});
12+
13+
14+
export default app;

src/models/task.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export interface Task {
2+
id: number;
3+
title: string;
4+
description: string;
5+
completed: boolean;
6+
}

src/server.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import app from './app';
2+
import { port } from './config/server.config';
3+
import db from './services/db';
4+
5+
const initApp = async () => {
6+
console.log('Database connection...');
7+
8+
app.listen(port || 3000, () => {
9+
console.log(`Server running at http://localhost:${port}`);
10+
});
11+
12+
try {
13+
await db.authenticate();
14+
console.log("Database connection has been established successfully.");
15+
} catch (error) {
16+
console.error("Unable to connect to the database:", error);
17+
}
18+
};
19+
20+
initApp();

src/services/db.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Sequelize } from "sequelize-typescript";
2+
import dbConfig from "../config/db.config";
3+
4+
const db = new Sequelize({
5+
database: dbConfig.DATABASE,
6+
username: dbConfig.USER,
7+
password: dbConfig.PASSWORD,
8+
host: dbConfig.HOST,
9+
dialect: "mysql",
10+
pool: {
11+
max: 5,
12+
min: 0,
13+
acquire: 30000,
14+
idle: 10000
15+
}
16+
});
17+
18+
export default db;

0 commit comments

Comments
 (0)