전체적인 패키지 및 파일 생성
1. CourseController
package com.example.basic_crud_10.controller;
import com.example.basic_crud_10.dto.CourseListResponseDto;
import com.example.basic_crud_10.dto.CourseRequestDto;
import com.example.basic_crud_10.dto.CourseResponseDto;
import com.example.basic_crud_10.dto.ResponseDto;
import com.example.basic_crud_10.service.CourseService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class CourseController {
private final CourseService courseService;
@PostMapping("/save/course")
public ResponseDto saveCourse(@RequestBody CourseRequestDto requestDto) {
return courseService.saveCourse(requestDto);
}
@GetMapping("/get/courses")
public CourseListResponseDto getCourses() {
return courseService.getCourses();
}
@GetMapping("/get/course")
public CourseResponseDto getCourse(@RequestParam Long id) {
return courseService.getCourse(id);
}
@PutMapping("/update/course/{id}")
public CourseResponseDto updateCourse(@PathVariable Long id, @RequestBody CourseRequestDto requestDto) {
return courseService.updateCourse(id, requestDto);
}
@DeleteMapping("/delete/course/{id}")
public ResponseDto deleteCourse(@PathVariable Long id){
return courseService.deleteCourse(id);
}
}
2. CourseService
package com.example.basic_crud_10.service;
import com.example.basic_crud_10.dto.CourseListResponseDto;
import com.example.basic_crud_10.dto.CourseRequestDto;
import com.example.basic_crud_10.dto.CourseResponseDto;
import com.example.basic_crud_10.dto.ResponseDto;
import com.example.basic_crud_10.entity.Course;
import com.example.basic_crud_10.repository.CourseRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@RequiredArgsConstructor
public class CourseService {
private final CourseRepository courseRepository;
@Transactional
public ResponseDto saveCourse(CourseRequestDto requestDto) {
courseRepository.save(new Course(requestDto));
return new ResponseDto("강의 등록 성공", HttpStatus.OK.value());
}
@Transactional(readOnly = true)
public CourseListResponseDto getCourses() {
CourseListResponseDto courseListResponseDto = new CourseListResponseDto();
List<Course> courses = courseRepository.findAll();
for (Course course : courses) {
courseListResponseDto.addCourse(new CourseResponseDto(course));
}
return courseListResponseDto;
}
@Transactional(readOnly = true)
public CourseResponseDto getCourse(Long id) {
Course course = checkCourse(id);
return new CourseResponseDto(course);
}
@Transactional
public CourseResponseDto updateCourse(Long id, CourseRequestDto responseDto) {
Course course = checkCourse(id);
course.update(responseDto);
return new CourseResponseDto(course);
}
@Transactional
public ResponseDto deleteCourse(Long id) {
Course course = checkCourse(id);
courseRepository.delete(course);
return new ResponseDto("강의 삭제 성공", HttpStatus.OK.value());
}
private Course checkCourse(Long id) {
return courseRepository.findById(id).orElseThrow(
() -> new RuntimeException("강의를 찾을 수 없다")
);
}
}
3. Course
package com.example.basic_crud_10.entity;
import com.example.basic_crud_10.dto.CourseRequestDto;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Entity
@Getter
@NoArgsConstructor
public class Course {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String instructor;
private Double cost;
public Course(CourseRequestDto requestDto) {
this.title = requestDto.getTitle();
this.instructor = requestDto.getInstructor();
this.cost = requestDto.getCost();
}
public void update(CourseRequestDto responseDto) {
this.title = responseDto.getTitle();
this.cost = responseDtoresponseDto.getCost();
}
}
4. CourseRepository
package com.example.basic_crud_10.repository;
import com.example.basic_crud_10.entity.Course;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CourseRepository extends JpaRepository<Course, Long> {
}
5. CourseRequestDto
package com.example.basic_crud_10.dto;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@NoArgsConstructor
public class CourseRequestDto {
private String title;
private String instructor;
private Double cost;
}
6. ResponseDto
package com.example.basic_crud_10.dto;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Getter
@NoArgsConstructor
public class ResponseDto {
private String msg;
private int statusCode;
public ResponseDto(String msg, int statusCode) {
this.msg = msg;
this.statusCode = statusCode;
}
}
7. CourseResponseDto
package com.example.basic_crud_10.dto;
import com.example.basic_crud_10.entity.Course;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Getter
@NoArgsConstructor
public class CourseResponseDto {
private String title;
private String instructor;
private Double cost;
public CourseResponseDto(Course course) {
this.title = course.getTitle();
this.instructor = course.getInstructor();
this.cost = course.getCost();
}
}
8. CourseListResponseDto
package com.example.basic_crud_10.dto;
import lombok.Getter;
import org.springframework.http.HttpStatus;
import java.util.ArrayList;
import java.util.List;
@Getter
public class CourseListResponseDto extends ResponseDto{
List<CourseResponseDto> courseList = new ArrayList<>();
public CourseListResponseDto(){
super("강의 목록 조회 성공", HttpStatus.OK.value());
}
public void addCourse(CourseResponseDto responseDto) {
courseList.add(responseDto);
}
}