refactor: removed some functions and added toggleStatus to checklistitem
This commit is contained in:
@@ -1,12 +1,12 @@
|
|||||||
import { Controller, Get, Post, Patch, Delete, Param, Body, Query } from "@nestjs/common";
|
import { Controller, Post, Patch, Delete, Param, Body } from "@nestjs/common";
|
||||||
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiQuery } from "@nestjs/swagger";
|
import { ApiTags, ApiOperation, ApiResponse, ApiParam } from "@nestjs/swagger";
|
||||||
import { CheckListItemService } from "../providers/check-list-item.service";
|
import { CheckListItemService } from "../providers/check-list-item.service";
|
||||||
import { CreateCheckListItemDto } from "../dto/check-list-item/create-check-list-item.dto";
|
import { CreateCheckListItemDto } from "../dto/check-list-item/create-check-list-item.dto";
|
||||||
import { UpdateCheckListItemDto } from "../dto/check-list-item/update-check-list-item.dto";
|
import { UpdateCheckListItemDto } from "../dto/check-list-item/update-check-list-item.dto";
|
||||||
import { CursorPaginationDto } from "../../../common/DTO/cursor-pagination.dto";
|
|
||||||
import { TMCheckListItem } from "../entities/check-list-item.entity";
|
import { TMCheckListItem } from "../entities/check-list-item.entity";
|
||||||
import { AuthGuards } from "../../../common/decorators/auth-guard.decorator";
|
import { AuthGuards } from "../../../common/decorators/auth-guard.decorator";
|
||||||
import { AdminRoute } from "../../../common/decorators/admin.decorator";
|
import { AdminRoute } from "../../../common/decorators/admin.decorator";
|
||||||
|
import { ParamDto } from "../../../common/DTO/param.dto";
|
||||||
|
|
||||||
@AuthGuards()
|
@AuthGuards()
|
||||||
@AdminRoute()
|
@AdminRoute()
|
||||||
@@ -15,26 +15,6 @@ import { AdminRoute } from "../../../common/decorators/admin.decorator";
|
|||||||
export class CheckListItemController {
|
export class CheckListItemController {
|
||||||
constructor(private readonly checkListItemService: CheckListItemService) {}
|
constructor(private readonly checkListItemService: CheckListItemService) {}
|
||||||
|
|
||||||
@Get()
|
|
||||||
@ApiOperation({ summary: "Get all check list items, optionally filtered by task" })
|
|
||||||
@ApiQuery({ name: "taskId", required: false, description: "Filter check list items by task ID" })
|
|
||||||
@ApiQuery({ name: "cursor", required: false, description: "Cursor for pagination (createdAt ISO string)" })
|
|
||||||
@ApiQuery({ name: "limit", required: false, description: "Number of records to return (max 50)" })
|
|
||||||
@ApiResponse({ status: 200, description: "Cursor-paginated list of check list items" })
|
|
||||||
findAll(@Query() pagination: CursorPaginationDto, @Query("taskId") taskId?: string) {
|
|
||||||
if (taskId) return this.checkListItemService.findByTask(taskId, pagination);
|
|
||||||
return this.checkListItemService.findAll(pagination);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Get(":id")
|
|
||||||
@ApiOperation({ summary: "Get a check list item by ID" })
|
|
||||||
@ApiParam({ name: "id", description: "CheckListItem ID" })
|
|
||||||
@ApiResponse({ status: 200, description: "CheckListItem found", type: TMCheckListItem })
|
|
||||||
@ApiResponse({ status: 404, description: "CheckListItem not found" })
|
|
||||||
findOne(@Param("id") id: string): Promise<TMCheckListItem> {
|
|
||||||
return this.checkListItemService.findOneOrFail(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Post()
|
@Post()
|
||||||
@ApiOperation({ summary: "Create a new check list item" })
|
@ApiOperation({ summary: "Create a new check list item" })
|
||||||
@ApiResponse({ status: 201, description: "CheckListItem created", type: TMCheckListItem })
|
@ApiResponse({ status: 201, description: "CheckListItem created", type: TMCheckListItem })
|
||||||
@@ -60,4 +40,13 @@ export class CheckListItemController {
|
|||||||
remove(@Param("id") id: string): Promise<TMCheckListItem> {
|
remove(@Param("id") id: string): Promise<TMCheckListItem> {
|
||||||
return this.checkListItemService.remove(id);
|
return this.checkListItemService.remove(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Patch("change-status/:id")
|
||||||
|
@ApiOperation({summary: "change status of a check list item."})
|
||||||
|
@ApiParam({name: "id", description: "CheckListItem ID"})
|
||||||
|
@ApiResponse({status: 200, description: "successfully changed the status!"})
|
||||||
|
@ApiResponse({ status: 404, description: "CheckListItem not found" })
|
||||||
|
changeStatus(@Param() paramDto: ParamDto) {
|
||||||
|
return this.checkListItemService.toggleStatus(paramDto.id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,9 +3,6 @@ import { CheckListItemRepository } from '../repositories/check-list-item.reposit
|
|||||||
import { TMCheckListItem } from '../entities/check-list-item.entity';
|
import { TMCheckListItem } from '../entities/check-list-item.entity';
|
||||||
import { CreateCheckListItemDto } from '../dto/check-list-item/create-check-list-item.dto';
|
import { CreateCheckListItemDto } from '../dto/check-list-item/create-check-list-item.dto';
|
||||||
import { UpdateCheckListItemDto } from '../dto/check-list-item/update-check-list-item.dto';
|
import { UpdateCheckListItemDto } from '../dto/check-list-item/update-check-list-item.dto';
|
||||||
import { CursorPaginationDto } from '../../../common/DTO/cursor-pagination.dto';
|
|
||||||
import { CursorPaginatedResult } from '../../../common/interfaces/cursor-paginated-result.interface';
|
|
||||||
import { buildCursorPageFormat } from '../../../common/helpers/cursor-paginated.helper';
|
|
||||||
import { checkListItemMessage } from '../../../common/enums/message.enum';
|
import { checkListItemMessage } from '../../../common/enums/message.enum';
|
||||||
import { TaskService } from './task.service';
|
import { TaskService } from './task.service';
|
||||||
|
|
||||||
@@ -16,27 +13,6 @@ export class CheckListItemService {
|
|||||||
private readonly taskService: TaskService,
|
private readonly taskService: TaskService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async findAll(pagination: CursorPaginationDto): Promise<CursorPaginatedResult<TMCheckListItem>> {
|
|
||||||
const limit = pagination.limit ?? 10;
|
|
||||||
const cursorDate = pagination.cursor ? new Date(pagination.cursor) : undefined;
|
|
||||||
|
|
||||||
const [data] = await this.checkListItemRepository.findAllItems(limit, cursorDate);
|
|
||||||
|
|
||||||
return buildCursorPageFormat(data, limit, cursorDate);
|
|
||||||
}
|
|
||||||
|
|
||||||
async findByTask(
|
|
||||||
taskId: string,
|
|
||||||
pagination: CursorPaginationDto,
|
|
||||||
): Promise<CursorPaginatedResult<TMCheckListItem>> {
|
|
||||||
const limit = pagination.limit ?? 10;
|
|
||||||
const cursorDate = pagination.cursor ? new Date(pagination.cursor) : undefined;
|
|
||||||
|
|
||||||
const [data] = await this.checkListItemRepository.findByTask(taskId, limit, cursorDate);
|
|
||||||
|
|
||||||
return buildCursorPageFormat(data, limit, cursorDate);
|
|
||||||
}
|
|
||||||
|
|
||||||
async findOneOrFail(id: string): Promise<TMCheckListItem> {
|
async findOneOrFail(id: string): Promise<TMCheckListItem> {
|
||||||
const checkListItem = await this.checkListItemRepository.findOneById(id);
|
const checkListItem = await this.checkListItemRepository.findOneById(id);
|
||||||
if (!checkListItem) throw new NotFoundException(checkListItemMessage.CHECKLISTITEM_NOT_FOUND);
|
if (!checkListItem) throw new NotFoundException(checkListItemMessage.CHECKLISTITEM_NOT_FOUND);
|
||||||
@@ -66,4 +42,13 @@ export class CheckListItemService {
|
|||||||
await this.checkListItemRepository.delete(id);
|
await this.checkListItemRepository.delete(id);
|
||||||
return checkListItem;
|
return checkListItem;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async toggleStatus(id: string) : Promise<TMCheckListItem> {
|
||||||
|
const checkListItem = await this.findOneOrFail(id);
|
||||||
|
const isDone = checkListItem.isDone;
|
||||||
|
checkListItem.isDone = !isDone;
|
||||||
|
await this.checkListItemRepository.save(checkListItem);
|
||||||
|
|
||||||
|
return checkListItem;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user