Skip to content

Commit

Permalink
refactor: new-reservation.js 코드 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
haeyoon1 committed Dec 19, 2024
1 parent fb70989 commit 6d9acfd
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/main/java/roomescape/dao/ReservationDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ public List<Reservation> findAll() {


public Reservation insert(Reservation reservation) {
String sql = "INSERT INTO reservation(name, date, time_id) VALUES (?, ?, ?)";

String sql = "INSERT INTO reservation(name, date, time_id) VALUES (?, ?, ?)";
jdbcTemplate.update(sql, reservation.getName(), reservation.getDate(), reservation.getTime().getId());

String query = "SELECT id FROM reservation ORDER BY id DESC LIMIT 1";
Long id = jdbcTemplate.queryForObject(query, Long.class);
//

return new Reservation(id, reservation.getName(), reservation.getDate(), reservation.getTime().getTime());
}

Expand Down
15 changes: 12 additions & 3 deletions src/main/java/roomescape/dao/TimeDao.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package roomescape.dao;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;
import roomescape.entity.Time;

import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.List;

@Repository
Expand All @@ -24,10 +28,15 @@ public List<Time> findAll() {

public Time insert(Time time) {
String sql = "INSERT INTO time(time) VALUES (?)";
jdbcTemplate.update(sql, time.getTime());
KeyHolder keyHolder = new GeneratedKeyHolder();

String query = "SELECT id FROM time ORDER BY id DESC LIMIT 1";
Long id = jdbcTemplate.queryForObject(query, Long.class);
jdbcTemplate.update(connection -> {
PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
ps.setString(1, time.getTime());
return ps;
}, keyHolder);

Long id = keyHolder.getKey().longValue();

return new Time(id, time.getTime());
}
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/roomescape/dto/TimeRequestDto.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package roomescape.dto;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class TimeRequestDto {
private String time;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/roomescape/entity/Reservation.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public Time getTime() {
}

public LocalTime getTimeAsLocalTime() {
return time.getTimeASLocalTime();
return time.getTimeAsLocalTime();
}

}
10 changes: 7 additions & 3 deletions src/main/java/roomescape/entity/Time.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package roomescape.entity;

import java.time.DateTimeException;
import java.time.LocalTime;

public class Time {
Expand All @@ -23,8 +24,11 @@ public String getTime() {
return time;
}

public LocalTime getTimeASLocalTime(){ //string -> localtime
return LocalTime.parse(time);
public LocalTime getTimeAsLocalTime(){ //string -> localtime
try {
return LocalTime.parse(time);
} catch (DateTimeException e) {
return LocalTime.parse(time + ":00"); // 초
}
}

}
13 changes: 1 addition & 12 deletions src/main/java/roomescape/service/ReservationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import roomescape.repository.TimeRepository;

import java.time.DateTimeException;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -45,7 +43,6 @@ public ReservationResponseDto createReservation(ReservationRequestDto requestDto
time
);
Reservation newReservation = reservationRepository.save(reservation);
System.out.println("new Reservation" + newReservation.toString());

return toResponseDto(newReservation);
}
Expand All @@ -63,18 +60,10 @@ private ReservationResponseDto toResponseDto(Reservation reservation) {
);
}

// private void validateTimeFormat (String time) {
// try {
// LocalTime.parse(time);
// } catch (DateTimeException e){
// throw new IllegalArgumentException("Invalid time format.");
// }
// }

private Long parseTimeId(String time) { // String -> long
try {
return Long.parseLong(time);
} catch (NumberFormatException e) {
} catch (DateTimeException e) {
throw new IllegalArgumentException("Invalid time id format: " + time);
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/roomescape/service/TimeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import java.time.LocalTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TimeService {
Expand All @@ -24,15 +23,15 @@ public List<TimeResponseDto> findAllTimes() {
.stream()
.map(time -> new TimeResponseDto(
time.getId(),
time.getTimeASLocalTime()))
.collect(Collectors.toList());
time.getTimeAsLocalTime()))
.toList();
}

public TimeResponseDto createTime(TimeRequestDto requestDto) {
Time time = new Time(requestDto.getTime());
Time newTime = timeRespository.save(time);

return toResponseDTO(newTime.getId(), newTime.getTimeASLocalTime());
return toResponseDTO(newTime.getId(), newTime.getTimeAsLocalTime());
}

private TimeResponseDto toResponseDTO(Long id, LocalTime time) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/static/js/new-reservation.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ function renderReservations(data) {
}

function insertReservationRow(row, reservation) {
['id', 'name', 'date'].forEach((field, index) => {
['id', 'name', 'date', 'time'].forEach((field, index) => {
row.insertCell(index).textContent = reservation[field];
});

row.insertCell(3).textContent = reservation.time.time;
row.insertCell(4).textContent = reservation.time.time;

const actionCell = row.insertCell(4);
const actionCell = row.insertCell(5);
actionCell.appendChild(createActionButton('삭제', 'btn-danger', deleteRow));
}

Expand Down

0 comments on commit 6d9acfd

Please sign in to comment.