Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ bin/

### Mac OS ###
.DS_Store\ntomcat.8080/

## tomcat
**/tomcat.8080
27 changes: 27 additions & 0 deletions http/lectures.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
### 강의 목록 조회
GET http://localhost:8080/lectures


### 강의 등록
POST http://localhost:8080/lectures
Content-Type: application/json

{
"name": "lecture1",
"price": "10000"
}


### 강의 수정
PUT http://localhost:8080/lectures
Content-Type: application/json

{
"id": "1",
"name": "lecture-1",
"price": "20000"
}


### 강의 삭제
DELETE http://localhost:8080/lectures?id=1
4 changes: 4 additions & 0 deletions src/main/java/com/diy/app/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.diy.app;

import com.diy.framework.web.server.TomcatWebServer;

public class Main {
public static void main(String[] args) {
TomcatWebServer webServer = new TomcatWebServer();
webServer.start();
}
}
37 changes: 37 additions & 0 deletions src/main/java/com/diy/app/lecture/Lecture.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.diy.app.lecture;

public class Lecture {
private Long id;
private String name;
private int price;

public Lecture(Long id, String name, int price) {
this.id = id;
this.name = name;
this.price = price;
}

public Long getId() {
return id;
}

public String getName() {
return name;
}

public int getPrice() {
return price;
}

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

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

public void setPrice(int price) {
this.price = price;
}
}
28 changes: 28 additions & 0 deletions src/main/java/com/diy/app/lecture/LectureRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.diy.app.lecture;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class LectureRepository {

private static Map<Long, Lecture> lectures = new ConcurrentHashMap<>();
private static long sequence = 0L;

public List<Lecture> getLectures() {
return new ArrayList<>(lectures.values());
}

public void save(Lecture lecture) {
lecture.setId(++sequence);
lectures.put(lecture.getId(), lecture);
}

public void delete(Long lectureId) {
lectures.remove(lectureId);
}

public void update(Lecture lecture) {
lectures.put(lecture.getId(), lecture);
}

}
19 changes: 19 additions & 0 deletions src/main/java/com/diy/app/lecture/LectureRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.diy.app.lecture;

public class LectureRequest {
private Long id;
private String name;
private int price;

public Long getId() {
return id;
}

public String getName() {
return name;
}

public int getPrice() {
return price;
}
}
28 changes: 28 additions & 0 deletions src/main/java/com/diy/app/lecture/LectureService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.diy.app.lecture;

import java.util.Collection;

public class LectureService {

LectureRepository lectureRepository = new LectureRepository();
private static long sequence = 0L;


public Collection<Lecture> getLectures() {
return lectureRepository.getLectures();
}

public void register(String name, int price) {
Long id = ++sequence;
lectureRepository.save(new Lecture(id, name, price));
}

public void modify(Long id, String name, int price) {
lectureRepository.update(new Lecture(id, name, price));
}

public void delete(Long id) {
lectureRepository.delete(id);
}
}

79 changes: 79 additions & 0 deletions src/main/java/com/diy/app/lecture/LectureServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.diy.app.lecture;

import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
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.util.Collection;

@WebServlet("/lectures")
public class LectureServlet extends HttpServlet {

private final LectureService lectureService = new LectureService();
private final ObjectMapper objectMapper = new ObjectMapper();

@Override
public void init(final ServletConfig config) throws ServletException {
super.init(config);
}

//강의 목록 조회
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("LectureServlet doGet" + req.getRequestURI());
RequestDispatcher requestDispatcher = req.getRequestDispatcher("/lecture-list.jsp");
Collection<Lecture> lectures = lectureService.getLectures();
req.setAttribute("lectures", lectures);
requestDispatcher.forward(req, resp);
}

// 강의 등록
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
LectureRequest request = objectMapper.readValue(req.getReader(), LectureRequest.class);

String name = request.getName();

int price = request.getPrice();

lectureService.register(name, price);

resp.sendRedirect(req.getContextPath() + "/lectures");
}

//강의 수정
@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
LectureRequest request = objectMapper.readValue(req.getReader(), LectureRequest.class);

Long id = request.getId();

String name = request.getName();

int price = request.getPrice();

lectureService.modify(id, name, price);

resp.sendRedirect(req.getContextPath() + "/lectures");
}

//강의 삭제
@Override
protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

String idText = req.getParameter("id");

Long id = Long.parseLong(idText);

lectureService.delete(id);

resp.sendRedirect(req.getContextPath() + "/lectures");
}
}

4 changes: 2 additions & 2 deletions src/main/resources/lecture-list.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
<a href="/lecture-registration.jsp">등록</a>
<c:forEach var="lecture" items="${lectures}">
<li>id: ${lecture.id}</li>
<li>pw: ${lecture.name}</li>
<li>pw: ${lecture.price}</li>
<li>name: ${lecture.name}</li>
<li>price: ${lecture.price}</li>
<br>
</c:forEach>
</body>
Expand Down