Created
April 20, 2019 18:26
-
-
Save victorsteven/04386c3ffd303da89c0cb7735078cd95 to your computer and use it in GitHub Desktop.
BookService file
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 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