Skip to content

Commit

Permalink
Remove question service script loading data
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelim01 committed Oct 1, 2024
1 parent 6268450 commit e61aa7e
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 186 deletions.
8 changes: 4 additions & 4 deletions frontend/src/app/questions/question-upload.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ export class QuestionUploadComponent {
next: (response: UploadQuestionsResponse) => {
this.messageService.add({
severity: 'success',
summary: 'Questions added!',
detail: '',
summary: 'Success',
detail: 'Questions added!',
life: 3000,
});
this.questionsUpsert.emit(response.data);
Expand All @@ -76,8 +76,8 @@ export class QuestionUploadComponent {
error: (error: HttpErrorResponse) => {
this.messageService.add({
severity: 'error',
summary: `${error.error.message}`,
detail: '',
summary: 'Error',
detail: `${error.error.message}`,
life: 3000,
});
const data = error.error?.data || [];
Expand Down
2 changes: 1 addition & 1 deletion services/question/src/controllers/questionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ export const uploadQuestions = async (req: Request, res: Response) => {

try {
const data = await Question.find({ id: { $in: questions.map(q => q.id) } });
handleConflict(res, 'Failed to add some questions', data);
handleConflict(res, 'Failed to add some questions!', data);
} catch {
handleError(res, 'Failed to add questions');
}
Expand Down
142 changes: 0 additions & 142 deletions services/question/src/data/questions.json

This file was deleted.

20 changes: 5 additions & 15 deletions services/question/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
import app from './app';
import { connectToDB, upsertManyQuestions } from './models';
import { getDemoQuestions } from './utils/data';
import { connectToDB } from './models';
import { initializeCounter } from './utils/sequence';

const port = process.env.PORT || 8081;

connectToDB()
.then(async () => {
console.log('MongoDB connected successfully');
return await getDemoQuestions();
})
.then(questions => upsertManyQuestions(questions))
.then(() => {
console.log('Questions synced successfully');
return initializeCounter();
})
.then(() => {
console.log('Question ID initialized successfully');
app.listen(port, () => console.log(`Question service is listening on port ${port}.`));
})
.then(() => console.log('MongoDB connected successfully'))
.then(async () => await initializeCounter())
.then(() => console.log('Question ID initialized successfully'))
.then(() => app.listen(port, () => console.log(`Question service is listening on port ${port}.`)))
.catch(error => {
console.error('Failed to start server');
console.error(error);
Expand Down
12 changes: 0 additions & 12 deletions services/question/src/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import mongoose from 'mongoose';
import { IQuestion, Question } from './questionModel';

export async function connectToDB() {
const mongoURI = process.env.NODE_ENV === 'production' ? process.env.DB_CLOUD_URI : process.env.DB_LOCAL_URI;
Expand All @@ -18,14 +17,3 @@ export async function connectToDB() {
pass: process.env.DB_PASSWORD,
});
}

export async function upsertManyQuestions(questions: IQuestion[]) {
const ops = questions.map(item => ({
updateOne: {
filter: { id: item.id },
update: item,
upsert: true,
},
}));
await Question.bulkWrite(ops);
}
12 changes: 0 additions & 12 deletions services/question/src/utils/data.ts

This file was deleted.

0 comments on commit e61aa7e

Please sign in to comment.