Building a CRUD application

Within the GitHub source folder for this chapter, you will find two examples. The first one is located in the Chapter04/customer-service/basic folder and will be discussed in this section. We recommend importing the project into your IDE before you move on.

If you take a look at the project's structure, you will see that it is made up of three main components:

  1. First of all, there is a model class that records customer entries:
package com.packt.quarkus.chapter4;

public class Customer {
private Integer id;
private String name;
private String surname;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSurname() {
return surname;
}

public void setSurname(String surname) {
this.surname = surname;
}
}

The Customer class is the minimal definition of a Customer record. It is defined as a Plain Old Java Object that should be stored in memory.

  1. Next, take a look at the CustomerRepository class, which contains the core functionalities that we'll use to manage our model:
package com.packt.quarkus.chapter4;

import javax.enterprise.context.ApplicationScoped;
import java.util.ArrayList;
import java.util.List;


@ApplicationScoped
public class CustomerRepository {

List<Customer> customerList = new ArrayList();
int counter;

public int getNextCustomerId() {
return counter++;
}

public List<Customer> findAll() {
return customerList;
}

public Customer findCustomerById(Integer id) {
for (Customer c:customerList) {
if (c.getId().equals(id)) {
return c;
}
}
throw new CustomerException("Customer not found!");
}

public void updateCustomer(Customer customer) {
Customer customerToUpdate =
findCustomerById(customer.getId());
customerToUpdate.setName(customer.getName());
customerToUpdate.setSurname(customer.getSurname());
}

public void createCustomer(Customer customer) {
customer.setId(getNextCustomerId());
findAll().add(customer);
}

public void deleteCustomer(Integer customerId) {
Customer c = findCustomerById(customerId);
findAll().remove(c);
}
}

As you can see, it's just a vanilla implementation of a repository that serves as a pattern that stores and retrieves our data. In upcoming chapters, we will be adding other features, such as persistent storage and asynchronous behavior. Due to this, it's good to start with a service-agnostic example.

  1. The customer service is completed by the CustomerEndpoint class, which has the following implementation:
package com.packt.quarkus.chapter4;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import java.util.List;


@Path("customers")
@ApplicationScoped
@Produces("application/json")
@Consumes("application/json")
public class CustomerEndpoint {

@Inject CustomerRepository customerRepository;

@GET
public List<Customer> getAll() {
return customerRepository.findAll();
}

@POST
public Response create(Customer customer) {
customerRepository.createCustomer(customer);
return Response.status(201).build();

}

@PUT
public Response update(Customer customer) {
customerRepository.updateCustomer(customer);
return Response.status(204).build();
}
@DELETE
public Response delete(@QueryParam("id") Integer customerId) {
customerRepository.deleteCustomer(customerId);
return Response.status(204).build();
}

}

As you can see, CustomerEndpoint is a thin REST layer over the CustomerRepository class and contains a method for each CRUD action, where it maps each one to the appropriate HTTP method. When using this approach, it would suffice to have a single REST path for the whole application (/customers) since the REST engine will call the appropriate method based on the HTTP request method.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.152.87