All files / src/todos todo.controller.ts

100% Statements 40/40
100% Branches 0/0
100% Functions 9/9
100% Lines 38/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 2961x                               1x                       1x 1x 1x 1x           1x 1x 1x           1x 16x                                                       1x       2x 2x 2x   2x 1x                                                                   1x               2x             2x 2x                   1x 1x 1x                                                                           1x       3x 1x                                               1x         2x 1x                                           1x       2x 1x                                         1x       2x                             1x     2x 2x            
import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Put,
  Query,
  ValidationPipe,
  UseGuards,
  ParseBoolPipe,
  HttpCode,
  HttpStatus,
} from "@nestjs/common";
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiBearerAuth,
  ApiQuery,
  ApiParam,
  ApiBadRequestResponse,
  ApiUnauthorizedResponse,
  ApiNotFoundResponse,
  ApiForbiddenResponse,
} from "@nestjs/swagger";
import { TodoService } from "./todo.service";
import { CreateTodoDto } from "./dto/create-todo.dto";
import { UpdateTodoDto } from "./dto/update-todo.dto";
import {
  TodoResponseDto,
  TodoListResponseDto,
  TodoStatsResponseDto,
  DeleteTodoResponseDto,
} from "./dto/todo-response.dto";
import { JwtAuthGuard } from "../auth/guards/jwt-auth.guard";
import { CurrentUser } from "../auth/decorators/current-user.decorator";
import { User } from "../users/user.entity";
 
@ApiTags("todos")
@Controller("todos")
@UseGuards(JwtAuthGuard)
@ApiBearerAuth("JWT-auth")
export class TodoController {
  constructor(private readonly todoService: TodoService) {}
 
  @Post()
  @ApiOperation({ summary: "새 할일 생성" })
  @ApiResponse({
    status: 201,
    description: "할일이 성공적으로 생성되었습니다",
    type: TodoResponseDto,
  })
  @ApiBadRequestResponse({
    description: "잘못된 입력 데이터",
    schema: {
      properties: {
        statusCode: { type: "number", example: 400 },
        message: { type: "array", items: { type: "string" } },
        error: { type: "string", example: "Bad Request" },
      },
    },
  })
  @ApiUnauthorizedResponse({
    description: "인증되지 않은 사용자",
    schema: {
      properties: {
        statusCode: { type: "number", example: 401 },
        message: { type: "string", example: "Unauthorized" },
      },
    },
  })
  async create(
    @Body() createTodoDto: CreateTodoDto,
    @CurrentUser() user: User,
  ): Promise<TodoResponseDto> {
    console.log("=== Todo Create Controller ===");
    console.log("User ID:", user.id);
    console.log("Create Todo DTO:", JSON.stringify(createTodoDto, null, 2));
 
    const todo = await this.todoService.create(createTodoDto, user.id);
    return { todo };
  }
 
  @Get()
  @ApiOperation({ summary: "할일 목록 조회" })
  @ApiResponse({
    status: 200,
    description: "할일 목록이 성공적으로 조회되었습니다",
    type: TodoListResponseDto,
  })
  @ApiQuery({
    name: "startDate",
    required: false,
    description: "시작 날짜 (ISO 8601 형식)",
    example: "2024-01-01T00:00:00.000Z",
  })
  @ApiQuery({
    name: "endDate",
    required: false,
    description: "종료 날짜 (ISO 8601 형식)",
    example: "2024-01-31T23:59:59.999Z",
  })
  @ApiQuery({
    name: "categoryId",
    required: false,
    description: "카테고리 ID로 필터링",
    example: "work",
  })
  @ApiQuery({
    name: "completed",
    required: false,
    description: "완료 상태로 필터링",
    example: true,
  })
  async findAll(
    @CurrentUser() user: User,
    @Query("startDate") startDate?: string,
    @Query("endDate") endDate?: string,
    @Query("categoryId") categoryId?: string,
    @Query("completed", new ParseBoolPipe({ optional: true }))
    completed?: boolean,
  ): Promise<TodoListResponseDto> {
    const todos = await this.todoService.findAll(
      user.id,
      startDate,
      endDate,
      categoryId,
      completed,
    );
    const stats = await this.todoService.getStats(user.id);
    return { todos, stats };
  }
 
  @Get("stats")
  @ApiOperation({ summary: "할일 통계 조회" })
  @ApiResponse({
    status: 200,
    description: "할일 통계가 성공적으로 조회되었습니다",
    type: TodoStatsResponseDto,
  })
  async getStats(@CurrentUser() user: User): Promise<TodoStatsResponseDto> {
    const stats = await this.todoService.getStats(user.id);
    return { stats };
  }
 
