Skip to content

Commit

Permalink
Merge pull request #33 from brandonnorsworthy/updates
Browse files Browse the repository at this point in the history
new leaderboard
  • Loading branch information
brandonnorsworthy authored Sep 20, 2024
2 parents c28d307 + f3fa372 commit be843c5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 8 deletions.
11 changes: 11 additions & 0 deletions controllers/suggestion.controller .ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ export default {
}
},

getLeaderboard: async (request: Request, response: Response) => {
try {
const leaderboard = await suggestionService.getLeaderboard();

return response.send(leaderboard);
} catch (error) {
console.error(error);
return response.status(500).send('An error occurred while retrieving the leaderboard');
}
},

createSuggestion: async (request: Request, response: Response) => {
try {
let { title, description } = request.body;
Expand Down
15 changes: 8 additions & 7 deletions database/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ CREATE TABLE users (
password VARCHAR(255),
metadata JSONB NOT NULL DEFAULT '{}',
completed_quests INTEGER[] NOT NULL DEFAULT ARRAY[]::INTEGER[],
approved_suggestions INTEGER DEFAULT 0,
oauth_provider VARCHAR(50), -- For OAuth integration (e.g., Google, Facebook)
oauth_id VARCHAR(255), -- ID from the OAuth provider
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
Expand Down Expand Up @@ -85,13 +86,13 @@ INSERT INTO categories (name) VALUES
('Building');

-- Insert 10 rows into users table
INSERT INTO users (username, role_id, completed_quests, metadata, password) VALUES
('zcog', 3, ARRAY[1, 3, 6, 8], '{"sound": false}', '$2b$10$ita5UtzrE2JBh.275g5i8ebBnnM99D9wZhRmcqfZYfgTjbt.baNyG'),
('notacoconut', 3, ARRAY[7, 9], '{"sound": true}', '$2b$10$ita5UtzrE2JBh.275g5i8ebBnnM99D9wZhRmcqfZYfgTjbt.baNyG'),
('demouser1', 2, ARRAY[2, 4, 7], '{"sound": true}', '$2b$10$ita5UtzrE2JBh.275g5i8ebBnnM99D9wZhRmcqfZYfgTjbt.baNyG'),
('demouser2', 2, ARRAY[1, 3, 4], '{"sound": true}', '$2b$10$ita5UtzrE2JBh.275g5i8ebBnnM99D9wZhRmcqfZYfgTjbt.baNyG'),
('demouser3', 1, ARRAY[5, 6], '{"sound": false}', '$2b$10$ita5UtzrE2JBh.275g5i8ebBnnM99D9wZhRmcqfZYfgTjbt.baNyG'),
('demouser4', 1, ARRAY[1, 2, 8], '{"sound": true}', '$2b$10$ita5UtzrE2JBh.275g5i8ebBnnM99D9wZhRmcqfZYfgTjbt.baNyG');
INSERT INTO users (username, role_id, completed_quests, metadata, password, approved_suggestions) VALUES
('zcog', 3, ARRAY[1, 3, 6, 8], '{"sound": false}', '$2b$10$ita5UtzrE2JBh.275g5i8ebBnnM99D9wZhRmcqfZYfgTjbt.baNyG', 0),
('notacoconut', 3, ARRAY[7, 9], '{"sound": true}', '$2b$10$ita5UtzrE2JBh.275g5i8ebBnnM99D9wZhRmcqfZYfgTjbt.baNyG', 0),
('demouser1', 2, ARRAY[2, 4, 7], '{"sound": true}', '$2b$10$ita5UtzrE2JBh.275g5i8ebBnnM99D9wZhRmcqfZYfgTjbt.baNyG', 2),
('demouser2', 2, ARRAY[1, 3, 4], '{"sound": true}', '$2b$10$ita5UtzrE2JBh.275g5i8ebBnnM99D9wZhRmcqfZYfgTjbt.baNyG', 1),
('demouser3', 1, ARRAY[5, 6], '{"sound": false}', '$2b$10$ita5UtzrE2JBh.275g5i8ebBnnM99D9wZhRmcqfZYfgTjbt.baNyG', 1),
('demouser4', 1, ARRAY[1, 2, 8], '{"sound": true}', '$2b$10$ita5UtzrE2JBh.275g5i8ebBnnM99D9wZhRmcqfZYfgTjbt.baNyG', 1);

-- Insert 10 rows into suggestions table without objectives
INSERT INTO suggestions (user_id, title, description) VALUES
Expand Down
4 changes: 4 additions & 0 deletions routes/suggestion.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ suggestionRouter.post(
validateBody(suggestionSchema.suggestion),
suggestionController.createSuggestion
);
suggestionRouter.get(
'/leaderboard',
suggestionController.getLeaderboard
)

// admin routes
suggestionRouter.get(
Expand Down
11 changes: 10 additions & 1 deletion services/quest.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,16 @@ export default {
`;
const values = [title, description, objectives, categoryId, suggestedByUserId, imageUrl];

return await executeQuery(query, values, true);
const newQuest = await executeQuery(query, values, true);

const userQuest = `
UPDATE users
SET approved_suggestions = approved_suggestions + 1
WHERE id = $1;
`
await executeQuery(userQuest, [suggestedByUserId]);

return newQuest
},

updateQuest: async (questId: number, userId: number, params: UpdateQuestParams): Promise<Quest> => {
Expand Down
16 changes: 16 additions & 0 deletions services/suggestion.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ export default {
return await executeQuery(query, values);
},

getLeaderboard: async () => {
const query = `
SELECT u.id,
u.username,
u.approved_suggestions AS suggestions
FROM users u
JOIN roles r ON u.role_id = r.id
WHERE r.name != 'guest'
ORDER BY u.approved_suggestions DESC,
u.id ASC
LIMIT 20;
`;

return await executeQuery(query);
},

getSuggestionById: async (suggestionId: number): Promise<{
id: number;
title: string;
Expand Down

0 comments on commit be843c5

Please sign in to comment.