Skip to content

LaunchCodeEducation/software-dev-course-spring-boot-2

Repository files navigation

Upgrading The Spring Boot Personal Library Manager

In this exercise, we are going to start with a completed copy of the Spring Boot Personal Library Manager application, and upgrade it to store the library items in a database, and allow us to add, update, and delete items from the library.

Setting Up The Database

The first step in upgrading the application is to set up the database. Open your mysql workbench and create a new schema called springboot2. You do not need to create any tables, or set anything up in the database as Spring Boot JPA will do that for you.

Reviewing the Existing Application

Take a few moments to review the existing application. Under the main package, you will find the application class (SpringBoot2Application) and three sub-packages:

  • Controllers: Contains the REST controllers for the application
  • Models: Contains the model classes for the application
  • Repositories: Contains the database access repository classes for the application (currently empty.)

The current set of controllers only allow reading from each of the three library item types (books, albums, and movies), and it reads them from a hard-coded list of items in the controller. We will be replacing this with a database by implementing and using JPA repository classes.

Updating the Models

Adding id members to the models

The first step in updating the models is to add an id member to each of the models. This will allow us to uniquely identify each item in the database. You should add this as a private int id member to each of the models, and provide getters and setters for the id member. You should also update the constructors for each of the models to include the new id member.

Adding an empty constructor to each model

You should also add an empty constructor to each of the models that takes no arguments and doesn't contain any code
This is required by JPA. An empty constructor is a constructor that takes no arguments:

public class Book {
    // ...
    public Book() {
    }
    
    public Book(String name, String author, int year) {
        // ...
    }
}

Adding JPA Annotations to the models

Next, you should add JPA annotations to each of the models.

  1. Add import statements for the JPA annotations at the top of each model class:
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
  1. Add the @Entity annotation to each of the models. This tells JPA that the model is an entity that should be stored in the database.
  2. Add the @Id annotation to the id member of each model. This tells JPA that the id member is the primary key for the entity.
  3. Also add the @GeneratedValue(strategy = GenerationType.IDENTITY) annotation to the id member of each model. This tells JPA that the id member should be automatically generated by the database when a new entity is created.
@Entity
public class Book {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    // ...
}

And that's it for the models. We do not need to add @Column annotations to the other members of the models, as JPA will automatically choose reasonable defaults for the column names and types based on the member names and types.

Adding JPA Repositories

Next, we need to add JPA repositories for each of the models. JPA repositories are interfaces that extend the JpaRepository interface, and provide methods for accessing the database. Spring Boot will automatically create implementations of these interfaces for us.

Right-click on the Repositories package and create a new Java Interface called BookRepository. This interface should extend the JpaRepository interface, and should be parameterized with the Book model class and the type of the id member of the Book model class (which is Integer.)

Once you have created the BookRepository interface, you should add a similar interface for the Album and Movie

Updating the Controllers

The next step is to update the controllers to use the JPA repositories to access the database.

Setting Up To Use The JPA Repositories

  1. Change the existing methods to return null for now. We will be replacing the hard-coded list of items with calls to the JPA repositories.
  2. Remove the hard-coded list of items from the controllers.
  3. Add private final fields for the JPA repository that corresponds to the controllers.
  4. Add a constructor to the controllers that takes the JPA repository as an argument and assigns it to the private final field.

Implementing The Methods

  1. Implement the getAllItems method in each of the controllers. This method should call the findAll method on the JPA repository and return the result, and should be annotated with @GetMapping
  2. Implement the getItem method in each of the controllers. This method should call the findById method on the JPA repository and return the result, and should be annotated with @GetMapping('/{id}') and have a mapped @PathVariable parameter for the id of type int.
  3. Implement the addItem method in each of the controllers. This method should call the save method on the JPA repository and return the result, and should be annotated with @PostMapping with a @RequestBody parameter
  4. Implement the updateItem method in each of the controllers. This method should call the save method on the JPA repository and return the result, and should be annotated with @PutMapping('/{id}') with a @PathVariable parameter for the id of type int and a @RequestBody parameter.
  5. Implement the deleteItem method in each of the controllers. This method should call the deleteById method on the JPA repository and should be annotated with @DeleteMapping('/{id}') with a @PathVariable parameter for the id of type int.

Testing The Application

Once you have completed the project, and have successfully built the application and have it running, you should test the application by adding, updating, and deleting items from the library. You can use Postman to test the application by sending HTTP requests to the appropriate endpoints.

A postman collection export file is included in the project directory. You can import this file into Postman to get a set of pre-configured requests for testing this application.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages