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

Anne onboarding1 #798

Open
wants to merge 1 commit into
base: gql
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions apps/backend/src/bootstrap/loaders/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ export default async (
isAuthenticated: req.isAuthenticated(),
logout: (callback: (err: any) => void) => req.logout(callback),
},
// user: req.user
// ? {
// ...req.user,
// isAuthenticated: req.isAuthenticated(),
// logout: (callback: (err: any) => void) => req.logout(callback),
// }
// : {
// id: "testUserId123",
// role: "admin",
// isAuthenticated: true, // 让 auth 解析器认为用户已登录
// logout: (callback: (err: any) => void) => callback(null),
// },
}),
})
);
Expand Down
39 changes: 39 additions & 0 deletions apps/backend/src/modules/discussion/discussionController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {DiscussionModel} from "@repo/common"
import {formatPost} from "./formater"

export const getDiscussions = async (courseNumber: string) => {
const discussion = await DiscussionModel.findOne({courseNumber: courseNumber});
if (!discussion) {
return { courseNumber, posts: [] };
}
const discussionObject = discussion.toObject();
return {
courseNumber: discussionObject.courseNumber,
posts: discussionObject.posts.map(formatPost)};
};

export const addPost = async(
courseNumber: string,
PostContent: string,
context: any
) => {
const userId = context.user.id;
const updatedPost = await DiscussionModel.findOneAndUpdate(
{ courseNumber },
{
$push: {
posts: {
userId,
PostContent,
}
}
},
{ new: true, upsert: true }
);
if (!updatedPost) {
throw new Error("Failed to update or create the discussion post.");
}
const updatedPostObj = updatedPost.toObject();
const lastPost = updatedPostObj.posts[updatedPostObj.posts.length - 1];
return formatPost(lastPost);
};
5 changes: 5 additions & 0 deletions apps/backend/src/modules/discussion/formater.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const formatPost = (post:any) => ({
userId: post.userId.toString(),
PostTime: post.PostTime.toISOString(),
PostContent: post.PostContent,
});
8 changes: 8 additions & 0 deletions apps/backend/src/modules/discussion/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import resolver from "./resolver";
import typeDef from "./typedefs/discussion";

export default {
resolver,
typeDef,
};

21 changes: 21 additions & 0 deletions apps/backend/src/modules/discussion/resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { getDiscussions, addPost } from "./discussionController";
import {DiscussionModule} from "./generated-types/module-types"

const resolvers: DiscussionModule.Resolvers = {
Query: {
discussions: async (_, {courseNumber}) => {
return await getDiscussions(courseNumber);
}
},
Mutation: {
post: async (
_,
{ courseNumber, PostContent},
context) => {
return await addPost(courseNumber, PostContent, context);
}
}
};

export default resolvers;

25 changes: 25 additions & 0 deletions apps/backend/src/modules/discussion/typedefs/discussion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { gql } from "graphql-tag";

export default gql`
scalar courseNumber

type Query {
discussions(courseNumber:courseNumber!): Discussion
}

type Post {
userId: String!
PostTime: String!
PostContent: String!
}

type Discussion {
courseNumber: courseNumber!
posts: [Post!]
}

type Mutation {
post(courseNumber: courseNumber!, PostContent: String!): Post! @auth
}

`;
2 changes: 2 additions & 0 deletions apps/backend/src/modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import GradeDistribution from "./grade-distribution";
import Schedule from "./schedule";
import Term from "./term";
import User from "./user";
import Discussion from "./discussion";

const modules = [
User,
Expand All @@ -20,6 +21,7 @@ const modules = [
Course,
Class,
Enrollment,
Discussion
];

export const resolvers = merge(modules.map((module) => module.resolver));
Expand Down
30 changes: 30 additions & 0 deletions packages/common/src/models/discussion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import mongoose, { Schema, Document, Model } from "mongoose";

interface Post {
userId: string;
PostContent: string;
PostTime: Date;
}
export interface DiscussionInfo extends Document {
courseNumber: string;
posts: Post[];
}

const postSchema: Schema = new Schema({
_id: { type: mongoose.Schema.Types.ObjectId, auto: true },
userId: { type: String, required: true }, //connect user?
PostTime: { type: Date, default: Date.now },
PostContent: { type: String, required: true },
});

const discussionSchema: Schema<DiscussionInfo> = new Schema({
courseNumber: { type: String, required: true },
posts: { type: [postSchema], default: []},
});

export const DiscussionModel: Model<DiscussionInfo> = mongoose.model<DiscussionInfo>(
"Discussion",
discussionSchema
);


1 change: 1 addition & 0 deletions packages/common/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from "./course";
export * from "./section";
export * from "./grade-distribution";
export * from "./enrollment-history";
export * from "./discussion";