-
Notifications
You must be signed in to change notification settings - Fork 159
[Spring Core] 정민주 미션 제출합니다. #416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: joungminju
Are you sure you want to change the base?
Changes from 13 commits
54ab53c
f6604d5
4b42211
ca5ea51
7bfb606
37d388d
0576925
8e05790
28e6db0
003c111
97f9ec9
049b660
5e028f6
730c33c
d8f1700
31ddc07
46f072f
200ee7c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package roomescape.controller; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import roomescape.entity.Dto.TimeInDto; | ||
import roomescape.entity.value.Time; | ||
import roomescape.service.ReservationService; | ||
|
||
@RestController | ||
@RequestMapping("/times") | ||
public class TimeController { | ||
|
||
private final ReservationService reservationService; | ||
|
||
public TimeController(ReservationService reservationService) { | ||
this.reservationService = reservationService; | ||
} | ||
|
||
@GetMapping | ||
public List<Time> getTimes() { | ||
return reservationService.findAllTimes(); | ||
} | ||
|
||
@Transactional | ||
@PostMapping | ||
public ResponseEntity<Time> createTime(@RequestBody TimeInDto timeInDto) { | ||
final Time time = reservationService.saveTime(timeInDto); | ||
URI location = URI.create("/times/" + time.getId()); | ||
return ResponseEntity.created(location).body(time); | ||
} | ||
|
||
@Transactional | ||
@DeleteMapping("/{id}") | ||
public ResponseEntity<Void> deleteTime(@PathVariable Long id) { | ||
reservationService.deleteTimeById(id); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기도 위와 동일한 리뷰 입니다 |
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package roomescape.entity.Dto; | ||
|
||
public class ReservationInDto { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 dto는 현재 reservation을 생성할 때 이용하는 dto인 것 같습니다. 해당 명칭이 와닿지 않는데 ReservationCreateDto와 같이 의미를 한번에 나타내주는 네이밍으로 관리하는 것은 어떤가요? |
||
|
||
private String name; | ||
private String date; | ||
private Long time; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 필드도 time의 값이 아닌 id 정보를 받아오는 것이기에 필드 명칭으로 timeId로 관리하는 것은 어떤가요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아 화면단에서 요청이 넘어올때 key값이 'time'으로 넘어와서요!! |
||
|
||
public ReservationInDto(String name, String date, Long time) { | ||
this.name = name; | ||
this.date = date; | ||
this.time = time; | ||
} | ||
|
||
public Long getTimeId() { | ||
return time; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getDate() { | ||
return date; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package roomescape.entity.Dto; | ||
|
||
public class ReservationOutDto { | ||
|
||
private Long id; | ||
|
||
private String name; | ||
private String date; | ||
private Long timeId; | ||
|
||
public ReservationOutDto(Long id, String name, String date, Long timeId) { | ||
this.id = id; | ||
this.name = name; | ||
this.date = date; | ||
this.timeId = timeId; | ||
} | ||
|
||
public Long getTimeId() { | ||
return timeId; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getDate() { | ||
return date; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package roomescape.entity.Dto; | ||
|
||
public class TimeInDto { | ||
|
||
private String time; | ||
|
||
private TimeInDto() { | ||
this.time = null; | ||
} | ||
|
||
public TimeInDto(String time) { | ||
this.time = time; | ||
} | ||
|
||
public String getTime() { | ||
return time; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -7,7 +7,8 @@ | |||||||||||||||||||||||||||||||||||||
import org.springframework.jdbc.core.namedparam.SqlParameterSource; | ||||||||||||||||||||||||||||||||||||||
import org.springframework.jdbc.core.simple.SimpleJdbcInsert; | ||||||||||||||||||||||||||||||||||||||
import org.springframework.stereotype.Repository; | ||||||||||||||||||||||||||||||||||||||
import roomescape.entity.Reservation; | ||||||||||||||||||||||||||||||||||||||
import roomescape.entity.Dto.ReservationInDto; | ||||||||||||||||||||||||||||||||||||||
import roomescape.entity.Dto.ReservationOutDto; | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
@Repository | ||||||||||||||||||||||||||||||||||||||
public class ReservationRepository { | ||||||||||||||||||||||||||||||||||||||
|
@@ -22,23 +23,28 @@ public ReservationRepository(JdbcTemplate jdbcTemplate, DataSource source) { | |||||||||||||||||||||||||||||||||||||
.usingGeneratedKeyColumns("id"); | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
public List<Reservation> findAll() { | ||||||||||||||||||||||||||||||||||||||
String sql = "SELECT * FROM reservation"; | ||||||||||||||||||||||||||||||||||||||
public List<ReservationOutDto> findAll() { | ||||||||||||||||||||||||||||||||||||||
String sql = "SELECT \n" + | ||||||||||||||||||||||||||||||||||||||
" r.id as reservation_id, \n" + | ||||||||||||||||||||||||||||||||||||||
" r.name, \n" + | ||||||||||||||||||||||||||||||||||||||
" r.date, \n" + | ||||||||||||||||||||||||||||||||||||||
" t.id as time_id, \n" + | ||||||||||||||||||||||||||||||||||||||
" t.time as time_value \n" + | ||||||||||||||||||||||||||||||||||||||
"FROM reservation as r inner join time as t on r.time_id = t.id\n"; | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
아래 같이 수정하면 가독성이 조금 더 좋아질 것 같습니다. |
||||||||||||||||||||||||||||||||||||||
return jdbcTemplate.query(sql, (rs, rowNum) -> | ||||||||||||||||||||||||||||||||||||||
new Reservation( | ||||||||||||||||||||||||||||||||||||||
rs.getLong("id"), | ||||||||||||||||||||||||||||||||||||||
new ReservationOutDto( | ||||||||||||||||||||||||||||||||||||||
rs.getLong("reservation_id"), | ||||||||||||||||||||||||||||||||||||||
rs.getString("name"), | ||||||||||||||||||||||||||||||||||||||
rs.getString("date"), | ||||||||||||||||||||||||||||||||||||||
rs.getString("time"))); | ||||||||||||||||||||||||||||||||||||||
rs.getLong("time_id"))); | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
public Reservation save(Reservation reservation) { | ||||||||||||||||||||||||||||||||||||||
public Long save(ReservationInDto reservationInDto) { | ||||||||||||||||||||||||||||||||||||||
SqlParameterSource params = new MapSqlParameterSource() | ||||||||||||||||||||||||||||||||||||||
.addValue("name", reservation.getName()) | ||||||||||||||||||||||||||||||||||||||
.addValue("date", reservation.getDate()) | ||||||||||||||||||||||||||||||||||||||
.addValue("time", reservation.getTime()); | ||||||||||||||||||||||||||||||||||||||
long id = simpleJdbcInsert.executeAndReturnKey(params).longValue(); | ||||||||||||||||||||||||||||||||||||||
return new Reservation(id, reservation.getName(), reservation.getDate(), reservation.getTime()); | ||||||||||||||||||||||||||||||||||||||
.addValue("name", reservationInDto.getName()) | ||||||||||||||||||||||||||||||||||||||
.addValue("date", reservationInDto.getDate()) | ||||||||||||||||||||||||||||||||||||||
.addValue("time_id", reservationInDto.getTimeId()); | ||||||||||||||||||||||||||||||||||||||
return simpleJdbcInsert.executeAndReturnKey(params).longValue(); | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
public int deleteById(Long id) { | ||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package roomescape.entity.repository; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import javax.sql.DataSource; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; | ||
import org.springframework.jdbc.core.namedparam.SqlParameterSource; | ||
import org.springframework.jdbc.core.simple.SimpleJdbcInsert; | ||
import org.springframework.stereotype.Repository; | ||
import roomescape.entity.Dto.TimeInDto; | ||
import roomescape.entity.value.Time; | ||
|
||
@Repository | ||
public class TimeRepository { | ||
|
||
private final JdbcTemplate jdbcTemplate; | ||
private final SimpleJdbcInsert simpleJdbcInsert; | ||
|
||
public TimeRepository(JdbcTemplate jdbcTemplate, DataSource source) { | ||
this.jdbcTemplate = jdbcTemplate; | ||
this.simpleJdbcInsert = new SimpleJdbcInsert(source) | ||
.withTableName("time") | ||
.usingGeneratedKeyColumns("id"); | ||
} | ||
|
||
public List<Time> findAll() { | ||
String sql = "SELECT * FROM time"; | ||
return jdbcTemplate.query(sql, (rs, rowNum) -> | ||
new Time( | ||
rs.getLong("id"), | ||
rs.getString("time"))); | ||
} | ||
|
||
public Time save(TimeInDto timeInDto) { | ||
SqlParameterSource params = new MapSqlParameterSource() | ||
.addValue("time", timeInDto.getTime()); | ||
long id = simpleJdbcInsert.executeAndReturnKey(params).longValue(); | ||
return new Time(id, timeInDto.getTime()); | ||
} | ||
|
||
public Optional<Time> findById(Long id) { | ||
String sql = "SELECT * FROM time WHERE id = ?"; | ||
final List<Time> times = jdbcTemplate.query(sql, new Object[]{id}, (rs, rowNum) -> | ||
new Time( | ||
rs.getLong("id"), | ||
rs.getString("time"))); | ||
return times.stream().findFirst(); | ||
} | ||
|
||
public int deleteById(Long id) { | ||
String sql = "DELETE FROM time WHERE id = ?"; | ||
return jdbcTemplate.update(sql, id); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이전 단계에서 적용하신
@Transactional
controller에 있는게 적절할까요? 아니면 새로 생긴 service layer에 있는 것이 적절할까요? 한번 고민해주시고 답해주세요!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
음 get의 경우 굳이 Transactional을 안붙여도 되니, 필요한 메서드(POST, DELETE)에만 명시해주는 것이 더 좋은 구현방식일 것이라 생각했습니다!