From 82cddae001618d58e4def196ac443ee5da6cc780 Mon Sep 17 00:00:00 2001 From: Ignas-rgb <61393113+Ignas-rgb@users.noreply.github.com> Date: Fri, 2 Feb 2024 16:33:17 +0200 Subject: [PATCH] Update endpoints to allow forest data posting and fetching Endpoints, DTOs updated with 'type' parameter, to specify what kind of reports is being created/fetched --- src/admin/admin.controller.ts | 3 ++- src/admin/admin.service.ts | 4 ++-- src/report/dto/create-report.dto.ts | 3 +++ src/report/report.controller.ts | 11 +++++++---- src/report/report.service.ts | 8 ++++---- src/repositories/reports/report.repository.ts | 13 +++++++------ 6 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/admin/admin.controller.ts b/src/admin/admin.controller.ts index e8f0777..39c6da4 100644 --- a/src/admin/admin.controller.ts +++ b/src/admin/admin.controller.ts @@ -47,10 +47,11 @@ export class AdminController { @Get('/reports') async getAllReports( @Query('isDeleted', ParseBoolPipe) isDeleted: boolean, + @Query('type') type: string, ): Promise { return isDeleted ? await this.adminService.getAllDeletedReports() - : await this.adminService.getAllReports(); + : await this.adminService.getAllReports(type); } @ApiOkResponse({ diff --git a/src/admin/admin.service.ts b/src/admin/admin.service.ts index 20d41a7..4c0dd80 100644 --- a/src/admin/admin.service.ts +++ b/src/admin/admin.service.ts @@ -22,8 +22,8 @@ export class AdminService { private readonly reportRepository: ReportRepository, ) {} - async getAllReports(): Promise { - const reports = await this.reportRepository.getAllReports(); + async getAllReports(type: string): Promise { + const reports = await this.reportRepository.getAllReports(type); return reports.map(AdminService.docToFullReport); } diff --git a/src/report/dto/create-report.dto.ts b/src/report/dto/create-report.dto.ts index 9f91684..9b59269 100644 --- a/src/report/dto/create-report.dto.ts +++ b/src/report/dto/create-report.dto.ts @@ -11,6 +11,9 @@ export class CreateReportDto { @IsLatitude() latitude: number; + @IsNotEmpty() + type: string; + @IsEmail() email: string; diff --git a/src/report/report.controller.ts b/src/report/report.controller.ts index 9d20c2d..e72561c 100644 --- a/src/report/report.controller.ts +++ b/src/report/report.controller.ts @@ -6,6 +6,7 @@ import { Param, ParseIntPipe, Post, + Query, UploadedFiles, UseInterceptors, } from '@nestjs/common'; @@ -46,8 +47,8 @@ export class ReportController { type: [PublicReportDto], }) @Get() - getAllPublicReports(): Promise { - return this.reportService.getAllVisibleReports(); + getAllPublicReports(@Query('type') type: string): Promise { + return this.reportService.getAllVisibleReports(type); } @ApiOkResponse({ @@ -55,8 +56,10 @@ export class ReportController { type: ReportStatisticsDto, }) @Get('/statistics') - getReportStatistics(): Promise { - return this.reportService.getReportStatistics(); + getReportStatistics( + @Query('type') type: string, + ): Promise { + return this.reportService.getReportStatistics(type); } @Get('/:refId') diff --git a/src/report/report.service.ts b/src/report/report.service.ts index 77ab2a7..4c8de87 100644 --- a/src/report/report.service.ts +++ b/src/report/report.service.ts @@ -8,8 +8,8 @@ import { ReportStatisticsDto } from './dto/report-statistics.dto'; export class ReportService { constructor(private readonly reportRepository: ReportRepository) {} - async getAllVisibleReports(): Promise { - const reports = await this.reportRepository.getVisibleReports(); + async getAllVisibleReports(type: string): Promise { + const reports = await this.reportRepository.getVisibleReports(type); return reports.map(ReportService.docToPublicReport); } @@ -30,8 +30,8 @@ export class ReportService { return ReportService.docToPublicReport(report); } - async getReportStatistics(): Promise { - const stats = await this.reportRepository.getVisibleStatusCounts(); + async getReportStatistics(type: string): Promise { + const stats = await this.reportRepository.getVisibleStatusCounts(type); return ReportService.docToReportStatistics(stats); } diff --git a/src/repositories/reports/report.repository.ts b/src/repositories/reports/report.repository.ts index 3cdfcb6..6abf560 100644 --- a/src/repositories/reports/report.repository.ts +++ b/src/repositories/reports/report.repository.ts @@ -14,16 +14,16 @@ export class ReportRepository { private cloudinary: CloudinaryService, ) {} - async getVisibleReports(): Promise { + async getVisibleReports(type: string): Promise { return await this.reportModel - .find({ isVisible: true, isDeleted: false }) + .find({ isVisible: true, isDeleted: false, type: type }) .sort({ reportDate: -1 }) .exec(); } - async getAllReports(): Promise { + async getAllReports(type: string): Promise { return await this.reportModel - .find({ isDeleted: false }) + .find({ isDeleted: false, type: type }) .sort({ reportDate: -1 }) .exec(); } @@ -39,13 +39,14 @@ export class ReportRepository { return await this.reportModel.findOne({ refId: refId }).exec(); } - async getVisibleStatusCounts(): Promise { + async getVisibleStatusCounts(type: string): Promise { return await this.reportModel .aggregate([ { $match: { isDeleted: false, isVisible: true, + type: type, }, }, { @@ -72,7 +73,7 @@ export class ReportRepository { const reportCount = reports.length; const newReport = new this.reportModel({ name: createReport.name, - type: 'trash', + type: createReport.type, refId: reportCount + 1, comment: ' ', reportLong: createReport.longitude,