Skip to content

Commit 4a20784

Browse files
committed
delete book from DB is done
1 parent 3ccebdf commit 4a20784

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

src/dbRelated/booksDbOps.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,21 @@ const addBookToDB = async (bookToAdd) => {
4141
}
4242
};
4343

44-
export { findBooksFromDB, addBookToDB };
44+
const deleteBookFromDB = async (bookToDelete) => {
45+
// create the connection
46+
const client = createConnectionToDB();
47+
try {
48+
// select the DB and collection
49+
const db = selectDB(client, DB_NAME);
50+
const bookDeleted = await db
51+
.collection(COLLECTION_BOOKS)
52+
.deleteOne({ _id: convertToObjectId(bookToDelete._id) });
53+
return bookDeleted.deletedCount;
54+
} catch (error) {
55+
return error.toString();
56+
} finally {
57+
await closeConnectionToDB(client);
58+
}
59+
};
60+
61+
export { findBooksFromDB, addBookToDB, deleteBookFromDB };

src/resolvers/bookResolver.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
// local dependencies
2-
import { findBooksFromDB, addBookToDB } from "../dbRelated/booksDbOps.js";
2+
import {
3+
findBooksFromDB,
4+
addBookToDB,
5+
deleteBookFromDB,
6+
} from "../dbRelated/booksDbOps.js";
37

48
const resolvers = {
59
Query: {
@@ -12,7 +16,9 @@ const resolvers = {
1216
addBook: (_, args) => {
1317
return addBookToDB(args);
1418
},
19+
deleteBook: (_, args) => {
20+
return deleteBookFromDB(args);
21+
},
1522
},
1623
};
17-
// console.log("fromm console", resolvers.Query.getAllBooks());
1824
export { resolvers };

src/typedefs/bookTypedef.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const booksTypeDefs = gql`
1515
# All the Mutations on Book listed here
1616
type Mutation {
1717
addBook(name: String!): Book!
18+
deleteBook(_id: ID!): Int!
1819
}
1920
`;
2021

0 commit comments

Comments
 (0)