You will be adding new views in the MyBooksApp by rendering simple HTML pages using Apache Velocity template engine. You will also practice with http cookies.
|Due||Fri||OCT 9 @ 11pm (EST)|
Grab a copy of homework 4 starter code, under
hw4_starter folder, from the MyBookApp repo on
jhu-oose github organization. The current implemetation has a file named
Server.java with a
main function in it. You should be able to run
Server.main to start SparkJava server. Now, point your browser to http://localhost:7000/ to see the login page. On the login page, there is a text field for entering a username and a dropdown list that allows selecting from among a few color options. Once you enter a user and press
Submit, you will be redirected to the front page where you see a welcome message and two links
Show all authors and
Add author. Your task is to:
Similar to the existing
Show all authorsand
Add authorlinks, add a new link
Show all books. When the user clicks it, he/she should be taken to
/booksview where a list of all books, fetched from the
Bookstable, are displayed.
To fetch the books, you should use your
Add another link in the front page titled
Add book. When the user clicks this link, he/she should be taken to
/addbookview where an HTMLL form is dispalyed with a submit button titled
Addto collect user input for a new book as well as a new author. When the button is pressed, first the author should be inserted into the
Authorstable. Then the
idof the inserted author must be used to insert the book into the
Bookstable. If the author already exists in the table (i.e. an author with the same exact name already exists), then no author insertion must be done. Though, the
idof the author must be retrieved and used for the book insertion.
To do the insertions, you must make use of
Ideally, author insertion and book insertion must be executed as a single transaction. A transaction is a sequence of operations performed (using one or more SQL statements) on a database as a single logical unit of work. If the author is inserted successfully, but book insertaion fails, then we will end up with the author added to the
Authorstable, but the book not inserted into the
Bookstable. Executing both of these queries as one transaction makes sure either both the author and book are added successfully or none of them is added. For this homework, you do NOT need to implement
addbookas a transaction (since transactions are out of the scope this class), but if you feel like going the extra step (to accomplish this task in a more proper way), go for it! You can start here.
On the login page, the user has a chance to select a color along with entering the user name. Right now, we do not do anything with this color selection, but it does get passed as a query param in the post request to
/. You should read this query param when receving/processing the post request and use it to change the color of the welcome message on the front page. Also, you need to set a session cookie on the response named
colorwith value whatever color that has been chosen by the user. As long as the cookie is in effect (i.e. session), the server should read this cookie and set the color of the welcome message accordingly.
Setting the color
For now, you can change the color of the welcome message by adding a style attribute to the
h1tag and setting the color attribute e.g.
<h1 style="color: blue">Welcome to MyBooks App</h1>sets the color of the heading to
Update all four views (
show all authors,
show all books,
add book, and
add author) such that only a "signed in" user can access them. If a not signed in user tries those views, they should be redirected to the front page where they can sign in.
Submit a single
.zip file to Gradescope. Th zip file should contain a folder named
hw4 that contains all your project files includeing a
README.md file which includes all the assumptions made, work done, etc.
Note that you need to work collaboratively to finish the homework, but you will make one submission as a group. All group members must contribute to the homework.