스프링 강의를 보고 일정 관리를 하는 스프링 과제를 만들기 시작했다
기능 | 메서드 | URL | request | response |
---|---|---|---|---|
일정 작성 | POST | /api/schedules | 요청 body | 등록 정보 |
일정 조회 | GET | /api/schedules/{scheduleId} | 요청 param | 단건 응답 정보 |
일정 목록 조회 | GET | /api/schedules | 요청 param | 다건 응답 정보 |
일정 수정 | PUT | /api/schedules/{scheduleId} | 요청 body | 수정 정보 |
일정 삭제 | DELETE | /api/schedules/{scheduleId} | 요청 param | - |
@PostMapping("/schedules")
public ScheduleResponseDto createSchedule(@RequestBody ScheduleRequestDto requestDto) {
Schedule schedule = new Schedule(requestDto);
KeyHolder keyHolder = new GeneratedKeyHolder();
String sql = "INSERT INTO schedule (sd_username, contents, sd_passward) VALUES (?, ?, ?)";
jdbcTemplate.update(con -> {
PreparedStatement preparedStatement = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
preparedStatement.setString(1, schedule.getUsername());
preparedStatement.setString(2, schedule.getContents());
preparedStatement.setString(3, schedule.getPassward());
return preparedStatement;
}, keyHolder);
Long id = keyHolder.getKey().longValue();
schedule.setId(id);
return new ScheduleResponseDto(schedule);
}
@GetMapping("/schedules")
public List<ScheduleResponseDto> getSchedule() {
String sql = "SELECT * FROM schedule";
return jdbcTemplate.query(sql, (rs, rowNum) -> new ScheduleResponseDto(
rs.getLong("sd_id"),
rs.getString("sd_username"),
rs.getString("contents"),
rs.getDate("sd_regdata"),
rs.getDate("sd_modifydate")
));
}
@PutMapping("/schedules")
public ScheduleResponseDto updateSchedule(@RequestBody UpdateDto updateDto) {
String sql = "UPDATE schedule SET sd_username = ?, contents = ? WHERE sd_id = ? AND sd_passward = ?";
jdbcTemplate.update(sql, updateDto.getUsername(), updateDto.getContents(), updateDto.getId(), updateDto.getPasswards());
sql = "SELECT * FROM schedule WHERE sd_id = ?";
return jdbcTemplate.query(sql, resultSet -> {
if (resultSet.next()) {
return new ScheduleResponseDto(
resultSet.getLong("sd_id"),
resultSet.getString("sd_username"),
resultSet.getString("contents"),
resultSet.getDate("sd_regdata"),
resultSet.getDate("sd_modifydate")
);
} else {
return null;
}
}, updateDto.getId());
}
@DeleteMapping("/schedules/{id}/{passward}")
public Long deleteSchedule(@PathVariable Long id, @PathVariable String passward) {
Schedule schedule = findById(id);
if (schedule != null) {
String sql = "DELETE FROM schedule WHERE sd_id = ? AND sd_passward = ?";
jdbcTemplate.update(sql, id, passward);
return id;
} else {
throw new IllegalArgumentException("선택한 일정은 존재하지 않습니다.");
}
}
@GetMapping("/schedules/read/{id}")
public Schedule findById(@PathVariable Long id) {
String sql = "SELECT * FROM schedule WHERE sd_id = ?";
return jdbcTemplate.query(sql, resultSet -> {
if (resultSet.next()) {
Schedule schedule = new Schedule();
schedule.setUsername(resultSet.getString("sd_username"));
schedule.setContents(resultSet.getString("contents"));
schedule.setCreationDate(resultSet.getDate("sd_regdata"));
schedule.setModificationDate(resultSet.getDate("sd_modifydate"));
return schedule;
} else {
return null;
}
}, id);
}
package com.sparta.springfirst.dto;
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
public class ScheduleRequestDto {
private String username;
private String contents;
private String passward;
private Long creationDate;
private Long modificationDate;
public ScheduleRequestDto(){
}
public ScheduleRequestDto(ScheduleRequestDto scheduleRequestDto) {
this.passward = scheduleRequestDto.getPassward();
this.contents = scheduleRequestDto.getContents();
this.username = scheduleRequestDto.getUsername();
}
}
package com.sparta.springfirst.dto;
import com.sparta.springfirst.entity.Schedule;
import lombok.Getter;
import java.util.Date;
@Getter
public class ScheduleResponseDto {
private Long id;
private String username;
private String contents;
private Date creationDate;
private Date modificationDate;
public ScheduleResponseDto(Schedule schedule) {
this.id = schedule.getId();
this.username = schedule.getUsername();
this.contents = schedule.getContents();
}
public ScheduleResponseDto(Long id, String username, String contents, Date creationDate, Date modificationDate) {
this.id = id;
this.username = username;
this.contents = contents;
this.creationDate = creationDate;
this.modificationDate = modificationDate;
}
}
package com.sparta.springfirst.dto;
import lombok.Getter;
import lombok.Setter;
@Getter
public class UpdateDto {
private Long id;
private String username;
private String contents;
private String passwards;
}
package com.sparta.springfirst.entity;
import com.sparta.springfirst.dto.ScheduleRequestDto;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.util.Date;
@Getter
@Setter
@NoArgsConstructor
public class Schedule {
private Long id;
private String username;
private String contents;
private String passward;
private Date creationDate;
private Date modificationDate;
public Schedule(ScheduleRequestDto requestDto) {
this.username = requestDto.getUsername();
this.contents = requestDto.getContents();
this.passward = requestDto.getPassward();
}
public void update(ScheduleRequestDto requestDto) {
this.username = requestDto.getUsername();
this.contents = requestDto.getContents();
this.passward = requestDto.getPassward();
}
}
과제 기본 구현 부분을 진행하면서 제일 문제가 많았던 부분은 테이블 명과 qury
문에서 작성한 이름이 오타가 많이 나서 에러가 났던 부분이 진행하면서 가장 많은 시간을 사용했던 것 같다. 앞으로 이 부분을 정말 주의해야 겠다. 이 부분은 검색으로도 잘 나오지 않는 사소하지만 개인의 문제라 찾을 수 없어서 더욱더 많은 시간을 쓰게 했던 문제였다.
스스로 정말 주의하자!!
git hub
https://github.com/Sangmin1999/schedule/commits/main/