Skip to content

Instantly share code, notes, and snippets.

@victorsteven
Created April 20, 2019 18:26
Show Gist options
  • Select an option

  • Save victorsteven/04386c3ffd303da89c0cb7735078cd95 to your computer and use it in GitHub Desktop.

Select an option

Save victorsteven/04386c3ffd303da89c0cb7735078cd95 to your computer and use it in GitHub Desktop.
BookService file
import database from '../src/models';
class BookService {
static async getAllBooks() {
try {
return await database.Book.findAll();
} catch (error) {
throw error;
}
}
static async addBook(newBook) {
try {
return await database.Book.create(newBook);
} catch (error) {
throw error;
}
}
static async updateBook(id, updateBook) {
try {
const bookToUpdate = await database.Book.findOne({
where: { id: Number(id) }
});
if (bookToUpdate) {
await database.Book.update(updateBook, { where: { id: Number(id) } });
return updateBook;
}
return null;
} catch (error) {
throw error;
}
}
static async getABook(id) {
try {
const theBook = await database.Book.findOne({
where: { id: Number(id) }
});
return theBook;
} catch (error) {
throw error;
}
}
static async deleteBook(id) {
try {
const bookToDelete = await database.Book.findOne({ where: { id: Number(id) } });
if (bookToDelete) {
const deletedBook = await database.Book.destroy({
where: { id: Number(id) }
});
return deletedBook;
}
return null;
} catch (error) {
throw error;
}
}
}
export default BookService;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment