Skip to content

Commit 07a069d

Browse files
authored
Merge pull request #32 from S0L-V/FEAT-방-생성
[FEAT] 방 DTO, DAO 작성 및 생성, 조회 기능 구현
2 parents 53f8bb7 + 829230d commit 07a069d

11 files changed

Lines changed: 420 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package room.controller;
2+
3+
import java.io.IOException;
4+
5+
import javax.servlet.ServletException;
6+
import javax.servlet.annotation.WebServlet;
7+
import javax.servlet.http.HttpServlet;
8+
import javax.servlet.http.HttpServletRequest;
9+
import javax.servlet.http.HttpServletResponse;
10+
11+
import room.dao.RoomDAO;
12+
import room.dao.RoomDAOImpl;
13+
14+
@WebServlet("/room/create")
15+
public class CreateRoomController extends HttpServlet {
16+
private static final long serialVersionUID = 1L;
17+
18+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
19+
throws ServletException, IOException {
20+
21+
String hostUserId = request.getParameter("hostUserId");
22+
String roomName = request.getParameter("roomName");
23+
String roomPwd = request.getParameter("roomPwd");
24+
String isPublic = request.getParameter("isPublic");
25+
String playType = request.getParameter("playType");
26+
27+
try {
28+
RoomDAO roomDAO = new RoomDAOImpl();
29+
30+
roomDAO.createRoom(
31+
hostUserId,
32+
roomName,
33+
roomPwd,
34+
isPublic,
35+
playType);
36+
37+
response.sendRedirect(request.getContextPath() + "/lobby");
38+
} catch (Exception e) {
39+
e.printStackTrace();
40+
throw new ServletException("방 생성 실패", e);
41+
}
42+
}
43+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package room.controller;
2+
3+
import java.io.IOException;
4+
5+
import javax.servlet.ServletException;
6+
import javax.servlet.annotation.WebServlet;
7+
import javax.servlet.http.HttpServlet;
8+
import javax.servlet.http.HttpServletRequest;
9+
import javax.servlet.http.HttpServletResponse;
10+
11+
@WebServlet("/lobby/room/create")
12+
public class CreateRoomFormController extends HttpServlet {
13+
private static final long serialVersionUID = 1L;
14+
15+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
16+
throws ServletException, IOException {
17+
request.getRequestDispatcher("/WEB-INF/views/room/create.jsp")
18+
.forward(request, response);
19+
}
20+
21+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package room.controller;
2+
3+
import java.io.IOException;
4+
import java.util.List;
5+
6+
import javax.servlet.ServletException;
7+
import javax.servlet.annotation.WebServlet;
8+
import javax.servlet.http.HttpServlet;
9+
import javax.servlet.http.HttpServletRequest;
10+
import javax.servlet.http.HttpServletResponse;
11+
12+
import room.dao.RoomDAO;
13+
import room.dao.RoomDAOImpl;
14+
import room.dto.RoomDTO;
15+
16+
@WebServlet("/lobby")
17+
public class ViewRoomListController extends HttpServlet {
18+
private static final long serialVersionUID = 1L;
19+
private final RoomDAO roomDAO = new RoomDAOImpl();
20+
21+
@Override
22+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
23+
throws ServletException, IOException {
24+
try {
25+
List<RoomDTO> roomList = roomDAO.getRoomList();
26+
request.setAttribute("roomList", roomList);
27+
request.getRequestDispatcher("/WEB-INF/views/lobby.jsp").forward(request, response);
28+
return;
29+
} catch (Exception e) {
30+
throw new ServletException("방 목록 조회를 실패하였습니다.", e);
31+
}
32+
}
33+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package room.dao;
2+
3+
import java.util.List;
4+
5+
import room.dto.RoomDTO;
6+
7+
public interface RoomDAO {
8+
9+
List<RoomDTO> getRoomList() throws Exception;
10+
11+
RoomDTO createRoom(
12+
String hostUserId,
13+
String roomName,
14+
String roomPwd,
15+
String isPublic,
16+
String playType) throws Exception;
17+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package room.dao;
2+
3+
import java.sql.Connection;
4+
import java.sql.PreparedStatement;
5+
import java.sql.ResultSet;
6+
import java.sql.SQLException;
7+
import java.sql.Types;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
import room.dto.RoomDTO;
12+
import util.DB;
13+
import util.Uuid;
14+
15+
public class RoomDAOImpl implements RoomDAO {
16+
17+
@Override
18+
public List<RoomDTO> getRoomList() throws Exception {
19+
String query = """
20+
SELECT
21+
id,
22+
host_user_id,
23+
room_name,
24+
is_public,
25+
play_type,
26+
total_user_cnt,
27+
current_user_cnt,
28+
created_at
29+
FROM room
30+
""";
31+
32+
List<RoomDTO> list = new ArrayList<>();
33+
34+
try (
35+
Connection conn = DB.getConnection();
36+
PreparedStatement pstmt = conn.prepareStatement(query);
37+
ResultSet rs = pstmt.executeQuery()) {
38+
while (rs.next()) {
39+
list.add(mapToRoom(rs));
40+
}
41+
} catch (Exception e) {
42+
e.printStackTrace();
43+
}
44+
45+
return list;
46+
}
47+
48+
@Override
49+
public RoomDTO createRoom(String hostUserId, String roomName, String roomPwd, String isPublic, String playType)
50+
throws Exception {
51+
52+
String id = Uuid.generate();
53+
int totalUserCnt = playType.equals("0") ? 2 : 4;
54+
55+
String query = """
56+
INSERT INTO room
57+
(id, host_user_id, room_name, room_pwd, is_public, play_type, current_user_cnt, total_user_cnt)
58+
VALUES
59+
(?, ?, ?, ?, ?, ?, 0, ?)
60+
""";
61+
62+
try (
63+
Connection conn = DB.getConnection();
64+
PreparedStatement pstmt = conn.prepareStatement(query)) {
65+
pstmt.setString(1, id);
66+
pstmt.setString(2, hostUserId);
67+
pstmt.setString(3, roomName);
68+
pstmt.setString(5, isPublic);
69+
pstmt.setString(6, playType);
70+
pstmt.setInt(7, totalUserCnt);
71+
72+
if (isPublic.equals("0")) {
73+
pstmt.setNull(4, Types.VARCHAR);
74+
} else {
75+
pstmt.setString(4, roomPwd);
76+
}
77+
78+
pstmt.executeUpdate();
79+
} catch (Exception e) {
80+
e.printStackTrace();
81+
}
82+
83+
return RoomDTO.builder()
84+
.id(id)
85+
.hostUserId(hostUserId)
86+
.roomName(roomName)
87+
.isPublic(isPublic)
88+
.playType(playType)
89+
.totalUserCnt(totalUserCnt)
90+
.currentUserCnt(0)
91+
.build();
92+
}
93+
94+
private RoomDTO mapToRoom(ResultSet rs) throws SQLException {
95+
return RoomDTO.builder()
96+
.id(rs.getString("id"))
97+
.hostUserId(rs.getString("host_user_id"))
98+
.roomName(rs.getString("room_name"))
99+
.isPublic(rs.getString("is_public"))
100+
.playType(rs.getString("play_type"))
101+
.totalUserCnt(rs.getInt("total_user_cnt"))
102+
.currentUserCnt(rs.getInt("current_user_cnt"))
103+
.build();
104+
}
105+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package room.dto;
2+
3+
import lombok.Builder;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
7+
@Getter
8+
@Setter
9+
@Builder
10+
public class RoomDTO {
11+
private String id;
12+
private String hostUserId;
13+
private String roomName;
14+
private String roomPwd;
15+
private String isPublic; // 0: 공개, 1: 비밀방
16+
private String playType; // 0: 개인, 1: 팀전
17+
private int totalUserCnt;
18+
private int currentUserCnt;
19+
private String createdAt;
20+
}

src/main/java/util/Uuid.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package util;
2+
3+
import java.util.UUID;
4+
5+
public class Uuid {
6+
public static String generate() {
7+
return UUID.randomUUID().toString();
8+
}
9+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<%@ page contentType="text/html; charset=UTF-8" %>
2+
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3+
4+
<!DOCTYPE html>
5+
<html>
6+
<head>
7+
<meta charset="UTF-8">
8+
<title>로비</title>
9+
<link rel="stylesheet"
10+
href="${pageContext.request.contextPath}/static/lobby/lobby.css">
11+
</head>
12+
<body>
13+
14+
<h2>🎮 오목 로비</h2>
15+
16+
<form action="${pageContext.request.contextPath}/lobby/room/create" method="get">
17+
<button type="submit">방 생성</button>
18+
</form>
19+
20+
<br/>
21+
22+
<table>
23+
<thead>
24+
<tr>
25+
<th>방 이름</th>
26+
<th>공개 여부</th>
27+
<th>게임 타입</th>
28+
<th>인원</th>
29+
<th>입장</th>
30+
</tr>
31+
</thead>
32+
33+
<tbody>
34+
<c:choose>
35+
<c:when test="${empty roomList}">
36+
<tr>
37+
<td colspan="5">현재 생성된 방이 없습니다.</td>
38+
</tr>
39+
</c:when>
40+
41+
<c:otherwise>
42+
<c:forEach var="room" items="${roomList}">
43+
<tr>
44+
<td>${room.roomName}</td>
45+
46+
<td>
47+
<c:choose>
48+
<c:when test="${room.isPublic == 1}">공개</c:when>
49+
<c:otherwise>비공개 🔒</c:otherwise>
50+
</c:choose>
51+
</td>
52+
53+
<td>
54+
<c:choose>
55+
<c:when test="${room.playType == 0}">개인전</c:when>
56+
<c:otherwise>팀전</c:otherwise>
57+
</c:choose>
58+
</td>
59+
60+
<td>
61+
${room.currentUserCnt} / ${room.totalUserCnt}
62+
</td>
63+
64+
<td>
65+
<form action="${pageContext.request.contextPath}/room/enter" method="get">
66+
<input type="hidden" name="roomId" value="${room.id}" />
67+
<button type="submit">입장</button>
68+
</form>
69+
</td>
70+
</tr>
71+
</c:forEach>
72+
</c:otherwise>
73+
</c:choose>
74+
</tbody>
75+
</table>
76+
77+
</body>
78+
</html>

0 commit comments

Comments
 (0)