Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: populate game room with questions #293

Merged
merged 11 commits into from
Mar 15, 2022
32 changes: 31 additions & 1 deletion backend/src/graphql/GameRoom/datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { ApolloError, UserInputError } = require("apollo-server-core");
const mongoose = require("mongoose");

const ChatRoom = require("../../schemas/ChatRoom/ChatRoom");
const Question = require("../../schemas/Question/Question");
const GameRoom = require("../../schemas/GameRoom/GameRoom");

const GameAnswerEnum = require("../../enum/GameAnswer");
Expand Down Expand Up @@ -34,9 +35,12 @@ class GameRoomAPI extends DataSource {
];

// add 10 questions
const tenQuestions = await this.getQuestion();
const questions = tenQuestions.map((ques) => ques._id);
oliver-pham marked this conversation as resolved.
Show resolved Hide resolved
const gameRoom = new GameRoom({
_id,
answers,
questions,
});

chatRoom.gameRoom = _id;
Expand All @@ -47,7 +51,7 @@ class GameRoomAPI extends DataSource {
return _id;
} catch (err) {
console.error(err);
throw new ApolloError("Internal Server Error");
throw new ApolloError(err);
}
}

Expand Down Expand Up @@ -76,6 +80,32 @@ class GameRoomAPI extends DataSource {
throw new ApolloError("Internal Server Error");
}
}

//for testing
async getGameRoomById(gameRoomId) {
try {
const gameRoom = await GameRoom.findById(gameRoomId);
return gameRoom;
} catch (err) {
throw new ApolloError("Internal Server Error");
}
}

async getQuestion() {
oliver-pham marked this conversation as resolved.
Show resolved Hide resolved
try {
let questions = [];
const limit = parseInt(process.env.QUESTION_LIMIT) || 10;
questions = await Question.aggregate([
{
$sample: { size: limit },
},
]);
return questions;
} catch (err) {
console.log(err);
throw new ApolloError("Internal Server Error - get question");
}
}
}

module.exports.GameRoomAPI = GameRoomAPI;
2 changes: 2 additions & 0 deletions backend/src/graphql/GameRoom/mutations.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module.exports.mutations = `
submitAnswers(gameRoomId: String!, answers: [String!]!) : GeneralMutationResponse!
"this is for testing purposes"
createGameRoom(chatRoomId: ID!): GeneralMutationResponse
dtnguyen22 marked this conversation as resolved.
Show resolved Hide resolved
`;
1 change: 1 addition & 0 deletions backend/src/graphql/GameRoom/queries.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
module.exports.queries = `
getGameRoomById(gameRoomId: ID!): GameRoom
`;
37 changes: 36 additions & 1 deletion backend/src/graphql/GameRoom/resolvers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const queries = {};
const { AuthenticationError, ApolloError } = require("apollo-server-core");

const mutations = {
submitAnswers: async (_, args, { dataSources, req, userAuthentication }) => {
Expand All @@ -15,6 +15,41 @@ const mutations = {
message: "Submit successfully",
};
},
createGameRoom: async (
_,
{ chatRoomId },
{ dataSources, req, userAuthentication }
) => {
userAuthentication(req.user);
tuanthanh2067 marked this conversation as resolved.
Show resolved Hide resolved
try {
userAuthentication(req.user);
const id = await dataSources.gameRoomAPI.createGameRoom(chatRoomId);
if (id) {
return {
success: true,
message: id,
dtnguyen22 marked this conversation as resolved.
Show resolved Hide resolved
};
}
} catch (err) {
throw new ApolloError(err);
}
},
};

const queries = {
getGameRoomById: async (
_,
{ gameRoomId },
{ dataSources, req, userAuthentication }
) => {
userAuthentication(req.user);
try {
userAuthentication(req.user);
return await dataSources.gameRoomAPI.getGameRoomById(gameRoomId);
} catch (err) {
throw new ApolloError(err);
}
},
};

const subscriptions = {};
Expand Down
10 changes: 10 additions & 0 deletions backend/src/graphql/GameRoom/types.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
module.exports.types = `
type GameRoom {
id: ID!
questions: [ID]
answers: [GameAnswer]
}

type GameAnswer{
id: ID!
user: ID!
answers: [String]
}
`;
30 changes: 30 additions & 0 deletions backend/src/graphql/Question/datasource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const { DataSource } = require("apollo-datasource");
const { UserInputError, ApolloError } = require("apollo-server-core");

const Question = require("../../schemas/Question/Question");

