diff --git a/src/app.js b/src/app.js index b4273f3..c020c4d 100644 --- a/src/app.js +++ b/src/app.js @@ -5,7 +5,7 @@ import bodyParser from 'body-parser'; import logger from './utils/logger'; // import routes here - +import authorsRoutes from './components/authors/authors.routes'; // init express app const app = express(); @@ -27,10 +27,8 @@ app.get('/', async (req, res) => { }); // Add your routes here -/** - * for example, - * app.use('/api/v1/authors', authorsRoutes); - */ +app.use('/api/v1/authors', authorsRoutes); + // routes end here diff --git a/src/components/authors/authors.controller.js b/src/components/authors/authors.controller.js new file mode 100644 index 0000000..83994dd --- /dev/null +++ b/src/components/authors/authors.controller.js @@ -0,0 +1,48 @@ +import { Prisma } from '@prisma/client'; +import authorsService from './authors.service'; + +class AuthorsController { + async getAuthors(req, res, next) { + try { + const authors = await authorsService.findAll(); + return res.status(200).json(authors); + } catch (error) { + console.error(error); + return next(error); + } + } + + async createAuthor(req, res, next) { + try { + const createdAuthor = await authorsService.create(req.body); + return res.status(201).json(createdAuthor); + } catch (error) { + // Prisma client errors + // read more about errors https://www.prisma.io/docs/reference/api-reference/error-reference + if (error instanceof Prisma.PrismaClientKnownRequestError) { + // The .code property can be accessed in a type-safe manner + if (error.code === 'P2002') { + return res.status(400).json({ + error: true, + message: 'Author already exists with the same email.', + }); + } + } + return next(error); + } + } + + async getAuthor(req, res, next) { + try { + const authorId = parseInt(req.params.authorId, 10); + const author = await authorsService.findOne(authorId); + + return res.status(200).json(author); + } catch (error) { + console.error(error.message); + return next(error); + } + } +} + +export default new AuthorsController(); diff --git a/src/components/authors/authors.dao.js b/src/components/authors/authors.dao.js new file mode 100644 index 0000000..da22c6f --- /dev/null +++ b/src/components/authors/authors.dao.js @@ -0,0 +1,37 @@ +import { prisma } from '../../config/prismaClient'; + +class AuthorDAO { + create(data) { + return prisma.author.create({ data }); + } + + findAll() { + return prisma.author.findMany(); + } + + findOne(id) { + return prisma.author.findUnique({ + where: { + id, + }, + }); + } + + update(id, updatedData) { + return prisma.author.update({ + where: { id }, + data: updatedData, + }); + } + + getBooksByAuthor(id) { + return prisma.author.findUnique({ + where: { id }, + select: { + books: true, + }, + }); + } +} + +export default new AuthorDAO(); diff --git a/src/components/authors/authors.routes.js b/src/components/authors/authors.routes.js new file mode 100644 index 0000000..9512e13 --- /dev/null +++ b/src/components/authors/authors.routes.js @@ -0,0 +1,16 @@ +import express from 'express'; + +import AuthorsController from './authors.controller'; + +const router = express.Router(); + +// get all authors +router.get('/', AuthorsController.getAuthors); + +// create author +router.post('/', AuthorsController.createAuthor); + +// get single author +router.get('/:authorId', AuthorsController.getAuthor); + +export default router; diff --git a/src/components/authors/authors.service.js b/src/components/authors/authors.service.js new file mode 100644 index 0000000..5d2c6ca --- /dev/null +++ b/src/components/authors/authors.service.js @@ -0,0 +1,17 @@ +import authorDAO from './authors.dao'; + +class AuthorsService { + findAll() { + return authorDAO.findAll(); + } + + findOne(id) { + return authorDAO.findOne(id); + } + + async create(data) { + return authorDAO.create(data); + } +} + +export default new AuthorsService(); diff --git a/src/components/books/books.controller.js b/src/components/books/books.controller.js new file mode 100644 index 0000000..e69de29 diff --git a/src/components/books/books.dao.js b/src/components/books/books.dao.js new file mode 100644 index 0000000..1ff9f95 --- /dev/null +++ b/src/components/books/books.dao.js @@ -0,0 +1,34 @@ +import { prisma } from '../../config/prismaClient'; + +class BooksDAO { + create(data) { + return prisma.book.create({ data }); + } + + findAll() { + return prisma.book.findMany(); + } + + findOne(id) { + return prisma.book.findUnique({ + where: { + id, + }, + }); + } + + update(id, updatedData) { + return prisma.book.update({ + where: { id }, + data: updatedData, + }); + } + + getBooksByAuthor(authorId) { + return prisma.book.findMany({ + where: { authorId }, + }); + } +} + +export const authorDAO = new BooksDAO(); diff --git a/src/components/books/books.routes.js b/src/components/books/books.routes.js new file mode 100644 index 0000000..e69de29 diff --git a/src/components/books/books.service.js b/src/components/books/books.service.js new file mode 100644 index 0000000..e69de29