Skip to content

Commit fe110b1

Browse files
committed
update book to DB is done
1 parent 4a20784 commit fe110b1

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

src/dbRelated/booksDbOps.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,25 @@ const deleteBookFromDB = async (bookToDelete) => {
5858
}
5959
};
6060

61-
export { findBooksFromDB, addBookToDB, deleteBookFromDB };
61+
const updateBookToDB = async (bookToUpdate) => {
62+
console.log("Book to update", bookToUpdate);
63+
// create the connection
64+
const client = createConnectionToDB();
65+
try {
66+
// select the DB and collection
67+
const db = selectDB(client, DB_NAME);
68+
const bookDeleted = await db
69+
.collection(COLLECTION_BOOKS)
70+
.updateOne(
71+
{ _id: convertToObjectId(bookToUpdate._id) },
72+
{ $set: { ...bookToUpdate.updateBookData } }
73+
);
74+
return bookDeleted.modifiedCount;
75+
} catch (error) {
76+
return error.toString();
77+
} finally {
78+
await closeConnectionToDB(client);
79+
}
80+
};
81+
82+
export { findBooksFromDB, addBookToDB, deleteBookFromDB, updateBookToDB };

src/resolvers/bookResolver.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
findBooksFromDB,
44
addBookToDB,
55
deleteBookFromDB,
6+
updateBookToDB,
67
} from "../dbRelated/booksDbOps.js";
78

89
const resolvers = {
@@ -19,6 +20,9 @@ const resolvers = {
1920
deleteBook: (_, args) => {
2021
return deleteBookFromDB(args);
2122
},
23+
updateBook: (_, args) => {
24+
return updateBookToDB(args);
25+
},
2226
},
2327
};
2428
export { resolvers };

src/typedefs/bookTypedef.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,16 @@ const booksTypeDefs = gql`
1212
findBooks: [Book]
1313
}
1414
15+
input UpdataBookParams {
16+
name: String!
17+
published: Int
18+
}
19+
1520
# All the Mutations on Book listed here
1621
type Mutation {
1722
addBook(name: String!): Book!
1823
deleteBook(_id: ID!): Int!
24+
updateBook(_id: ID!, updateBookData: UpdataBookParams!): Int!
1925
}
2026
`;
2127

0 commit comments

Comments
 (0)