Skip to content

Commit

Permalink
api's added
Browse files Browse the repository at this point in the history
  • Loading branch information
D6link committed Oct 23, 2023
1 parent 405f7d9 commit 5ac487d
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 35 deletions.
11 changes: 0 additions & 11 deletions api/lib/sessions.js

This file was deleted.

10 changes: 0 additions & 10 deletions api/lib/users.js

This file was deleted.

7 changes: 3 additions & 4 deletions api/models/book.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const mongoose = require("mongoose");

const BookSchema = new mongoose.Schema({
genre: { type: String, required: true },
author: { type: String, required: true },

authors: { type: Array, required: true },
title: { type: String, required: true },
year_published: { type: String, required: true },
// TODO: check type for year_published and session_id
Expand All @@ -14,8 +14,7 @@ const BookSchema = new mongoose.Schema({
},
],
cover_photo: { type: String, required: false },
personal_rating: { type: String, required: false },
external_rating: { type: String, required: false },

});

const Book = mongoose.model("Book", BookSchema);
Expand Down
41 changes: 41 additions & 0 deletions api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"node": ">=18.1.0"
},
"dependencies": {
"bootstrap": "^5.3.2",
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"express": "^4.18.2",
Expand Down
44 changes: 34 additions & 10 deletions frontend/src/components/session/SessionForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ const SessionForm = ({ setModal }) => {
const [chosenBook, setChosenBook] = useState("")
const [token, setToken] = useState(window.localStorage.getItem("token"));
const [sessions, setSessions] = useState([]);
const [authors, setAuthors] = useState([]);
const [title, setTitle] = useState("");
const [yearPublished, setYearPublished] = useState("");
const [coverPhoto, setCoverPhoto] = useState("");


console.log("hello");

Expand All @@ -26,6 +31,17 @@ const SessionForm = ({ setModal }) => {
setChosenBook(event.target.value)
}

async function fetchBookDetails() {
const url = "https://www.googleapis.com/books/v1/volumes?q=isbn:9780008334840"
let response = await fetch(url);
response = await response.json()
setAuthors(response.items[0].volumeInfo.authors);
setTitle(response.items[0].volumeInfo.title);
setYearPublished(response.items[0].volumeInfo.publishedDate);
setCoverPhoto(response.items[0].volumeInfo.imageLinks.smallThumbnail);
}


const onClickButtonClose = () => {
console.log("Close button clicked");
setModal(false);
Expand All @@ -37,25 +53,31 @@ const SessionForm = ({ setModal }) => {
console.log("location:", location)
console.log("chosenBook:", chosenBook)

fetchBookDetails();
console.log(title);
console.log("year pub", yearPublished);

if (token) {
fetch("/sessions", {
fetch("/books", {
method: "POST",
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
date: date,
location: location,
chosen_book: chosenBook,
authors: authors,
title: title,
year_published: yearPublished,
cover_photo: coverPhoto

}),
})
.then((response) => {
if (response.status === 201) {
console.log("Session successfully added");
console.log("Book successfully added");
return response.json();
} else {
console.log("Session not successfully added");
console.log("Book not successfully added");
}
})
.then((data) =>{
Expand All @@ -65,14 +87,16 @@ const SessionForm = ({ setModal }) => {
the current state of sessions at the time the function is executed. */
window.localStorage.setItem("token", data.token);
setToken(window.localStorage.getItem("token"));
setSessions((prevSessions) => [data.session, ...prevSessions]);
setDate("");
setLocation("");
setChosenBook("");
setAuthors([]);
setTitle("");
setYearPublished("");
setCoverPhoto("");
});
} else {
console.log("No token");
}


};

// This return section is the JSX that gets rendered on the webpage
Expand Down

0 comments on commit 5ac487d

Please sign in to comment.