Skip to content

Commit

Permalink
Update Contest
Browse files Browse the repository at this point in the history
  • Loading branch information
HUNG-rushb committed Dec 10, 2023
1 parent 752b9b1 commit 2d44f87
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 136 deletions.
70 changes: 38 additions & 32 deletions src/Type_Definitions/Contest_Contest.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ const contestDefs = gql`
allContests: [Contest]!
contestInfo(data: ContestInfoInput!): Contest!
contestPosts(
data: ContestPostsInpout
limit: Int
contestId: String
userId: String
after: String
): PostConnection!
getTopContestPosts(contestId: String, top: Int!): [Post]!
# getTopContestPosts(contestId: String, top: Int!): [Post]!
getTopContestPosts(data: ContestInfoInput!): [Post]!
# getContestPrizes
# getPrizes
# getContestPostScore
Expand All @@ -27,11 +28,12 @@ const contestDefs = gql`
# _______________________________________________________
extend type Mutation {
createPrize(data: CreatePrizeInput): Prize!
# createPrize(data: CreatePrizeInput): Prize!
# createPrizeSet
createContest(data: CreateContestInput!): Contest!
deleteContest(data: DeleteContestInput!): Contest!
joinContest(data: JoinContestInput!): Contest!
endContest(data: EndContestInput!): Contest!
# submitPostToContest(data: SubmitPostToContestInput): Contest_Score
# deletePostToContest(data: SubmitPostToContestInput): Contest_Score
# endContest
Expand All @@ -44,20 +46,18 @@ const contestDefs = gql`
# contestId: ID!
# }
input CreatePrizeInput {
name: String!
prizeImageURL: String!
}
# input CreatePrizeInput {
# name: String!
# prizeImageURL: String!
# }
input CreateContestInput {
name: String!
contestImageURL: String!
description: String!
startDate: Int!
endDate: Int
contestPrizeList: [CreateContestPrizeInput]!
startDate: String!
endDate: String
}
input CreateContestPrizeInput {
Expand All @@ -75,44 +75,50 @@ const contestDefs = gql`
userId: ID!
}
input EndContestInput {
contestId: ID!
}
type Contest {
id: ID!
name: String!
contestImageURL: String!
description: String!
startDate: Int!
endDate: Int
startDate: String!
endDate: String
isFinished: Boolean!
joinedUserList: [User]!
scores: [Contest_Score]!
joinedUserIds: [User]!
contestPrizeList: [Contest_Prize]!
}
type Contest_Score {
id: ID!
# type Contest_Score {
# id: ID!
contest: Contest!
user: User!
post: Post!
score: Int
}
# contest: Contest!
# user: User!
# post: Post!
# score: Int
# }
type Contest_Prize {
id: ID!
contest: Contest!
prize: Prize!
user: User
contestId: Contest!
userId: User!
title: String!
type: String!
}
type Prize {
id: ID!
name: String!
prizeImageURL: String!
contestPrizeList: [Contest_Prize]
}
# type Prize {
# id: ID!
# name: String!
# prizeImageURL: String!
# contestPrizeList: [Contest_Prize]
# }
`;

export default contestDefs;
Expand Down
3 changes: 3 additions & 0 deletions src/Type_Definitions/User_User.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ const userDefs = gql`
biography: String
userEndorsements: [Endorsement]
interestCategories: [Category]
joinedContestIds: [Contest]!
contestPrizeList: [Contest_Prize]!
}
${categoryDefs}
Expand Down
2 changes: 1 addition & 1 deletion src/prisma/ERD.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 33 additions & 28 deletions src/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ model User {
interestCategoryIds String[] @db.ObjectId
interestCategories Category[] @relation(name: "interest", fields: [interestCategoryIds], references: [id])
submissions Contest_Score[]
// submissions Contest_Score[]
contestPrizeList Contest_Prize[]
joinedContestIds String[] @db.ObjectId
Expand Down Expand Up @@ -251,7 +251,7 @@ model Post {
userLikedPost String[]
reportedUserIds String[]
submissions Contest_Score[]
// submissions Contest_Score[]
}

model Story {
Expand Down Expand Up @@ -308,61 +308,66 @@ model Contest {
contestImageURL String
description String
startDate DateTime
endDate DateTime?
startDate DateTime @default(now())
endDate DateTime
isFinished Boolean
joinedUserIds String[] @db.ObjectId
joinedUserList User[] @relation(fields: [joinedUserIds],references: [id])
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
scores Contest_Score[]
// scores Contest_Score[]
contestPrizeList Contest_Prize[]
}

model Contest_Prize {
id String @id @default(auto()) @map("_id") @db.ObjectId
contestId String @db.ObjectId
contest Contest @relation(fields: [contestId],references: [id])
// contestId String @db.ObjectId
// contest Contest @relation(fields: [contestId],references: [id])
prizeId String @db.ObjectId
prize Prize @relation(fields: [prizeId], references: [id])
// prizeId String @db.ObjectId
// prize Prize @relation(fields: [prizeId], references: [id])
contestId String @db.ObjectId
prize_to_contest Contest @relation(fields: [contestId], references: [id])
userId String? @db.ObjectId
user User? @relation(fields: [userId], references: [id])
title String
type String
prizeImageURL String
}

model Prize {
id String @id @default(auto()) @map("_id") @db.ObjectId
name String
prizeImageURL String
// model Prize {
// id String @id @default(auto()) @map("_id") @db.ObjectId
// name String
// prizeImageURL String

contestPrizeList Contest_Prize[]
}
// contestPrizeList Contest_Prize[]
// }

model Contest_Score {
id String @id @default(auto()) @map("_id") @db.ObjectId
// model Contest_Score {
// id String @id @default(auto()) @map("_id") @db.ObjectId

contestId String @db.ObjectId
contest Contest @relation(fields: [contestId], references: [id])
// contestId String @db.ObjectId
// contest Contest @relation(fields: [contestId], references: [id])

userId String @db.ObjectId
user User @relation(fields: [userId], references: [id])
// userId String @db.ObjectId
// user User @relation(fields: [userId], references: [id])

postId String? @db.ObjectId
post Post? @relation(fields: [postId], references: [id])
// postId String? @db.ObjectId
// post Post? @relation(fields: [postId], references: [id])

score Int?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
// score Int?
// createdAt DateTime @default(now())
// updatedAt DateTime @updatedAt

}
// }

// Chat Application
model Chat {
Expand Down
77 changes: 53 additions & 24 deletions src/resolvers/Mutation/contest.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ const contestMutation = {
* @param {*} info
* @returns
*/
createPrize: async (parent, args, info) => {
const { name, prizeImageURL } = args.data;
return prisma.prize.create({
data: {
name: name,
prizeImageURL: prizeImageURL,
},
});
},
// createPrize: async (parent, args, info) => {
// const { name, prizeImageURL } = args.data;
// return prisma.prize.create({
// data: {
// name: name,
// prizeImageURL: prizeImageURL,
// },
// });
// },

/**
* @param {*} parent
Expand All @@ -26,30 +26,59 @@ const contestMutation = {
*/
createContest: async (parent, args, info) => {
const { data } = args;
const { contestPrizeList } = data;

const result = await prisma.contest.create({
data: {
name: data.name,
contestImageURL: data.contestImageURL,
description: data.description,
isFinished: false,
startDate: new Date(data.startDate),
endDate: data.endDate ? new Date(data.endDate) : null,
contestPrizeList: {
create: contestPrizeList.map((each) => {
return {
prize: {
connect: {
id: each.prizeId,
},
},
type: each.type,
title: each.title,
};
}),
endDate: new Date(data.endDate),
},
});

return result;
},
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!
endContest: async (parent, args, info) => {
const { contestId } = args.data;

const result = await prisma.contest.update({
where: {
id: contestId,
},
data: {
isFinished: true,
},
});

const users = await prisma.post.findMany({
where: {
contestId: contestId,
},
orderBy: [
{ points: 'desc' },
{
createdAt: 'desc',
},
],
take: 3,
include: {
userId: {
id: true,
},
},
});

console.log({ users });

// await prisma.contest_Prize.create({
// data: {
// contestId: contestId,
// },
// });

return result;
},

Expand Down Expand Up @@ -77,7 +106,7 @@ const contestMutation = {
id: args.data.contestId,
},
data: {
userJoined: {
joinedUserIds: {
push: args.data.userId,
},
},
Expand Down
2 changes: 2 additions & 0 deletions src/resolvers/Mutation/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const userMutation = {
isAdmin: 0,
age: 18,
notiIds: [],
contestPrizeList: [],
level: {
create: {
currentXP: 0,
Expand Down Expand Up @@ -161,6 +162,7 @@ const userMutation = {
interestCategories: true,
},
});

if (!user) {
throw Error('User is not existed');
}
Expand Down
Loading

0 comments on commit 2d44f87

Please sign in to comment.