# Code coverage report for modules/bgm/bgm.controller.ts

# All files / modules/bgm bgm.controller.ts

0% Statements 0/132
0% Branches 0/1
0% Functions 0/1
0% Lines 0/132

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

     import Router from '@koa/router';
    import { bgmService } from './bgm.service';

    const router = new Router();

    /**
     * 获取背景音乐列表
     * GET /api/bgm
     */
    router.get('/', async (ctx) => {
      try {
        const { mood } = ctx.query as { mood?: string };

        let bgmList;
        if (mood) {
          bgmList = bgmService.getBgmByMood(mood);
        } else {
          bgmList = bgmService.getBgmList();
        }

        ctx.body = {
          code: 0,
          message: 'success',
          data: bgmList,
        };
      } catch (error: any) {
        ctx.body = {
          code: 400,
          message: error.message || '获取背景音乐列表失败',
        };
      }
    });

    /**
     * 获取所有风格
     * GET /api/bgm/moods
     */
    router.get('/moods', async (ctx) => {
      try {
        const moods = bgmService.getMoods();
        ctx.body = {
          code: 0,
          message: 'success',
          data: moods,
        };
      } catch (error: any) {
        ctx.body = {
          code: 400,
          message: error.message || '获取风格列表失败',
        };
      }
    });

    /**
     * 获取单个背景音乐
     * GET /api/bgm/:id
     */
    router.get('/:id', async (ctx) => {
      try {
        const { id } = ctx.params;
        const bgm = bgmService.getBgmById(parseInt(id));

        if (!bgm) {
          ctx.body = {
            code: 404,
            message: '背景音乐不存在',
          };
          return;
        }

        ctx.body = {
          code: 0,
          message: 'success',
          data: bgm,
        };
      } catch (error: any) {
        ctx.body = {
          code: 400,
          message: error.message || '获取背景音乐失败',
        };
      }
    });

    /**
     * 添加背景音乐
     * POST /api/bgm
     */
    router.post('/', async (ctx) => {
      try {
        const { name, url, mood, duration } = ctx.request.body as {
          name: string;
          url: string;
          mood: string;
          duration: number;
        };

        if (!name || !url || !mood || !duration) {
          ctx.body = {
            code: 400,
            message: '缺少必填字段',
          };
          return;
        }

        const bgm = bgmService.addBgm({ name, url, mood, duration });
        ctx.body = {
          code: 0,
          message: 'success',
          data: bgm,
        };
      } catch (error: any) {
        ctx.body = {
          code: 400,
          message: error.message || '添加背景音乐失败',
        };
      }
    });

    /**
     * 更新背景音乐
     * PUT /api/bgm/:id
     */
    router.put('/:id', async (ctx) => {
      try {
        const { id } = ctx.params;
        const { name, url, mood, duration, size } = ctx.request.body as {
          name?: string;
          url?: string;
          mood?: string;
          duration?: number;
          size?: number;
        };

        const bgm = bgmService.updateBgm(parseInt(id), { name, url, mood, duration, size });

        if (!bgm) {
          ctx.body = {
            code: 404,
            message: '背景音乐不存在',
          };
          return;
        }

        ctx.body = {
          code: 0,
          message: 'success',
          data: bgm,
        };
      } catch (error: any) {
        ctx.body = {
          code: 400,
          message: error.message || '更新背景音乐失败',
        };
      }
    });

    /**
     * 删除背景音乐
     * DELETE /api/bgm/:id
     */
    router.delete('/:id', async (ctx) => {
      try {
        const { id } = ctx.params;
        const success = bgmService.deleteBgm(parseInt(id));

        if (!success) {
          ctx.body = {
            code: 404,
            message: '背景音乐不存在',
          };
          return;
        }

        ctx.body = {
          code: 0,
          message: 'success',
        };
      } catch (error: any) {
        ctx.body = {
          code: 400,
          message: error.message || '删除背景音乐失败',
        };
      }
    });

    export default router;