|
| 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 | +} |
0 commit comments