diff --git a/controllers/suggestion.controller .ts b/controllers/suggestion.controller .ts index 11fefe2..6758806 100644 --- a/controllers/suggestion.controller .ts +++ b/controllers/suggestion.controller .ts @@ -5,7 +5,8 @@ import suggestionService from "../services/suggestion.service"; export default { getSuggestions: async (request: Request, response: Response) => { try { - const suggestions = await suggestionService.getSuggestions(); + const { page } = request.query; + const suggestions = await suggestionService.getSuggestions(Number(page)); if (!suggestions || suggestions.length === 0) { return response.status(404).send('No suggestions found'); diff --git a/database/init.sql b/database/init.sql index 9a5ac54..8a38f9e 100644 --- a/database/init.sql +++ b/database/init.sql @@ -23,8 +23,7 @@ CREATE TABLE suggestions ( id SERIAL PRIMARY KEY, user_id INTEGER REFERENCES users(id), title VARCHAR(255) NOT NULL, - description TEXT NOT NULL, - objectives TEXT[] NOT NULL + description TEXT NOT NULL ); -- Quests table @@ -60,44 +59,15 @@ INSERT INTO users (username, role, completed_quests, metadata, password, oauth_p ('pageinabook', 'user', ARRAY[2, 5, 6], '{"sound": false}', '$2b$10$2iWlcbpf1MYROBYodo/4yufNXOpGzwiQenhLjyK3I/D4Y.q3V4aEu', NULL, NULL), ('notacoconut', 'admin', ARRAY[7, 9], '{"sound": true}', '$2b$10$2iWlcbpf1MYROBYodo/4yufNXOpGzwiQenhLjyK3I/D4Y.q3V4aEu', 'google', 'google-id-234'); --- Insert 10 rows into suggestions table with defined objectives -INSERT INTO suggestions (user_id, title, description, objectives) VALUES -(4, 'expand the base', 'Add a second floor to your base', ARRAY[ - 'Gather 1000 wood for construction', - 'Craft and place 20 floor tiles', - 'Install a metal door for added security' -]), -(5, 'scout for enemies', 'Check nearby areas for enemy players', ARRAY[ - 'Travel to the nearest monument and observe activity', - 'Use binoculars to scan the surroundings for potential threats', - 'Report back to the team with findings' -]), -(6, 'gather supplies', 'Collect wood, stone, and metal ore for upgrades', ARRAY[ - 'Chop down 50 trees for wood', - 'Mine 100 stone nodes', - 'Collect 200 metal ore from nearby rocks' -]), -(7, 'build a turret', 'Place an automated turret near your base entrance', ARRAY[ - 'Craft the turret using 1000 metal fragments and 200 high-quality metal', - 'Gather and load 500 ammo rounds into the turret', - 'Position the turret at the base entrance for maximum coverage' -]), -(8, 'upgrade weapons', 'Craft better weapons for defense', ARRAY[ - 'Collect 300 scrap for weapon blueprints', - 'Craft a semi-automatic rifle', - 'Gather 200 gunpowder for ammunition' -]), -(9, 'prepare for winter', 'Stockpile food and resources for the upcoming winter', ARRAY[ - 'Hunt and store 50 pieces of cooked meat', - 'Gather 100 cloth for clothing and warmth', - 'Build 3 campfires for heating' -]), -(10, 'team up', 'Find other players to team up with for raids', ARRAY[ - 'Visit the nearest outpost to meet potential allies', - 'Exchange resources to build trust', - 'Organize a raid schedule with new team members' -]); - +-- Insert 10 rows into suggestions table without objectives +INSERT INTO suggestions (user_id, title, description) VALUES +(4, 'expand the base', 'Add a second floor to your base'), +(5, 'scout for enemies', 'Check nearby areas for enemy players'), +(6, 'gather supplies', 'Collect wood, stone, and metal ore for upgrades'), +(7, 'build a turret', 'Place an automated turret near your base entrance'), +(8, 'upgrade weapons', 'Craft better weapons for defense'), +(9, 'prepare for winter', 'Stockpile food and resources for the upcoming winter'), +(10, 'team up', 'Find other players to team up with for raids'); -- Insert 10 rows into quests table INSERT INTO quests (category_id, description, title, objectives, image_url) VALUES diff --git a/middleware/validate.ts b/middleware/validate.ts index 5ee42c7..8c58a51 100644 --- a/middleware/validate.ts +++ b/middleware/validate.ts @@ -13,7 +13,6 @@ export const validateParams = (schema: any) => { export const validateQuery = (schema: any) => { return (request: Request, response: Response, next: NextFunction) => { - console.log(request.query) const { error } = schema.validate(request.query); if (error) { return response.status(400).json({ error: error.details[0].message }); diff --git a/routes/suggestion.router.ts b/routes/suggestion.router.ts index 1e7f3e7..b177a32 100644 --- a/routes/suggestion.router.ts +++ b/routes/suggestion.router.ts @@ -1,15 +1,24 @@ import express from 'express'; import suggestionController from '../controllers/suggestion.controller '; -import { validateBody } from '../middleware/validate'; +import { validateBody, validateQuery } from '../middleware/validate'; import suggestionSchema from '../validationSchemas/suggestionSchema'; +import isAdmin from '../middleware/isAdmin'; const suggestionRouter = express.Router(); -suggestionRouter.get('/', suggestionController.getSuggestions); +// public routes suggestionRouter.post( '/', validateBody(suggestionSchema.suggestion), suggestionController.createSuggestion ); +// admin routes +suggestionRouter.get( + '/', + isAdmin, + validateQuery(suggestionSchema.allSuggestions), + suggestionController.getSuggestions +); + export default suggestionRouter; \ No newline at end of file diff --git a/services/suggestion.service.ts b/services/suggestion.service.ts index e96a90c..6062f51 100644 --- a/services/suggestion.service.ts +++ b/services/suggestion.service.ts @@ -1,10 +1,33 @@ import { executeQuery } from "../database/connection" export default { - getSuggestions: async () => { - const query = `SELECT * FROM suggestions;`; + getSuggestions: async (offset: number = 1) => { + const query = ` + WITH suggestions_with_user AS ( + SELECT + s.id, + s.title, + s.description, + u.username + FROM + suggestions s + JOIN + users u ON s.user_id = u.id + ) + SELECT + id, + title, + description, + username + FROM + suggestions_with_user + ORDER BY + id + LIMIT 20 OFFSET (($1 - 1) * 20); + `; + const values = [offset] - return await executeQuery(query); + return await executeQuery(query, values); }, createSuggestion: async (userId: string, title: string, description: string) => { diff --git a/validationSchemas/suggestionSchema.ts b/validationSchemas/suggestionSchema.ts index 26b2750..4af90e2 100644 --- a/validationSchemas/suggestionSchema.ts +++ b/validationSchemas/suggestionSchema.ts @@ -1,6 +1,13 @@ import Joi from 'joi'; export default { + allSuggestions: Joi.object({ + page: Joi.number() + .min(1) + .integer() + .required() + }), + suggestion: Joi.object({ title: Joi.string() .min(1)