Initial commit
This commit is contained in:
54
skills/backend-endpoint/examples/users-get.ts
Normal file
54
skills/backend-endpoint/examples/users-get.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* User Routes - GET endpoint example
|
||||
*
|
||||
* @route GET /api/users/:id
|
||||
* @access Private
|
||||
*/
|
||||
|
||||
import { Router, Request, Response, NextFunction } from 'express';
|
||||
import { authMiddleware } from '../middleware/auth';
|
||||
|
||||
const router = Router();
|
||||
|
||||
/**
|
||||
* GET /api/users/:id
|
||||
* @description Get user by ID
|
||||
* @access Private
|
||||
*/
|
||||
router.get(
|
||||
'/api/users/:id',
|
||||
authMiddleware,
|
||||
async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
// TODO: Fetch user from database
|
||||
const user = await getUserById(id);
|
||||
|
||||
if (!user) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
error: 'User not found',
|
||||
});
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: user,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
export default router;
|
||||
|
||||
// Mock function (replace with actual database query)
|
||||
async function getUserById(id: string) {
|
||||
return {
|
||||
id,
|
||||
name: 'John Doe',
|
||||
email: 'john@example.com',
|
||||
};
|
||||
}
|
||||
59
skills/backend-endpoint/examples/users-post.ts
Normal file
59
skills/backend-endpoint/examples/users-post.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* User Routes - POST endpoint example with validation
|
||||
*
|
||||
* @route POST /api/users
|
||||
* @access Private
|
||||
*/
|
||||
|
||||
import { Router, Request, Response, NextFunction } from 'express';
|
||||
import { z } from 'zod';
|
||||
import { authMiddleware } from '../middleware/auth';
|
||||
import { validateRequest } from '../middleware/validation';
|
||||
|
||||
const router = Router();
|
||||
|
||||
// Validation schema
|
||||
const createUserSchema = z.object({
|
||||
name: z.string().min(1).max(100),
|
||||
email: z.string().email(),
|
||||
age: z.number().int().min(18).optional(),
|
||||
});
|
||||
|
||||
type CreateUserInput = z.infer<typeof createUserSchema>;
|
||||
|
||||
/**
|
||||
* POST /api/users
|
||||
* @description Create new user
|
||||
* @access Private
|
||||
*/
|
||||
router.post(
|
||||
'/api/users',
|
||||
authMiddleware,
|
||||
validateRequest(createUserSchema),
|
||||
async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const userData: CreateUserInput = req.body;
|
||||
|
||||
// TODO: Save user to database
|
||||
const newUser = await createUser(userData);
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
data: newUser,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
export default router;
|
||||
|
||||
// Mock function (replace with actual database insert)
|
||||
async function createUser(data: CreateUserInput) {
|
||||
return {
|
||||
id: '123',
|
||||
...data,
|
||||
createdAt: new Date(),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user