  @Get(":id")
  @ApiOperation({ summary: "특정 할일 조회" })
  @ApiParam({
    name: "id",
    description: "할일 ID",
    example: "abc123",
  })
  @ApiResponse({
    status: 200,
    description: "할일이 성공적으로 조회되었습니다",
    type: TodoResponseDto,
  })
  @ApiNotFoundResponse({
    description: "할일을 찾을 수 없습니다",
    schema: {
      properties: {
        statusCode: { type: "number", example: 404 },
        message: { type: "string", example: "할일을 찾을 수 없습니다" },
        error: { type: "string", example: "Not Found" },
      },
    },
  })
  @ApiForbiddenResponse({
    description: "해당 할일에 접근할 권한이 없습니다",
    schema: {
      properties: {
        statusCode: { type: "number", example: 403 },
        message: {
          type: "string",
          example: "해당 할일에 접근할 권한이 없습니다",
        },
        error: { type: "string", example: "Forbidden" },
      },
    },
  })
  async findOne(
    @Param("id") id: string,
    @CurrentUser() user: User,
  ): Promise<TodoResponseDto> {
    const todo = await this.todoService.findOne(id, user.id);
    return { todo };
  }
 
  @Put(":id")
  @ApiOperation({ summary: "할일 수정" })
  @ApiParam({
    name: "id",
    description: "할일 ID",
    example: "abc123",
  })
  @ApiResponse({
    status: 200,
    description: "할일이 성공적으로 수정되었습니다",
    type: TodoResponseDto,
  })
  @ApiBadRequestResponse({
    description: "잘못된 입력 데이터",
  })
  @ApiNotFoundResponse({
    description: "할일을 찾을 수 없습니다",
  })
  @ApiForbiddenResponse({
    description: "해당 할일을 수정할 권한이 없습니다",
  })
  async update(
    @Param("id") id: string,
    @Body(ValidationPipe) updateTodoDto: UpdateTodoDto,
    @CurrentUser() user: User,
  ): Promise<TodoResponseDto> {
    const todo = await this.todoService.update(id, updateTodoDto, user.id);
    return { todo };
  }
 
  @Patch(":id/toggle")
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: "할일 완료 상태 토글" })
  @ApiParam({
    name: "id",
    description: "할일 ID",
    example: "abc123",
  })
  @ApiResponse({
    status: 200,
    description: "할일 완료 상태가 성공적으로 토글되었습니다",
    type: TodoResponseDto,
  })
  @ApiNotFoundResponse({
    description: "할일을 찾을 수 없습니다",
  })
  @ApiForbiddenResponse({
    description: "해당 할일을 수정할 권한이 없습니다",
  })
  async toggle(
    @Param("id") id: string,
    @CurrentUser() user: User,
  ): Promise<TodoResponseDto> {
    const todo = await this.todoService.toggle(id, user.id);
    return { todo };
  }
 
  @Delete(":id")
  @ApiOperation({ summary: "할일 삭제" })
  @ApiParam({
    name: "id",
    description: "할일 ID",
    example: "abc123",
  })
  @ApiResponse({
    status: 200,
    description: "할일이 성공적으로 삭제되었습니다",
    type: DeleteTodoResponseDto,
  })
  @ApiNotFoundResponse({
    description: "할일을 찾을 수 없습니다",
  })
  @ApiForbiddenResponse({
    description: "해당 할일을 삭제할 권한이 없습니다",
  })
  async remove(
    @Param("id") id: string,
    @CurrentUser() user: User,
  ): Promise<DeleteTodoResponseDto> {
    return await this.todoService.remove(id, user.id);
  }
 
  @Delete()
  @ApiOperation({ summary: "모든 할일 삭제" })
  @ApiResponse({
    status: 200,
    description: "모든 할일이 성공적으로 삭제되었습니다",
    schema: {
      properties: {
        deletedCount: { type: "number", example: 5 },
        message: { type: "string", example: "모든 할일이 삭제되었습니다" },
      },
    },
  })
  async removeAll(
    @CurrentUser() user: User,
  ): Promise<{ deletedCount: number; message: string }> {
    const deletedCount = await this.todoService.removeAllByUserId(user.id);
    return {
      deletedCount,
      message: "모든 할일이 삭제되었습니다",
    };
  }
}