generated from Kan-A-Pesh/express-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from La-404-Devinci/feat/challenges
- Loading branch information
Showing
15 changed files
with
1,227 additions
and
857 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import DB from "@/database/config"; | ||
import { challenges } from "@/database/schema/challenges"; | ||
import { eq } from "drizzle-orm"; | ||
|
||
export default abstract class ChallengesController { | ||
public static async getAllChallengesWithDetails() { | ||
const allChallenges = await DB.instance | ||
.select({ | ||
id: challenges.id, | ||
name: challenges.name, | ||
score: challenges.score, | ||
clubId: challenges.clubId | ||
}) | ||
.from(challenges); | ||
|
||
return allChallenges; | ||
} | ||
|
||
public static async getChallenge(id: number) { | ||
const challenge = await DB.instance | ||
.select({ | ||
id: challenges.id, | ||
name: challenges.name, | ||
score: challenges.score | ||
}) | ||
.from(challenges) | ||
.where(eq(challenges.id, id)) | ||
.limit(1); | ||
|
||
return challenge.length ? challenge[0] : null; | ||
} | ||
|
||
public static async getChallengeWithDetails(id: number) { | ||
const challenge = await DB.instance | ||
.select({ | ||
id: challenges.id, | ||
name: challenges.name, | ||
score: challenges.score, | ||
clubId: challenges.clubId | ||
}) | ||
.from(challenges) | ||
.where(eq(challenges.id, id)); | ||
|
||
return challenge.length ? challenge[0] : null; | ||
} | ||
|
||
public static async createChallenge(clubId: number, score: number, name: string) { | ||
const challenge = await DB.instance | ||
.insert(challenges) | ||
.values({ | ||
clubId, | ||
score, | ||
name | ||
}) | ||
.returning({ | ||
id: challenges.id, | ||
name: challenges.name, | ||
score: challenges.score, | ||
clubId: challenges.clubId | ||
}); | ||
|
||
return challenge.length ? challenge[0] : null; | ||
} | ||
|
||
public static async updateChallenge(id: number, score?: number, name?: string) { | ||
const challenge = await DB.instance | ||
.update(challenges) | ||
.set({ | ||
score, | ||
name | ||
}) | ||
.where(eq(challenges.id, id)) | ||
.returning({ | ||
id: challenges.id, | ||
name: challenges.name, | ||
score: challenges.score, | ||
clubId: challenges.clubId | ||
}); | ||
|
||
return challenge.length ? challenge[0] : null; | ||
} | ||
|
||
public static async deleteChallenge(id: number) { | ||
await DB.instance.delete(challenges).where(eq(challenges.id, id)); | ||
} | ||
|
||
public static async getDailyChallenges() {} | ||
} |
Oops, something went wrong.