All files / src/billing/product product.service.ts

100% Statements 16/16
100% Branches 2/2
100% Functions 3/3
100% Lines 14/14

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 391x 1x 1x 1x         1x     4x       2x   2x         1x       2x   2x 2x 2x   1x            
import { Injectable, InternalServerErrorException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Product } from './entities/product.entity';
import { Repository } from 'typeorm';
import { GetProductsDto } from './dto/get-products.dto';
import { CreateProductInputDto } from './dto/create-product.dto';
 
@Injectable()
export class ProductService {
  constructor(
    @InjectRepository(Product)
    private readonly productRepository: Repository<Product>,
  ) {}
 
  async getProducts(query: GetProductsDto) {
    const { type, sortBy = 'createdAt', order = 'DESC' } = query;
 
    const result = await this.productRepository.find({
      where: { type },
      order: { [sortBy]: order },
    });
 
    return { result };
  }
 
  async createProduct(body: CreateProductInputDto): Promise<Product> {
    const { name, type, price } = body;
 
    const object = this.productRepository.create({ name, type, price });
    try {
      return await this.productRepository.save(object);
    } catch (error) {
      throw new InternalServerErrorException(
        '상품을 DB에 저장하던 중 오류가 발생하였습니다.',
      );
    }
  }
}