Last active
February 22, 2023 20:18
-
-
Save andersonFaro9/d2b3ef622840b7807c92bae9ae326ef4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@nestjs/common'; | |
import { PrismaService } from '../services/prisma.service'; | |
import { BooksDto } from './../booksDto/books.dto'; | |
@Injectable() | |
export class BooksService { | |
constructor(private prisma: PrismaService) {} | |
async create(data: BooksDto) { | |
const barCodeExist = await this.prisma.book.findFirst({ | |
where: { | |
bar_code: data.bar_code, | |
}, | |
}); | |
if (barCodeExist) { | |
throw new Error('erro'); | |
} | |
const books = await this.prisma.book.create({ | |
data: { | |
title: 'titulo mundial 14', | |
description: 'description 14', | |
year: '2028', | |
author: 'joão', | |
bar_code: '5555555555392010101kdkslskdlksdl', | |
}, | |
}); | |
return books; | |
} | |
async findAll() { | |
const books = await this.prisma.book.findMany(); | |
console.log(books); | |
return books; | |
} | |
async update(id: string, data: BooksDto) { | |
const booksExists = await this.prisma.book.findUnique({ | |
where: { id }, | |
}); | |
if (!booksExists) { | |
throw new Error('Book does not exists'); | |
} | |
return await this.prisma.book.update({ | |
data, | |
where: { id }, | |
}); | |
} | |
async delete(id: string) { | |
const booksExists = await this.prisma.book.findUnique({ | |
where: { id }, | |
}); | |
if (!booksExists) { | |
throw new Error('Book does not exists'); | |
} | |
return this.prisma.book.delete({ | |
where: { id }, | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment