All files / notice notice.service.ts

100% Statements 41/41
100% Branches 6/6
100% Functions 6/6
100% Lines 39/39

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 862x 2x 2x     2x 2x 2x 2x   2x     2x     2x 10x     1x 1x 1x 1x 1x   1x   1x       2x 2x 1x     1x               3x 3x 1x     2x   2x                 2x 2x 1x     1x   1x   1x       2x 2x 1x     1x 1x   1x      
import { Injectable, NotFoundException } from '@nestjs/common';
import { plainToInstance } from 'class-transformer';
import { ERROR } from 'src/commons/constants/error';
import { LastItemIdResponseDto } from 'src/commons/dtos/last-item-id-response.dto';
import { UuidResponseDto } from 'src/commons/dtos/uuid-response.dto';
import { isEmpty } from 'src/commons/util/is/is-empty';
import { generateUUID } from 'src/commons/util/uuid';
import { NoticeEntity } from 'src/entities/notice.entity';
import { ApiNoticeGetDetailResponseDto } from 'src/notice/dto/api-notice-get-detail-response.dto';
import { ApiNoticeGetRequestQueryDto } from 'src/notice/dto/api-notice-get-request-query.dto';
import { ApiNoticeGetResponseDto } from 'src/notice/dto/api-notice-get-response.dto';
import { ApiNoticePostRequestBodyDto } from 'src/notice/dto/api-notice-post-request-body.dto';
import { ApiNoticePutRequestBodyDto } from 'src/notice/dto/api-notice-put-request-body.dto';
import { NoticeQueryRepository } from 'src/notice/notice.query.repository';
 
@Injectable()
export class NoticeService {
  constructor(private readonly noticeQueryRepository: NoticeQueryRepository) {}
 
  async createNotice(dto: ApiNoticePostRequestBodyDto) {
    const noticeEntity = new NoticeEntity();
    noticeEntity.uuid = generateUUID();
    noticeEntity.title = dto.title;
    noticeEntity.content = dto.content;
    noticeEntity.published_at = dto.published_at;
 
    await this.noticeQueryRepository.save(noticeEntity);
 
    return { uuid: noticeEntity.uuid };
  }
 
  async getNoticeDetail(uuid: string) {
    const notice: NoticeEntity = await this.noticeQueryRepository.findOne(uuid);
    if (isEmpty(notice)) {
      throw new NotFoundException(ERROR.NOT_EXIST_DATA);
    }
 
    return plainToInstance(ApiNoticeGetDetailResponseDto, notice, {
      excludeExtraneousValues: true,
    });
  }
 
  async getNoticeList(
    dto: ApiNoticeGetRequestQueryDto,
  ): Promise<LastItemIdResponseDto<ApiNoticeGetResponseDto>> {
    const noticeList: NoticeEntity[] = await this.noticeQueryRepository.find(dto);
    if (noticeList.length === 0) {
      return { items: [], last_item_id: 0 };
    }
 
    const lastItemId = noticeList.length === dto.size ? noticeList[noticeList.length - 1].id : 0;
 
    return {
      items: plainToInstance(ApiNoticeGetResponseDto, noticeList, {
        excludeExtraneousValues: true,
      }),
      last_item_id: lastItemId,
    };
  }
 
  async updateNotice(uuid: string, dto: ApiNoticePutRequestBodyDto): Promise<UuidResponseDto> {
    const notice: NoticeEntity = await this.noticeQueryRepository.findOne(uuid);
    if (isEmpty(notice)) {
      throw new NotFoundException(ERROR.NOT_EXIST_DATA);
    }
 
    Object.assign(notice, dto);
 
    await this.noticeQueryRepository.save(notice);
 
    return { uuid };
  }
 
  async deleteNotice(uuid: string): Promise<UuidResponseDto> {
    const notice: NoticeEntity = await this.noticeQueryRepository.findOne(uuid);
    if (isEmpty(notice)) {
      throw new NotFoundException(ERROR.NOT_EXIST_DATA);
    }
 
    notice.archived_at = new Date();
    await this.noticeQueryRepository.save(notice);
 
    return { uuid };
  }
}