Skip to content

Commit

Permalink
Merge pull request #5 from La-404-Devinci/feat/challenges
Browse files Browse the repository at this point in the history
  • Loading branch information
Drixares authored Nov 2, 2024
2 parents d1140e9 + b272994 commit e7b95fb
Show file tree
Hide file tree
Showing 15 changed files with 1,227 additions and 857 deletions.
88 changes: 88 additions & 0 deletions controllers/challenges.ts
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() {}
}
Loading

0 comments on commit e7b95fb

Please sign in to comment.