|
| 1 | +# LabTV REST Server |
| 2 | + |
| 3 | +A Spring project aimed at simulating [IMDb-API](https://imdb-api.com/). |
| 4 | + |
| 5 | +It provides **JSON** responses through any REST API Client such as [Postman](https://www.postman.com/), but was actually intended to work as a **backend** for [LabTV REST Client](https://github.com/Montblanc0/LabTV-REST-Client). |
| 6 | + |
| 7 | +This project was built with [Spring Framework](https://spring.io/projects/spring-framework) v5.3.20, [Hibernate](https://hibernate.org/) v5.6.5 and [Maven](https://maven.apache.org/) v3.8.4 against JavaSE-11. Its Persistence Unit is configured to work with MariaDB v10.4.24 provided by [XAMPP](https://www.apachefriends.org/download.html) v8.1.6. |
| 8 | + |
| 9 | +## Database Structure and JPA Entities |
| 10 | + |
| 11 | +[](https://i.ibb.co/rF7Mwtr/labtv-full2.jpg "LabTV MySQL Structure") |
| 12 | + |
| 13 | +Behind the scenes is a **MySQL** database built around the snapshot of a "**Most Popular Movies**" query from around June 2022 provided by our web development school and initially represented by the "evidenza" table only. We were tasked to create the "film" and "trailer" tables with specific fields and to make the according relationships. |
| 14 | + |
| 15 | +Hibernate was set to create and update tables and fields thanks to the `hibernate.hbm2ddl.auto` property. |
| 16 | + |
| 17 | +Each table (except join tables) are mapped as JPA Entities and feature `@OneToOne` and `@ManyToMany` uni- and bi-directional relationships. To take advantage of `FetchType.LAZY` and to avoid troubles with Hibernate's session and transaction management I ended up needing some more properties: |
| 18 | + |
| 19 | +``` |
| 20 | +hibernate.enable_lazy_load_no_trans=true |
| 21 | +hibernate.event.merge.entity_copy_observer=allow |
| 22 | +``` |
| 23 | +I also included `hibernate-enhance-maven-plugin` to improve lazy loading. |
| 24 | + |
| 25 | +In order to fill the database with missing entries, I made an ad-hoc component in [LabTV REST Client](https://github.com/Montblanc0/LabTV-REST-Client) accessible via `/onetime` where i fetched data from IMDb-API by making ["Movie Title" and "Movie Trailer" queries](https://imdb-api.com/api/#Title-header). To avoid adding new entries everytime so to just work with a snapshot, each query response was saved to a `*.har` file and fetching methods were commented out. These files were then sent from the client to a dedicated Spring `@RestController` via POST methods. |
| 26 | + |
| 27 | +## JSON response modeling |
| 28 | + |
| 29 | +Since database tables are shaped to reflect IMDb-API's JSON response structure and in order to take advantage of the redundancy of some fields, I decided not to map Entities against DTOs, but to map requests and responses directly onto Entities by making use of some Jackson annotations such as `@JsonPropertyOrder`, `@JsonGetter` and `@JsonIgnore`. I aimed at producing the same output as IMDb-API, so that [LabTV](https://github.com/Montblanc0/LabTV) needed minimal changes to keep on working seamlessly. |
| 30 | + |
| 31 | +[](https://i.ibb.co/5hQgD25/json-response.jpg "'Most Popular Movies' JSONs") |
| 32 | + |
| 33 | +## Instructions |
| 34 | + |
| 35 | +### 1. Tomcat Server |
| 36 | +This project was meant to work with [Tomcat v9.x](https://tomcat.apache.org/download-90.cgi "Tomcat v9.x") and won't work on any other version. [Add Tomcat to Eclipse](https://www.baeldung.com/eclipse-tomcat "Add Tomcat to Eclipse") and make sure it is [configured](https://www.baeldung.com/eclipse-tomcat#configure "configured") to run on **HTTP/1.1 port 8082** as [LabTV REST Client](https://github.com/Montblanc0/LabTV-REST-Client) will be making calls directly to `http://localhost:8082/labtv-api/`. |
| 37 | + |
| 38 | +### 2. Eclipse Configuration |
| 39 | + |
| 40 | +[Eclipse IDE for Enterprise Java and Web Developers](https://www.eclipse.org/downloads/packages/release/2022-06/r/eclipse-ide-enterprise-java-and-web-developers "Eclipse IDE for Enterprise Java and Web Developers") is preferred. |
| 41 | + |
| 42 | +- Import this project folder by selecting it from `File -> Open Projects from File System...` ; |
| 43 | +- Right click on the project name from the Project Explorer and run `Maven -> Update Project...` (you may need to check `Force Update of Snapshots/Releases`); |
| 44 | +- Right click on the project, select `Properties`, then `Web Project Settings` and make sure that `Context root` is set to `labtv-api`. |
| 45 | + |
| 46 | +### 3. Database Import |
| 47 | + |
| 48 | +The Persistence Unit is configured to work on a stock MySQL configuration as provided by [XAMPP](https://www.apachefriends.org/it/index.html "XAMPP"), so just make sure your MySQL process is running on **port 3306** and that you haven't added a password. |
| 49 | + |
| 50 | + #### Full Import (recommended) |
| 51 | +- Make sure to **backup** any database named `labtv`, if any. The provided `*.sql` files will automatically create or select a database named exactly "labtv". |
| 52 | +- Open your phpMyAdmin's **homepage** (don't select any database); |
| 53 | +- Select `Import`; |
| 54 | +- Browse for a file and select `labtv_full.sql` from the project root; |
| 55 | +- Leave the rest as it is and clic the **Import** button. |
| 56 | + |
| 57 | +If done correctly, you will find a database named "labtv" in your database list. You can now go back to **Eclipse**: |
| 58 | + |
| 59 | +- Right click on the project, `Run As` and then `Run on Server` (if prompted, select your "Tomcat v9.0 Server"). |
| 60 | + |
| 61 | +You can safely close the browser window and check the server logs from Eclipse's Console. **LabTV REST Server is now listening on port 8082.** |
| 62 | + |
| 63 | +You can now switch to [LabTV REST Client](https://github.com/Montblanc0/LabTV-REST-Client) or use any client to try the [available queries](https://github.com/Montblanc0/LabTV-REST-Server#queries). |
| 64 | + |
| 65 | +#### Initial Import |
| 66 | + |
| 67 | +You can choose to import a minimal version of the database consisting of only one table (as provided by the school) and fill it later from [LabTV REST Client](https://github.com/Montblanc0/LabTV-REST-Client)'s "OneTime" component to see how Hibernate handles `INSERT`s. |
| 68 | + |
| 69 | +- The instructions are the same as above, with the exception of selecting `labtv_initial.sql` from the Import page instead of the full variant. |
| 70 | + |
| 71 | +If done correctly, you will find a database named "labtv" in your database list. You can now go back to **Eclipse**: |
| 72 | + |
| 73 | +- Right click on the project, `Run As` and then `Run on Server` (if prompted, select your "Tomcat v9.0 Server"); |
| 74 | +- Hibernate will automatically create the missing tables and set their fields, indexes and relationships. |
| 75 | + |
| 76 | +You can safely close the browser window and check the server logs from Eclipse's Console. **LabTV REST Server is now listening on port 8082.** |
| 77 | + |
| 78 | +Now, it's time to switch to the **frontend** to fill the database: |
| 79 | + |
| 80 | +- Follow LabTV REST Client's [instructions](https://github.com/Montblanc0/LabTV-REST-Client#instructions) and fire it up; |
| 81 | +- Manually navigate to `/onetime` (example: `http://localhost:4200/onetime`); |
| 82 | +- click on `Send All Movies` and wait for it to finish; |
| 83 | +- click on `Send All Trailers` and wait for it to finish. |
| 84 | + |
| 85 | +You can now use [LabTV REST Client](https://github.com/Montblanc0/LabTV-REST-Client) as normal or use any client to try the [available queries](https://github.com/Montblanc0/LabTV-REST-Server#queries). |
| 86 | + |
| 87 | +## Build |
| 88 | + |
| 89 | +### Requirements |
| 90 | +A Windows build is also provided, but still requires [Tomcat v9.x](https://tomcat.apache.org/download-90.cgi "Tomcat v9.x") to be deployed correctly. |
| 91 | +> It is advised not to run Tomcat from Eclipse. Instructions will reference your Tomcat installation directory. |
| 92 | +
|
| 93 | +- Download [labtv-api.war](https://github.com/Montblanc0/LabTV-REST-Server/releases); |
| 94 | +- Locate your Tomcat v9.x installation folder; |
| 95 | +- Place the war file into the `webapps` subfolder; |
| 96 | +- Go back to the Tomcat installation folder and open the `conf` folder; |
| 97 | +- Right click the `server.xml` file and `Open with` an editor of your choice such as Notepad; |
| 98 | +- Replace any occurrence of `8080` with `8082` and save; |
| 99 | +- Go back to the Tomcat installation folder and open the `bin` folder; |
| 100 | +- Press `CTRL + L` to highlight the address bar (it could be `CTRL + D` on certain machines); |
| 101 | +- type `powershell` and press enter to open PowerShell directly from that folder; |
| 102 | +- type `./startup` and press Enter; |
| 103 | +- Tomcat will start in a new window and will automatically deploy the war file. |
| 104 | + |
| 105 | +**LabTV REST Server is now listening on port 8082.** You can now use [LabTV REST Client](https://github.com/Montblanc0/LabTV-REST-Client) as normal or use any client to try the [available queries](https://github.com/Montblanc0/LabTV-REST-Server#queries). |
| 106 | + |
| 107 | +### Queries |
| 108 | +- Most Popular Movies |
| 109 | +`http://localhost:8082/labtv-api/api/evidenze` |
| 110 | + |
| 111 | +- Find Movie By Id |
| 112 | +`http://localhost:8082/labtv-api/api/films/<id>` |
| 113 | + |
| 114 | +- Find Movie By Title |
| 115 | +`http://localhost:8082/labtv-api/api/films/titoli/<title>` |
| 116 | + |
| 117 | +- Get Movie Trailer By Id |
| 118 | +`http://localhost:8082/labtv-api/api/trailers/<id>` |
| 119 | + |
| 120 | +- Get All Most Popular Entries |
| 121 | +`http://localhost:8082/labtv-api/api/all-evidenze` |
| 122 | + |
| 123 | +- Get Most Popular Movie By Id |
| 124 | +`http://localhost:8082/labtv-api/api/evidenza/<id>` |
0 commit comments