Skip to content

Commit

Permalink
Merge pull request #66 from poojakarma/Search_API
Browse files Browse the repository at this point in the history
PS -2114,20015 -> search API
  • Loading branch information
snehal0904 authored Oct 7, 2024
2 parents bafbf94 + f22fc1c commit 0f7e81d
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 3 deletions.
39 changes: 37 additions & 2 deletions src/academicyears/academicyears.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Body, Controller, Post, Res, Headers, UsePipes, ValidationPipe, UseFilters, BadRequestException } from '@nestjs/common';
import { ApiBody, ApiCreatedResponse, ApiHeader, ApiTags } from '@nestjs/swagger';
import { Body, Controller, Post, Res, Headers, UsePipes, ValidationPipe, UseFilters, BadRequestException, Get, Param, ParseUUIDPipe, UseGuards } from '@nestjs/common';
import { ApiBasicAuth, ApiBody, ApiCreatedResponse, ApiHeader, ApiInternalServerErrorResponse, ApiResponse, ApiTags } from '@nestjs/swagger';
import { Response } from "express";
import { AcademicYearDto } from './dto/academicyears-create.dto';
import { AcademicYearAdapter } from './academicyearsadaptor';
Expand All @@ -8,16 +8,20 @@ import { APIID } from '@utils/api-id.config';
import { API_RESPONSES } from '@utils/response.messages';
import { DateValidationPipe } from 'src/common/pipes/date-validation.pipe';
import { isUUID } from 'class-validator';
import { AcademicYearSearchDto } from './dto/academicyears-search.dto';
import { JwtAuthGuard } from 'src/common/guards/keycloak.guard';


@ApiTags("Academicyears")
@Controller('academicyears')
@UseGuards(JwtAuthGuard)
export class AcademicyearsController {

constructor(private readonly academicYearAdapter: AcademicYearAdapter) { }

@UseFilters(new AllExceptionsFilter(APIID.ACADEMICYEAR_CREATE))
@Post('/create')
@ApiBasicAuth("access-token")
@UsePipes(new ValidationPipe({ transform: true }), new DateValidationPipe())
@ApiBody({ type: AcademicYearDto })
@ApiCreatedResponse({ description: API_RESPONSES.ACADEMICYEAR })
Expand All @@ -32,4 +36,35 @@ export class AcademicyearsController {
let result = await this.academicYearAdapter.buildAcademicYears().createAcademicYear(academicyearsService, tenantId, response)
return response.status(result.statusCode).json(result);
}

@UseFilters(new AllExceptionsFilter(APIID.ACADEMICYEAR_LIST))
@Post('/list')
@ApiBasicAuth("access-token")
@UsePipes(new ValidationPipe({ transform: true }))
@ApiHeader({ name: "tenantid" })
@ApiBody({ type: AcademicYearSearchDto })
@ApiCreatedResponse({ description: API_RESPONSES.ACADEMICYEAR })
async getAcademicYearList(@Body() academicYearSearchDto: AcademicYearSearchDto, @Res() response: Response, @Headers() headers) {
const tenantId = headers["tenantid"];
if (!tenantId || !isUUID(tenantId)) {
throw new BadRequestException(API_RESPONSES.TENANTID_VALIDATION);
}
let result = await this.academicYearAdapter.buildAcademicYears().getAcademicYearList(academicYearSearchDto, tenantId, response)
return response.status(result.statusCode).json(result);
}

@UseFilters(new AllExceptionsFilter(APIID.ACADEMICYEAR_GET))
@Get('/:id')
@ApiBasicAuth("access-token")
@ApiResponse({ status: 200, description: API_RESPONSES.ACADEMICYEAR_GET_SUCCESS })
@ApiInternalServerErrorResponse({
description: API_RESPONSES.INTERNAL_SERVER_ERROR,
})
async getAcademicYearById(
@Param('id', new ParseUUIDPipe()) id: string,
@Res() response: Response,
) {
let result = await this.academicYearAdapter.buildAcademicYears().getAcademicYearById(id, response)
return response.status(result.statusCode).json(result);
}
}
10 changes: 10 additions & 0 deletions src/academicyears/dto/academicyears-search.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsBoolean, IsOptional } from 'class-validator';

