Homework 4

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.

DayDate
ReleaseFriOCT 2
DueFriOCT 9 @ 11pm (EST)

Task

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:

  1. Similar to the existing Show all authors and Add author links, add a new link Show all books. When the user clicks it, he/she should be taken to /books view where a list of all books, fetched from the Books table, are displayed.

    info

    To fetch the books, you should use your Sql2oBookDAO.listAll.

  2. Add another link in the front page titled Add book. When the user clicks this link, he/she should be taken to /addbook view where an HTMLL form is dispalyed with a submit button titled Add to 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 Authors table. Then the id of the inserted author must be used to insert the book into the Books table. 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 id of the author must be retrieved and used for the book insertion.

    info

    To do the insertions, you must make use of Sql2oBookDao.add and Sql2oAuthorDao.add.

    tip

    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 Authors table, but the book not inserted into the Books table. 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 addbook as 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.

  3. 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 color with 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 h1 tag and setting the color attribute e.g. <h1 style="color: blue">Welcome to MyBooks App</h1> sets the color of the heading to blue.

  4. 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.

Submission

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.

caution

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.