Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions server/src/models/Characters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import mongoose, { Schema, Document } from 'mongoose';

export interface ICharacter extends Document {
name: string;
picture: string;
voice: string;
}

const CharacterSchema: Schema = new Schema(
{
name: {
type: String,
required: true,
trim: true,
},
picture: {
type: String,
required: true,
},
voice: {
type: String,
required: true,
},
},
);

const Character = mongoose.model<ICharacter>('Character', CharacterSchema);

export default Character;
26 changes: 23 additions & 3 deletions server/src/schemas/resolvers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import User from '../models/User';
import Character from '../models/Characters';
import { signToken } from '../utils/auth';
import { AuthenticationError } from 'apollo-server-express';

Expand All @@ -23,15 +24,27 @@ const resolvers = {
}
throw new AuthenticationError('You need to be logged in!');
},
users: async () => {
return await User.find();
},
user: async (_: any, { username }: { username: string }) => {
return await User.findOne({ username });
},
characters: async () => {
return await Character.find();
},
character: async (_: any, { id }: { id: string }) => {
return await Character.findById(id);
},
},
Mutation: {
addUser: async (_parent: any, { input }: AddUserArgs) => {
const user = await User.create(input) as { username: string; email: string; _id: string };
const user = await User.create(input);
const token = signToken(user.username, user.email, user._id);
return { token, user };
},
login: async (_parent: any, { email, password }: LoginUserArgs) => {
const user = await User.findOne({ email }) as { username: string; email: string; _id: string; isCorrectPassword: (password: string) => Promise<boolean> };
const user = await User.findOne({ email });

if (!user) {
throw new AuthenticationError('Invalid credentials');
Expand All @@ -46,7 +59,14 @@ const resolvers = {
const token = signToken(user.username, user.email, user._id);
return { token, user };
},
createCharacter: async (_: any, { name, picture, voice }: { name: string, picture: string, voice: string }) => {
const newCharacter = new Character({ name, picture, voice });
return await newCharacter.save();
},
deleteCharacter: async (_: any, { id }: { id: string }) => {
return await Character.findByIdAndDelete(id);
},
},
};

export default resolvers;
export default resolvers;
22 changes: 19 additions & 3 deletions server/src/schemas/typeDefs.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
const typeDefs = `
import { gql } from 'apollo-server-express';

const typeDefs = gql`
type User {
_id: ID
username: String
email: String
password: String
correctAnswers: Int
wrongAnswers: Int
}

input UserInput {
username: String!
email: String!
password: String!
}


type Character {
_id: ID!
name: String!
picture: String!
voice: String!
}

type Auth {
token: ID!
user: User
Expand All @@ -21,12 +32,17 @@ const typeDefs = `
users: [User]
user(username: String!): User
me: User
updateStats(isCorrect: Boolean!): User
characters: [Character]
character(id: ID!): Character
}

type Mutation {
addUser(input: UserInput!): Auth
login(email: String!, password: String!): Auth
createCharacter(name: String!, picture: String!, voice: String!): Character
deleteCharacter(id: ID!): Character
}
`;

export default typeDefs;
export default typeDefs;
4 changes: 3 additions & 1 deletion server/src/utils/PromptBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { FallbackQuestion, fallbackQuestion } from "@/utils/fallbackQuestions";

import { FallbackQuestion, fallbackQuestion } from "../utils/fallbackQuestions";


export class PromptBuilder {
/**
Expand Down
Loading