-
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.
feat: introduced question and answer api
- Loading branch information
1 parent
adfb775
commit 485bf53
Showing
8 changed files
with
868 additions
and
24 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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 |
---|---|---|
@@ -1,18 +1,106 @@ | ||
import { Controller, Req } from '@nestjs/common'; | ||
import { Controller, Req, Body, Param, UsePipes, UseGuards, ForbiddenException, ConflictException } from '@nestjs/common'; | ||
import { Api, initNestServer } from '@ts-rest/nest'; | ||
import { ZodValidationPipe } from 'nestjs-zod'; | ||
import { Request } from 'express'; | ||
import { ApiQueryFromZod } from '../decorators/api-query-from-zod'; | ||
import { ApiOkResponseFromZod } from '../decorators/api-response-from-zod'; | ||
import { apiHome } from 'libs/contracts/src/lib/contract-home'; | ||
import { HomeService } from './home.service'; | ||
import { | ||
QuestionAndAnswerQueryParams, | ||
ResponseQuestionAndAnswerSchemaWithRelations, | ||
ResponseQuestionAndAnswerSchema, | ||
CreateQuestionAndAnswerSchemaDto, | ||
UpdateQuestionAndAnswerSchemaDto | ||
} from 'libs/contracts/src/schema'; | ||
import { UserTokenValidation } from '../guards/user-token-validation.guard'; | ||
import { MembersService } from '../members/members.service'; | ||
import { NoCache } from '../decorators/no-cache.decorator'; | ||
import { PrismaQueryBuilder } from '../utils/prisma-query-builder'; | ||
import { prismaQueryableFieldsFromZod } from '../utils/prisma-queryable-fields-from-zod'; | ||
|
||
const server = initNestServer(apiHome); | ||
type RouteShape = typeof server.routeShapes; | ||
|
||
@Controller() | ||
export class HomeController { | ||
constructor(private homeService: HomeService) {} | ||
constructor( | ||
private homeService: HomeService, | ||
private memberService: MembersService | ||
) {} | ||
|
||
@Api(server.route.getAllFeaturedData) | ||
async getAllFeaturedData(@Req() request: Request) { | ||
async getAllFeaturedData() { | ||
return await this.homeService.fetchAllFeaturedData(); | ||
} | ||
|
||
@Api(server.route.getAllQuestionAndAnswers) | ||
@ApiQueryFromZod(QuestionAndAnswerQueryParams) | ||
@ApiOkResponseFromZod(ResponseQuestionAndAnswerSchemaWithRelations.array()) | ||
@NoCache() | ||
async getQuestionAndAnswers(@Req() request: Request) { | ||
const queryableFields = prismaQueryableFieldsFromZod( | ||
ResponseQuestionAndAnswerSchema | ||
); | ||
const builder = new PrismaQueryBuilder(queryableFields); | ||
const builtQuery = builder.build(request.query); | ||
return await this.homeService.fetchQuestionAndAnswers(builtQuery); | ||
} | ||
|
||
|
||
@Api(server.route.getQuestionAndAnswer) | ||
@ApiQueryFromZod(QuestionAndAnswerQueryParams) | ||
@ApiOkResponseFromZod(ResponseQuestionAndAnswerSchemaWithRelations) | ||
@NoCache() | ||
async getQuestionAndAnswer(@Param('slug') slug: string) | ||
{ | ||
return await this.homeService.fetchQuestionAndAnswerBySlug(slug); | ||
} | ||
|
||
@Api(server.route.createQuestionAndAnswer) | ||
@UsePipes(ZodValidationPipe) | ||
@UseGuards(UserTokenValidation) | ||
async addQuestionAndAnswer( | ||
@Body() questionAndAnswer: CreateQuestionAndAnswerSchemaDto, | ||
@Req() request | ||
) { | ||
const userEmail = request["userEmail"]; | ||
const member: any = await this.memberService.findMemberByEmail(userEmail); | ||
const result = await this.memberService.checkIfAdminUser(member); | ||
if (!result) { | ||
throw new ForbiddenException(`Member with email ${userEmail} isn't admin`); | ||
} | ||
return await this.homeService.createQuestionAndAnswer(questionAndAnswer as any, member); | ||
} | ||
|
||
@Api(server.route.updateQuestionAndAnswer) | ||
@UsePipes(ZodValidationPipe) | ||
@UseGuards(UserTokenValidation) | ||
async modifyQuestionAndAnswer( | ||
@Param('slug') slug: string, | ||
@Body() questionAndAnswer: UpdateQuestionAndAnswerSchemaDto, | ||
@Req() request | ||
) { | ||
const userEmail = request["userEmail"]; | ||
const member: any = await this.memberService.findMemberByEmail(userEmail); | ||
const result = await this.memberService.checkIfAdminUser(member); | ||
if (!result) { | ||
throw new ForbiddenException(`Member with email ${userEmail} isn't admin`); | ||
} | ||
return await this.homeService.updateQuestionAndAnswerBySlug(slug, questionAndAnswer as any, member); | ||
} | ||
|
||
@Api(server.route.updateQuestionAndAnswerViewCount) | ||
async modifyQuestionAndAnswerViewCount( | ||
@Param('slug') slug: string | ||
) { | ||
return await this.homeService.updateQuestionAndAnswerViewCount(slug); | ||
} | ||
|
||
@Api(server.route.updateQuestionAndAnswerShareCount) | ||
async modifyQuestionAndAnswerShareCount( | ||
@Param('slug') slug: string | ||
) { | ||
return await this.homeService.updateQuestionAndAnswerShareCount(slug); | ||
} | ||
} |
Oops, something went wrong.