class QuestionAPI extends DataSource {
constructor() {
super();
}
async getQuestion() {
try {
let questions = [];
const limit = parseInt(process.env.QUESTION_LIMIT) || 10;
questions = await Question.aggregate([
{
$sample: { size: limit },
},
]);
return questions.map((obj) => ({
...obj,
id: obj._id,
}));
} catch (err) {
console.log(err);
throw new ApolloError("Internal Server Error - get question");
}
}
}

module.exports.QuestionAPI = QuestionAPI;
13 changes: 13 additions & 0 deletions backend/src/graphql/Question/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { queries } = require("./queries");
const { mutations } = require("./mutations");
const { resolvers } = require("./resolvers");
const { types } = require("./types");
const { QuestionAPI } = require("./datasource");

module.exports.Question = {
queries,
mutations,
resolvers,
types,
QuestionAPI,
};
3 changes: 3 additions & 0 deletions backend/src/graphql/Question/mutations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports.mutations = `

`;
4 changes: 4 additions & 0 deletions backend/src/graphql/Question/queries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports.queries = `
"get 10 questions to populate game room"
getQuestion: [Question!]!
dtnguyen22 marked this conversation as resolved.
Show resolved Hide resolved
`;
19 changes: 19 additions & 0 deletions backend/src/graphql/Question/resolvers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { AuthenticationError, ApolloError } = require("apollo-server-core");

const queries = {
getQuestion: async (_, __, { dataSources, req, userAuthentication }) => {
try {
userAuthentication(req.user);
return await dataSources.questionAPI.getQuestion();
} catch (err) {
throw new ApolloError(err);
}
},
dtnguyen22 marked this conversation as resolved.
Show resolved Hide resolved
};

const mutations = {};

module.exports.resolvers = {
queries,
mutations,
};
7 changes: 7 additions & 0 deletions backend/src/graphql/Question/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports.types = `
type Question{
id: ID!
question: String!,
topics: [String!]!
}
`;
2 changes: 2 additions & 0 deletions backend/src/graphql/datasources.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const { UserStatus } = require("./UserStatus");
const { PlayingStatus } = require("./PlayingStatus");
const { OpeningLine } = require("./OpeningLine");
const { Image } = require("./Image");
const { Question } = require("./Question");

module.exports = {
UserAPI: User.UserAPI,
Expand All @@ -26,4 +27,5 @@ module.exports = {
PlayingStatusAPI: PlayingStatus.PlayingStatusAPI,
OpeningLineAPI: OpeningLine.OpeningLineAPI,
ImageAPI: Image.ImageAPI,
QuestionAPI: Question.QuestionAPI,
};
3 changes: 3 additions & 0 deletions backend/src/graphql/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const { UserStatus } = require("./UserStatus");
const { OpeningLine } = require("./OpeningLine");
const { Upload } = require("./Upload");
const { Image } = require("./Image");
const { Question } = require("./Question");

const resolvers = {
Upload: GraphQLUpload,
Expand All @@ -29,6 +30,7 @@ const resolvers = {
...OpeningLine.resolvers.queries,
...Upload.resolvers.queries,
...Image.resolvers.queries,
...Question.resolvers.queries,
},
Mutation: {
...User.resolvers.mutations,
Expand All @@ -43,6 +45,7 @@ const resolvers = {
...OpeningLine.resolvers.mutations,
...Upload.resolvers.mutations,
...Image.resolvers.mutations,
...Question.resolvers.mutations,
},
Subscription: {
...ChatRoom.resolvers.subscriptions,
Expand Down
4 changes: 4 additions & 0 deletions backend/src/graphql/typeDefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const { SharedType } = require("./SharedType");
const { GameRoom } = require("./GameRoom");
const { GameRequest } = require("./GameRequest");
const { Upload } = require("./Upload");
const { Question } = require("./Question");

const typeDefs = gql`
${interfaces.types}
Expand All @@ -37,6 +38,7 @@ const typeDefs = gql`
${UserStatus.types}
${OpeningLine.types}
${Upload.types}
${Question.types}

type Query {
${User.queries}
Expand All @@ -52,6 +54,7 @@ const typeDefs = gql`
${OpeningLine.queries}
${Upload.queries}
${Image.queries}
${Question.queries}
}

type Mutation {
Expand All @@ -67,6 +70,7 @@ const typeDefs = gql`
${OpeningLine.mutations}
${Upload.mutations}
${Image.mutations}
${Question.mutations}
}

type Subscription {
Expand Down
1 change: 1 addition & 0 deletions backend/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const dataSources = () => ({
playingStatusAPI: new datasources.PlayingStatusAPI(),
openingLineAPI: new datasources.OpeningLineAPI(),
imageAPI: new datasources.ImageAPI(),
questionAPI: new datasources.QuestionAPI(),
});

cloudinary.config({
Expand Down