export class AcademicYearSearchDto {

@ApiProperty({ description: 'isActive', example: true })
@IsOptional()
@IsBoolean()
isActive?: boolean;
}
3 changes: 3 additions & 0 deletions src/adapters/academicyearsservicelocater.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { AcademicYearDto } from "src/academicyears/dto/academicyears-create.dto";
import { Response } from "express";
import { AcademicYearSearchDto } from "src/academicyears/dto/academicyears-search.dto";

export interface IServicelocatorAcademicyear {
createAcademicYear(academicYearDto: AcademicYearDto, tenantId, response: Response): Promise<any>;
getAcademicYearList(academicYearSearchDto: AcademicYearSearchDto, tenantId, response: Response)
getAcademicYearById(id, response: Response)
}
72 changes: 72 additions & 0 deletions src/adapters/postgres/academicyears-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Repository } from "typeorm";
import { API_RESPONSES } from "@utils/response.messages";
import { APIID } from "@utils/api-id.config";
import APIResponse from "src/common/responses/response";
import { AcademicYearSearchDto } from "src/academicyears/dto/academicyears-search.dto";

@Injectable()
export class PostgresAcademicYearService implements IServicelocatorAcademicyear {
Expand Down Expand Up @@ -79,4 +80,75 @@ export class PostgresAcademicYearService implements IServicelocatorAcademicyear
}
return false;
}

async getAcademicYearList(academicYearSearchDto: AcademicYearSearchDto, tenantId, response: Response) {
const apiId = APIID.ACADEMICYEAR_LIST;
try {
// Fetch academic years based on active status or just tenantId
const searchCriteria = { tenantId };
if (academicYearSearchDto.isActive !== undefined) {
searchCriteria['isActive'] = academicYearSearchDto.isActive;
}
const academicYearList = await this.academicYearRespository.find({ where: searchCriteria });

// Check if the list is empty
if (academicYearList.length === 0) {
return APIResponse.error(
response,
apiId,
API_RESPONSES.ACADEMICYEAR_NOTFOUND,
API_RESPONSES.NOT_FOUND,
HttpStatus.BAD_REQUEST
);
}
return APIResponse.success(
response,
apiId,
academicYearList,
HttpStatus.OK,
API_RESPONSES.ACADEMICYEAR
);

} catch (error) {
const errorMessage = error.message || API_RESPONSES.INTERNAL_SERVER_ERROR;
return APIResponse.error(
response,
apiId,
API_RESPONSES.INTERNAL_SERVER_ERROR,
errorMessage,
HttpStatus.INTERNAL_SERVER_ERROR
);
}
}
async getAcademicYearById(id, response) {
const apiId = APIID.ACADEMICYEAR_GET;
try {
const academicYearResult = await this.academicYearRespository.findOne({ where: { id: id } });
if (!academicYearResult) {
return APIResponse.error(
response,
apiId,
API_RESPONSES.ACADEMICYEAR_NOTFOUND,
API_RESPONSES.NOT_FOUND,
HttpStatus.BAD_REQUEST
);
}
return APIResponse.success(
response,
apiId,
academicYearResult,
HttpStatus.OK,
API_RESPONSES.ACADEMICYEAR
);
} catch (error) {
const errorMessage = error.message || API_RESPONSES.INTERNAL_SERVER_ERROR;
return APIResponse.error(
response,
apiId,
API_RESPONSES.INTERNAL_SERVER_ERROR,
errorMessage,
HttpStatus.INTERNAL_SERVER_ERROR
);
}
}
}
4 changes: 3 additions & 1 deletion src/common/utils/api-id.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,7 @@ export const APIID = {
REFRESH: "api.refresh",
USER_AUTH: "api.user.auth",
RBAC_TOKEN: "api.rbac.token",
ACADEMICYEAR_CREATE: 'api.academicyear.create'
ACADEMICYEAR_CREATE: 'api.academicyear.create',
ACADEMICYEAR_LIST: 'api.academicyear.list',
ACADEMICYEAR_GET: 'api.academicyear.get'
}

0 comments on commit 0f7e81d

Please sign in to comment.