Skip to content

Commit

Permalink
Merge branch 'crowd-sourced-data' of https://github.com/asuc-octo/ber…
Browse files Browse the repository at this point in the history
…keleytime into crowd-sourced-data
  • Loading branch information
nnicolee committed Oct 30, 2024
2 parents f738445 + 1604307 commit 7cc3134
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 29 deletions.
2 changes: 2 additions & 0 deletions apps/backend/src/modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Common from "./common";
import Course from "./course";
import Enrollment from "./enrollment";
import GradeDistribution from "./grade-distribution";
import Rating from "./rating";
import Schedule from "./schedule";
import Term from "./term";
import User from "./user";
Expand All @@ -20,6 +21,7 @@ const modules = [
Course,
Class,
Enrollment,
Rating,
];

export const resolvers = merge(modules.map((module) => module.resolver));
Expand Down
59 changes: 59 additions & 0 deletions apps/backend/src/modules/rating/controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { RatingModel, RatingType } from '@repo/common';
import {
CreateRatingInput,
DeleteRatingInput,
Mutation,
Query,
Rating,
RatingSummary,
UpdateRatingInput,
} from "../../generated-types/graphql";

export const getRating = async (name: string, subject: string, number: string) => {
if (!context.user._id) throw new Error("Unauthorized");

const rating = mongoose.model("schedule", scheduleSchema)
return null;
}

export const getUserRatings = async () => {

}

export const createRating = async (context: any, input: CreateRatingInput) => {
if (!context.user._id) throw new Error("Unauthorized");
if (input.value < 1 || input.value > 5) {
throw new Error("Rating must be between 1 and 5");
}

};
// export const getSchedule = async (context: any, id: string) => {
// const schedule = await ScheduleModel.findOne({
// _id: id,
// $or: [{ public: true }, { createdBy: context.user._id }],
// });

// if (!schedule) throw new Error("Not found");

// return await formatSchedule(schedule);
// };

// export const createSchedule = async (
// context: any,
// input: CreateScheduleInput
// ) => {
// if (!context.user._id) throw new Error("Unauthorized");

// const term = await TermModel.findOne({
// name: `${input.year} ${input.semester}`,
// });

// if (!term) throw new Error("Invalid term");

// const schedule = await ScheduleModel.create({
// ...input,
// createdBy: context.user._id,
// });

// return await formatSchedule(schedule);
// };
Empty file.
7 changes: 7 additions & 0 deletions apps/backend/src/modules/rating/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import resolver from "./resolver";
import typeDef from "./typedefs/rating";

export default {
resolver,
typeDef,
};
32 changes: 32 additions & 0 deletions apps/backend/src/modules/rating/resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {
getRating,
getUserRatings,
createRating
} from "./controller";

import { RatingModule } from "./generated-types/module-types";

const resolvers: RatingModule.Resolvers = {
Query: {
rating: async (_, { name: name, subject: subject, number: number }) => {
const ratingSummary = await getRating(name, subject, number);
return ratingSummary as RatingModule.RatingSummary;
},
user_ratings: async (_, { subject: subject, number: number }, context) => {
const ratings = await getUserRatings(context, subject, number);
return ratings as RatingModule.Rating[];
}
},
Mutation: {
createRating: async(_, { rating }, context) => {
const ratingSummary = await createRating(rating, context);
return ratingSummary as RatingModule.RatingSummary;
},
deleteRating: async(_, { subject: subject, number: number }, context) => {
const ratingSummary = await deleteRating(subject, number, context);
return ratingSummary as RatingModule.RatingSummary;
}
}
}

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

const typedef = gql`
type HistogramEntry {
value: Int!
count: Int!
class: String!
}
type ClassHistogram {
class: Class
histogram: [HistogramEntry!]
}
type RatingSummary {
name: String!
class_histogram: [ClassHistogram!]
overall_histogram: [HistogramEntry!]
totalCount: Int!
average: Float!
}
type Rating {
class: Class!
question: String!
value: Int!
}
type Query {
rating(name: String!, subject: String!, number: String!): RatingSummary!
user_ratings(subject: String!, number: String!): [Rating] @auth
}
type CreateRatingInput {
class: String!
question: String!
value: Int!
}
type Mutation {
createRating(rating: CreateRatingInput): RatingSummary! @auth
deleteRating(subject: String!, number: String!): RatingSummary! @auth
}
`;

export default typedef;
56 changes: 28 additions & 28 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"eslint": "^8.56.0",
"patch-package": "^8.0.0",
"prettier": "^3.3.3",
"turbo": "^2.0.9"
"turbo": "^2.2.3"
},
"name": "berkeleytime"
}
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,4 +6,5 @@ export * from "./term";
export * from "./course";
export * from "./course";
export * from "./section";
export * from "./rating";
export * from "./grade-distribution";
51 changes: 51 additions & 0 deletions packages/common/src/models/rating.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import mongoose, { InferSchemaType, Schema } from "mongoose";

const ratingSchema = new Schema({
_id: Schema.Types.ObjectId,
google_id: {
type: String,
trim: true,
required: true,
immutable: true,
select: false,
},
email: {
type: String,
trim: true,
required: true,
immutable: true,
},
class: {
type: Schema.Types.ObjectId,
ref: 'Class',
required: true,
immutable: true
},
// likert int 1 -> 5 values
// boolean 0/1 values
// string properties assign hash code
value: {
type: Number,
required: true,
validate: {
validator: Number.isInteger,
}
},
// indicate value type (granularity: specific questions)
value_type: {
type: String,
required: true,
}
}, {
timestamps: {
createdAt: "createdAt",
updatedAt: "updatedAt",
},
});

export const RatingModel = mongoose.model(
"crowdsource_rating",
ratingSchema,
"crowdsource_rating"
);
export type RatingType = InferSchemaType<typeof ratingSchema>;

0 comments on commit 7cc3134

Please sign in to comment.