-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathLectureServlet.java
More file actions
125 lines (101 loc) · 3.62 KB
/
Copy pathLectureServlet.java
File metadata and controls
125 lines (101 loc) · 3.62 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.diy.app;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;
@WebServlet("/lectures")
public class LectureServlet extends HttpServlet {
private final ObjectMapper objectMapper = new ObjectMapper();
private final List<Lecture> lectures = new ArrayList<>();
private Long idx = 1L;
@Override
public void init() throws ServletException {
System.out.println("[INIT] servlet initialized");
IntStream.rangeClosed(1, 5)
.forEach(i -> lectures.add(new Lecture(idx++, "TEST"+i, BigDecimal.valueOf(i*10000))));
super.init();
}
/**
* 강의 목록 조회
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("[GET] lecture list");
req.setAttribute("lectures", lectures);
req.getRequestDispatcher("/lecture-list.jsp").forward(req, resp);
}
/**
* 강의 등록
*/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
System.out.println("[POST] lecture register");
Map<String, Object> payload = objectMapper.readValue(req.getReader(), Map.class);
Lecture lecture = new Lecture(
idx++,
payload.get("name").toString(),
new BigDecimal(payload.get("price").toString())
);
lectures.add(lecture);
resp.sendRedirect("/lectures");
}
/**
* 강의 수정
*/
@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException {
System.out.println("[PUT] lecture modify");
// req: id, name, price
Map<String, Object> payload = objectMapper.readValue(req.getReader(), Map.class);
Long id = Long.valueOf(payload.get("id").toString());
for (int i = 0; i < lectures.size(); i++) {
var orgLecture = lectures.get(i);
if (id.equals(orgLecture.getId())) {
lectures.set(i, new Lecture(
orgLecture.getId(),
payload.get("name").toString(),
new BigDecimal(payload.get("price").toString())
));
break;
}
}
}
/**
* 강의 삭제
*/
@Override
protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("[DELETE] lecture delete");
// req: id
Map<String, Object> payload = objectMapper.readValue(req.getReader(), Map.class);
Long id = Long.valueOf(payload.get("id").toString());
lectures.removeIf(lecture -> id.equals(lecture.getId()));
}
public static class Lecture {
private final Long id;
private final String name;
private final BigDecimal price;
private Lecture(Long id, String name, BigDecimal price) {
this.id = id;
this.name = name;
this.price = price;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public BigDecimal getPrice() {
return price;
}
}
}