Skip to content

Instantly share code, notes, and snippets.

@andersonFaro9
Last active February 22, 2023 20:18
Show Gist options
  • Save andersonFaro9/d2b3ef622840b7807c92bae9ae326ef4 to your computer and use it in GitHub Desktop.
Save andersonFaro9/d2b3ef622840b7807c92bae9ae326ef4 to your computer and use it in GitHub Desktop.
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