-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserController.js
140 lines (140 loc) · 4.7 KB
/
UserController.js
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = __importDefault(require("express"));
const cors_1 = __importDefault(require("cors"));
const queries_1 = require("./queries");
const app = (0, express_1.default)();
const router = express_1.default.Router();
// Use CORS middleware
const corsOptions = {
origin: 'http://localhost:5173', // Replace with your frontend URL
methods: 'GET,POST,PUT,DELETE',
allowedHeaders: 'Content-Type,Authorization'
};
app.use((0, cors_1.default)(corsOptions));
app.options('*', (0, cors_1.default)(corsOptions)); // Enable preflight requests for all routes
// Other middlewares
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', 'http://localhost:5173'); // Replace with your frontend URL
res.header('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type,Authorization');
console.log(`${req.method} ${req.url}`);
next();
});
app.use(express_1.default.json());
// Get all todos
/**
* @swagger
* /api/todos:
* get:
* summary: Retrieve a list of todos
* responses:
* 200:
* description: A list of todos
* content:
* application/json:
* schema:
* type: array
* items:
* type: object
* properties:
* id:
* type: integer
* text:
* type: string
*/
router.get('/todos', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
try {
const todos = yield (0, queries_1.getTodos)();
console.log(todos);
res.json(todos);
}
catch (error) {
console.error('Error fetching todos:', error);
res.status(500).json({ error: 'Internal server error' });
}
}));
// Add a new todo
/**
* @swagger
* /api/todos:
* post:
* summary: Create a new todo
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* name:
* type: string
* responses:
* 201:
* description: The created todo
* content:
* application/json:
* schema:
* type: object
* properties:
* id:
* type: integer
* text:
* type: string
*/
router.post('/todos', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
try {
console.log('post todos start');
const { name } = req.body;
const newTodo = yield (0, queries_1.addTodo)(name);
res.status(201).json(newTodo);
}
catch (error) {
console.error('Error adding todo:', error);
res.status(500).json({ error: 'Internal server error' });
}
}));
// Update an existing todo
router.put('/todos/:id', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
try {
const { id } = req.params;
const { name } = req.body;
const updatedTodo = yield (0, queries_1.updateTodo)(Number(id), name);
res.json(updatedTodo);
}
catch (error) {
console.error('Error updating todo:', error);
res.status(500).json({ error: 'Internal server error' });
}
}));
// Delete a todo
router.delete('/todos/:id', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
try {
const { id } = req.params;
const deletedTodo = yield (0, queries_1.deleteTodo)(Number(id));
res.json(deletedTodo);
}
catch (error) {
console.error('Error deleting todo:', error);
res.status(500).json({ error: 'Internal server error' });
}
}));
app.use('/', router);
/*const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
*/
exports.default = app;