Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add authors resource #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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

Expand Down
48 changes: 48 additions & 0 deletions src/components/authors/authors.controller.js
Original file line number Diff line number Diff line change
@@ -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();
37 changes: 37 additions & 0 deletions src/components/authors/authors.dao.js
Original file line number Diff line number Diff line change
@@ -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();
16 changes: 16 additions & 0 deletions src/components/authors/authors.routes.js
Original file line number Diff line number Diff line change
@@ -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;
17 changes: 17 additions & 0 deletions src/components/authors/authors.service.js
Original file line number Diff line number Diff line change
@@ -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();
Empty file.
34 changes: 34 additions & 0 deletions src/components/books/books.dao.js
Original file line number Diff line number Diff line change
@@ -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();
Empty file.
Empty file.