/challengerManagers.ts Secret
Created
June 16, 2024 13:28
Revisions
-
Djsmk123 created this gist
Jun 16, 2024 .There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,51 @@ import { getDb } from './db'; export const getChallenges = async (): Promise<string[]> => { try { const collection = getDb().collection('challenges'); const result = await collection.find().toArray(); return result.map((item) => item.value); } catch (error) { console.error('Error getting challenges from MongoDB:', error); return []; } }; // Function to add challenge to MongoDB export const addChallenge = async (challenge: string): Promise<void> => { try { const collection = getDb().collection('challenges'); await collection.insertOne({ value: challenge }); console.log('Challenge added to MongoDB'); } catch (error) { throw new Error(error); } }; // Function to check if challenge exists in MongoDB export const isChallengeValid = async (challenge: string): Promise<boolean> => { try { const collection = getDb().collection('challenges'); const result = await collection.findOne({ value: challenge }); return !!result; } catch (error) { console.error('Error checking challenge in MongoDB:', error); return false; } }; //delete challenge from MongoDB export const deleteChallenge = async (challenge: string): Promise<void> => { try { const collection = getDb().collection('challenges'); await collection.deleteOne({ value: challenge }); console.log('Challenge deleted from MongoDB'); } catch (error) { console.error('Error deleting challenge from MongoDB:', error); } };