Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement getCampainById #7

Merged
merged 1 commit into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions package-lock.json

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

8 changes: 6 additions & 2 deletions src/prisma/prisma.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { Injectable, OnModuleInit } from '@nestjs/common';
import { Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';

@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
export class PrismaService extends PrismaClient implements OnModuleInit, OnModuleDestroy {

Check failure on line 5 in src/prisma/prisma.service.ts

View workflow job for this annotation

GitHub Actions / lint-and-build-backend

Replace `·extends·PrismaClient·implements·OnModuleInit,·OnModuleDestroy·` with `⏎··extends·PrismaClient⏎··implements·OnModuleInit,·OnModuleDestroy⏎`
async onModuleInit() {
await this.$connect();
}

async onModuleDestroy() {
await this.$disconnect();
}
}
24 changes: 23 additions & 1 deletion src/resolvers/campaign.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
import { Resolver } from '@nestjs/graphql';
import { Resolver, Query, Args } from '@nestjs/graphql';
import { PrismaService } from 'src/prisma/prisma.service';
import { Campaign } from './models/campaign.model';
import { NotFoundException, BadRequestException } from '@nestjs/common';

@Resolver(() => Campaign)
export class CampaignResolver {
constructor(private prismaService: PrismaService) {}

@Query(() => Campaign, { nullable: true })
async getCampaignById(@Args('id') id: number): Promise<Campaign | null> {
// Validate ID format (assuming UUID format)
//TODO: difine other ways to validate the id.
if (typeof id !== 'number') {
throw new BadRequestException('Invalid campaign ID format');
}

// Fetch campaign from database
const campaign = await this.prismaService.campaign.findUnique({
where: { id },
});

// Handle non-existent campaign
if (!campaign) {
throw new NotFoundException(`Campaign with ID ${id} not found`);
}

return campaign;
}
}
Loading