본문 바로가기

아카이브/미륵관심법

feat : 알림 GET POST DELETE

728x90

- 알림 GET POST DELETE
- 기기 GET POST userId 추가하여 변경

 

//notification.service.ts

import { Injectable } from "@nestjs/common";
import { CreateNotificationDto } from "./dto/create-notification.dto";
import { InjectRepository } from "@nestjs/typeorm";
import { NotificationEntity } from "./entities/notification.entity";
import { Repository } from "typeorm";

@Injectable()
export class NotificationsService {
  constructor(
    @InjectRepository(NotificationEntity)
    private readonly notificationRepository: Repository<NotificationEntity>
  ) {}
  async create(createNotificationDto: CreateNotificationDto) {
    try {
      const result = await this.notificationRepository.insert({
        wavURL: createNotificationDto.wavURL,
        readStatus: createNotificationDto.readStatus,
        user: createNotificationDto.user,
        device: createNotificationDto.device,
      });
      if (result) {
        return "알림이 등록되었습니다.";
      } else {
        return "알림 등록에 실패하였습니다.";
      }
    } catch (error) {
      return "일시적인 오류가 발생하였습니다.";
    }
  }

  async findAll() {
    try {
      return await this.notificationRepository
        .createQueryBuilder("notification")
        .getRawMany();
    } catch (error) {
      return "일시적인 오류가 발생하였습니다.";
    }
  }

  async findOne(id: number) {
    try {
      return await this.notificationRepository
        .createQueryBuilder("notification")
        .where("notification.id = :id", { id })
        .getRawOne();
    } catch (error) {
      return "일시적인 오류가 발생하였습니다.";
    }
  }

  async remove(id: number) {
    try {
      const target = await this.notificationRepository.findOne({
        where: { id },
      });
      if (!target) {
        return "존재하지 않는 알림입니다.";
      }
      const result = await this.notificationRepository.delete({
        id,
      });
      if (result) {
        return `알림이 삭제되었습니다.`;
      } else {
        return `알림 삭제에 실패하였습니다.`;
      }
    } catch (error) {
      return "일시적인 오류가 발생하였습니다.";
    }
  }
}

// devices.service.ts

import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { CreateDeviceDto } from "./dto/create-device.dto";
import { DeleteDeviceDto } from "./dto/delete-device.dto";
import { Repository } from "typeorm";
import { DeviceEntity } from "./entities/device.entity";
import { UpdateDeviceDto } from "./dto/update-device.dto";

@Injectable()
export class DevicesService {
  constructor(
    @InjectRepository(DeviceEntity)
    private readonly deviceRepository: Repository<DeviceEntity>
  ) {}
  async create(createDeviceDto: CreateDeviceDto, uuid: string) {
    try {
      const target = await this.deviceRepository.findOne({
        where: { fcmToken: createDeviceDto.fcmToken },
      });
      if (target) {
        return "이미 등록된 기기입니다.";
      } else {
        const result = await this.deviceRepository.insert({
          uuid,
          fcmToken: createDeviceDto.fcmToken,
          name: createDeviceDto.name,
          mode: createDeviceDto.mode,
          startTime: createDeviceDto.startTime,
          endTime: createDeviceDto.endTime,
          alarmCount: createDeviceDto.alarmCount,
          activeStatus: createDeviceDto.activeStatus,
          user: createDeviceDto.userEntity,
        });
        if (result) {
          return "기기가 등록되었습니다.";
        } else {
          return "기기 등록에 실패하였습니다.";
        }
      }
    } catch (error) {
      return "일시적인 오류가 발생하였습니다.";
    }
  }

  async findAll() {
    try {
      return await this.deviceRepository
        .createQueryBuilder("device")
        .getRawMany();
    } catch (error) {
      return "일시적인 오류가 발생하였습니다.";
    }
  }

  async remove(deleteDeviceDto: DeleteDeviceDto) {
    try {
      const target = await this.deviceRepository.findOne({
        where: { uuid: deleteDeviceDto.uuid },
      });
      if (!target) {
        return "존재하지 않는 기기입니다.";
      }
      const result = await this.deviceRepository.delete({
        uuid: deleteDeviceDto.uuid,
      });
      if (result) {
        return `기기가 삭제되었습니다.`;
      } else {
        return `기기 삭제에 실패하였습니다.`;
      }
    } catch (error) {
      return "일시적인 오류가 발생하였습니다.";
    }
  }
  async update(updateDeviceDto: UpdateDeviceDto) {
    try {
      const result = await this.deviceRepository.update(
        { uuid: updateDeviceDto.uuid },
        { fcmToken: updateDeviceDto.fcmToken }
      );
      if (result) return `기기가 재연결 되었습니다.`;
      else `기기 재연결에 실패하였습니다.`;
    } catch (error) {
      return "일시적인 오류가 발생하였습니다.";
    }
  }
}