-
Notifications
You must be signed in to change notification settings - Fork 119
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
[Spring Core] 김수민 미션 제출합니다. #265
base: boyekim
Are you sure you want to change the base?
Changes from 1 commit
a9004ad
b1a40a3
56c15c3
fd13535
95a28af
930df59
479443e
d0ff629
887148c
1ffa569
b4e3901
6073bad
a828dd1
90e4fcb
27e0f5d
e1e3667
cd2f609
76cabe5
ea519fa
403eca5
75f9754
d772917
a23806c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. TimeController도 요구사항에 맞춰서 잘 짜신 것 같습니다! 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. 감사합니다 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. 여기도 전부 repository에서 service를 호출하셨네요. 굿입니다☺ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package roomescape.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.*; | ||
import roomescape.model.Time; | ||
import roomescape.repository.TimeRepository; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Controller | ||
@RequestMapping("times") | ||
public class TimeController { | ||
|
||
private final TimeRepository timeRepository; | ||
|
||
@ResponseBody | ||
@GetMapping | ||
public List<Time> findTime() { | ||
List<Time> times = timeRepository.findAll(); | ||
log.info("times = {}", times); | ||
return times; | ||
} | ||
|
||
@ResponseBody | ||
@PostMapping | ||
public ResponseEntity<Time> timeAdd(@RequestBody Time time) { | ||
Time resultTime = timeRepository.timeAdd(time); | ||
HttpHeaders headers = new HttpHeaders(); | ||
String uri = "/times/" + resultTime.getId(); | ||
headers.setLocation(URI.create(uri)); | ||
ResponseEntity<Time> response = new ResponseEntity<>(resultTime, headers, HttpStatus.CREATED); | ||
log.info("response = {}", response); | ||
return response; | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
public ResponseEntity<Void> deleteController(@PathVariable int id) { | ||
timeRepository.delete(id); | ||
return new ResponseEntity<>(HttpStatus.NO_CONTENT); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package roomescape.model; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class Time { | ||
int id; | ||
String 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. 이것도 요구사항에 맞춰서 잘 동작하도록 잘 작성하신 것 같습니다. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package roomescape.repository; | ||
|
||
import org.springframework.jdbc.core.BeanPropertyRowMapper; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.jdbc.core.RowMapper; | ||
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource; | ||
import org.springframework.jdbc.core.namedparam.SqlParameterSource; | ||
import org.springframework.jdbc.core.simple.SimpleJdbcInsert; | ||
import roomescape.model.ReservationDTO; | ||
import roomescape.model.Time; | ||
|
||
import javax.sql.DataSource; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class JdbcTimeRepository implements TimeRepository { | ||
|
||
private final SimpleJdbcInsert jdbcInsert; | ||
private final JdbcTemplate jdbcTemplate; | ||
|
||
public JdbcTimeRepository(DataSource dataSource) { | ||
this.jdbcTemplate = new JdbcTemplate(dataSource); | ||
this.jdbcInsert = new SimpleJdbcInsert(dataSource) | ||
.withTableName("time") | ||
.usingGeneratedKeyColumns("id"); | ||
} | ||
|
||
@Override | ||
public Time timeAdd(Time time) { | ||
SqlParameterSource param = new BeanPropertySqlParameterSource(time); | ||
Number key = jdbcInsert.executeAndReturnKey(param); | ||
time.setId(key.intValue()); | ||
return time; | ||
} | ||
|
||
@Override | ||
public List<Time> findAll() { | ||
String sql = "select * from time"; | ||
return jdbcTemplate.query(sql, timeRowMapper()); | ||
} | ||
|
||
private RowMapper<Time> timeRowMapper() { | ||
return BeanPropertyRowMapper.newInstance(Time.class); | ||
} | ||
|
||
@Override | ||
public void delete(int id) { | ||
jdbcTemplate.update("delete from time where id = ?", id); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package roomescape.repository; | ||
|
||
import roomescape.model.Time; | ||
|
||
import java.util.List; | ||
|
||
public interface TimeRepository { | ||
|
||
Time timeAdd(Time time); | ||
|
||
List<Time> findAll(); | ||
|
||
void delete(int id); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,15 @@ | ||
CREATE TABLE reservation | ||
( | ||
id BIGINT NOT NULL AUTO_INCREMENT, | ||
name VARCHAR(255) NOT NULL, | ||
date VARCHAR(255) NOT NULL, | ||
time VARCHAR(255) NOT NULL, | ||
id BIGINT NOT NULL AUTO_INCREMENT, | ||
name VARCHAR(255) NOT NULL, | ||
date VARCHAR(255) NOT NULL, | ||
time VARCHAR(255) NOT NULL, | ||
PRIMARY KEY (id) | ||
); | ||
|
||
CREATE TABLE time | ||
( | ||
id BIGINT NOT NULL AUTO_INCREMENT, | ||
time VARCHAR(255) NOT NULL, | ||
PRIMARY KEY (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.
이렇게 time, reservation, home 빼두신 거 좋은 선택인 것 같아요. 좀 더 세부적으로 나누시려고 빼신 것 같은데 맞을까요?
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.
HomeController 클래스 말씀하시는 것 맞을까요? 이외의 컨트롤러에서 RequestMapping을 써서 좀 더 깔끔하게 만들고자 단순히 페이지 뷰 이름만 반환하는 컨트롤러를 따로 분리하였습니다 ㅎㅎ