Skip to content

Commit 01744d0

Browse files
committed
author feature added
1 parent 547b524 commit 01744d0

File tree

5 files changed

+147
-1
lines changed

5 files changed

+147
-1
lines changed

src/dbRelated/AuthorsDbOps.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import {
2+
createConnectionToDB,
3+
closeConnectionToDB,
4+
selectDB,
5+
convertToObjectId,
6+
} from "../helpers/dbHelpers.js";
7+
8+
// all the db related process variables
9+
const { DB_NAME, COLLECTION_AUTHORS } = process.env;
10+
11+
const findAuthorsFromDB = async () => {
12+
// create the connection
13+
const client = createConnectionToDB();
14+
try {
15+
// select the DB and collection
16+
const db = selectDB(client, DB_NAME);
17+
const booksFound = await db.collection(COLLECTION_AUTHORS).find({}).toArray();
18+
const res = booksFound || [];
19+
return res;
20+
} catch (error) {
21+
return error.toString();
22+
} finally {
23+
await closeConnectionToDB(client);
24+
}
25+
};
26+
27+
const addAuthorToDB = async (bookToAdd) => {
28+
// create the connection
29+
const client = createConnectionToDB();
30+
try {
31+
// select the DB and collection
32+
const db = selectDB(client, DB_NAME);
33+
const bookInserted = await db
34+
.collection(COLLECTION_AUTHORS)
35+
.insertOne(bookToAdd);
36+
return { _id: bookInserted.insertedId, ...bookToAdd };
37+
} catch (error) {
38+
return error.toString();
39+
} finally {
40+
await closeConnectionToDB(client);
41+
}
42+
};
43+
44+
const deleteAuthorFromDB = 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_AUTHORS)
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+
const updateAuthorToDB = async (bookToUpdate) => {
62+
console.log("Author 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_AUTHORS)
70+
.updateOne(
71+
{ _id: convertToObjectId(bookToUpdate._id) },
72+
{ $set: { ...bookToUpdate.updateAuthorData } }
73+
);
74+
return bookDeleted.modifiedCount;
75+
} catch (error) {
76+
return error.toString();
77+
} finally {
78+
await closeConnectionToDB(client);
79+
}
80+
};
81+
82+
export { findAuthorsFromDB, addAuthorToDB, deleteAuthorFromDB, updateAuthorToDB };
83+

src/resolvers/authorResolver.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// local dependencies
2+
import {
3+
findAuthorsFromDB,
4+
addAuthorToDB,
5+
deleteAuthorFromDB,
6+
updateAuthorToDB,
7+
} from "../dbRelated/AuthorsDbOps.js";
8+
9+
const resolvers = {
10+
Query: {
11+
findAuthors: () => {
12+
return findAuthorsFromDB();
13+
},
14+
},
15+
16+
Mutation: {
17+
addAuthor: (_, args) => {
18+
return addAuthorToDB(args);
19+
},
20+
deleteAuthor: (_, args) => {
21+
return deleteAuthorFromDB(args);
22+
},
23+
updateAuthor: (_, args) => {
24+
return updateAuthorToDB(args);
25+
},
26+
},
27+
};
28+
export { resolvers };
29+

src/resolvers/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import { resolvers as helloResolvers } from "./helloResolver.js";
22
import { resolvers as bookResolvers } from "./bookResolver.js";
3+
import { resolvers as authorResolvers } from "./authorResolver.js";
34

45
const resolvers = {
56
Query: {
67
...bookResolvers.Query,
78
...helloResolvers.Query,
9+
...authorResolvers.Query,
810
},
911
Mutation: {
1012
...bookResolvers.Mutation,
1113
...helloResolvers.Mutation,
14+
...authorResolvers.Mutation,
1215
},
1316
};
1417

src/typedefs/authorTypedef.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { gql } from "apollo-server-lambda";
2+
3+
const authorsTypeDefs = gql`
4+
# type book is defined
5+
type Author {
6+
_id: ID!
7+
firstName: String!
8+
lastName: String
9+
country: String
10+
}
11+
12+
type Query {
13+
findAuthors: [Author]
14+
}
15+
16+
input UpdataAuthorParams {
17+
firstName: String!
18+
lastName: String
19+
country: String
20+
}
21+
22+
# All the Mutations on Author listed here
23+
type Mutation {
24+
addAuthor(firstName: String!, lastName: String): Author!
25+
deleteAuthor(_id: ID!): Int!
26+
updateAuthor(_id: ID!, updateAuthorData: UpdataAuthorParams!): Int!
27+
}
28+
`;
29+
30+
export { authorsTypeDefs };

src/typedefs/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// local dependencies
22
import { booksTypeDefs } from "./bookTypedef.js";
33
import { helloTypeDefs } from "./helloTypedef.js";
4+
import { authorsTypeDefs } from "./authorTypedef.js";
45

5-
const typeDefs = [booksTypeDefs, helloTypeDefs];
6+
const typeDefs = [booksTypeDefs, helloTypeDefs, authorsTypeDefs];
67

78
export { typeDefs };

0 commit comments

Comments
 (0)