Skip to content

Commit

Permalink
Merge pull request #2 from La-404-Devinci/feat/clubs
Browse files Browse the repository at this point in the history
add clubs endpoints & tests
  • Loading branch information
Kan-A-Pesh authored Oct 29, 2024
2 parents 709daa2 + 51ba810 commit a031a40
Show file tree
Hide file tree
Showing 37 changed files with 1,512 additions and 521 deletions.
7 changes: 7 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ POSTGRES_DB=postgres
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_HOST=postgres

# Secrets
# The secret used to sign and verify JWT tokens
JWT_SECRET=secret

# Super-admin token
ADMIN_TOKEN=super_admin_token
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ This is a boilerplate for an ExpressJS server with a few basic features.

## Features

- 📜 Custom File & Console logging
- 📦 Environment variables
- 📝 Body parsing
- 🚀 Structured responses
- 🛑 Error handling
- 🐳 Docker managed
- 🧼 Prettier & ESLint config
- 🧪 Jest & Supertest testing
- 📜 Custom File & Console logging
- 📦 Environment variables
- 📝 Body parsing
- 🚀 Structured responses
- 🛑 Error handling
- 🐳 Docker managed
- 🧼 Prettier & ESLint config
- 🧪 Jest & Supertest testing

## Getting started

Expand Down
129 changes: 129 additions & 0 deletions controllers/clubs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import DB from "@/database/config";
import { clubs } from "@/database/schema/clubs";
import Logger from "@/log/logger";
import { eq } from "drizzle-orm";

export default abstract class ClubController {
public static async getAllClubs() {
const allClubs = await DB.instance
.select({
avatarUrl: clubs.avatarUrl,
name: clubs.name
})
.from(clubs)
.orderBy(clubs.name);

return allClubs;
}

public static async getAllClubsWithDetails() {
const allClubs = await DB.instance
.select({
id: clubs.id,
avatarUrl: clubs.avatarUrl,
name: clubs.name,
description: clubs.description,
dailyDate: clubs.dailyDate
})
.from(clubs)
.orderBy(clubs.name);

return allClubs;
}

public static async getClub(id: number) {
const club = await DB.instance
.select({
avatarUrl: clubs.avatarUrl,
name: clubs.name
})
.from(clubs)
.where(eq(clubs.id, id))
.limit(1);

return club.length ? club[0] : null;
}

public static async getClubWithDetails(id: number) {
const club = await DB.instance
.select({
id: clubs.id,
avatarUrl: clubs.avatarUrl,
name: clubs.name,
description: clubs.description,
dailyDate: clubs.dailyDate
})
.from(clubs)
.where(eq(clubs.id, id))
.limit(1);

return club.length ? club[0] : null;
}

public static async getDailyClub() {
const club = await DB.instance
.select({
avatarUrl: clubs.avatarUrl,
name: clubs.name,
description: clubs.description
})
.from(clubs)
.where(eq(clubs.dailyDate, new Date()))
.limit(1);

return club.length ? club[0] : null;
}

public static async createClub(name: string, avatarUrl: string, description?: string, dailyDate?: Date) {
try {
const club = await DB.instance
.insert(clubs)
.values({
name: name,
avatarUrl: avatarUrl,
description: description,
dailyDate: dailyDate
})
.returning();

return club[0];
} catch (error) {
Logger.error(error);
return null;
}
}

public static async updateClub(
id: number,
name: string,
avatarUrl: string,
description?: string,
dailyDate?: Date
) {
try {
const club = await DB.instance
.update(clubs)
.set({
name: name,
avatarUrl: avatarUrl,
description: description,
dailyDate: dailyDate
})
.where(eq(clubs.id, id))
.returning();

return club[0];
} catch (error) {
Logger.error(error);
return null;
}
}

public static async deleteClub(id: number) {
await DB.instance.delete(clubs).where(eq(clubs.id, id));
}

public static async deleteAllClubs() {
await DB.instance.delete(clubs);
}
}
2 changes: 2 additions & 0 deletions database/migrations/0001_nosy_maestro.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE "clubs" ALTER COLUMN "description" DROP NOT NULL;--> statement-breakpoint
ALTER TABLE "clubs" ALTER COLUMN "daily_date" DROP NOT NULL;
Loading

0 comments on commit a031a40

Please sign in to comment.