-
Notifications
You must be signed in to change notification settings - Fork 8
/
Cafe.java
88 lines (72 loc) · 2.28 KB
/
Cafe.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package cafe.web.view;
import cafe.model.entity.Coffee;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.RequestScoped;
import jakarta.faces.context.FacesContext;
import jakarta.inject.Named;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.core.GenericType;
import jakarta.ws.rs.core.MediaType;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
@Named
@RequestScoped
public class Cafe implements Serializable {
private static final long serialVersionUID = 1L;
private String baseUri;
private transient Client client;
@NotNull
@NotEmpty
protected String name;
@NotNull
protected Double price;
protected List<Coffee> coffeeList;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public List<Coffee> getCoffeeList() {
this.getAllCoffees();
return coffeeList;
}
public String getHostName() {
return System.getenv("HOSTNAME");
}
@PostConstruct
private void init() {
HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext()
.getRequest();
baseUri = "http://localhost:9080" + request.getContextPath() + "/rest/coffees";
this.client = ClientBuilder.newBuilder().build();
}
private void getAllCoffees() {
this.coffeeList = this.client.target(this.baseUri).path("/").request(MediaType.APPLICATION_JSON)
.get(new GenericType<List<Coffee>>() {
});
}
public void addCoffee() throws IOException {
Coffee coffee = new Coffee(this.name, this.price);
this.client.target(baseUri).request(MediaType.APPLICATION_JSON).post(Entity.json(coffee));
this.name = null;
this.price = null;
FacesContext.getCurrentInstance().getExternalContext().redirect("");
}
public void removeCoffee(String coffeeId) throws IOException {
this.client.target(baseUri).path(coffeeId).request().delete();
FacesContext.getCurrentInstance().getExternalContext().redirect("");
}
}