Skip to content

Commit

Permalink
Refactor : 전반적인 코드 리팩토링
Browse files Browse the repository at this point in the history
1. POSIX New Line 적용
2. 컨트롤러 객체에 빌더 스타일 적용으로 가독성 향상
3. 보안성을 위해 엔티티 객체들의 @NoArgsConstructor 어노테이션에 (access = AccessLevel.PROTECTED) 속성 추가
4. 그외 개행, import문 정리 등 코드 스타일 개선
  • Loading branch information
TwooTwoo committed Nov 5, 2024
1 parent 15d90f4 commit 326721c
Show file tree
Hide file tree
Showing 41 changed files with 654 additions and 688 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,36 @@ public class CourseController {
// 강의 생성
@PostMapping
public ResponseEntity<CourseInfoResponseDto> saveCourse(@RequestBody CourseSaveRequestDto dto) {
return new ResponseEntity<>(courseService.save(dto), HttpStatus.CREATED);
return ResponseEntity.status(HttpStatus.CREATED)
.body(courseService.save(dto));
}

// courseId 로 강의 조회
@GetMapping("/{courseId}")
public ResponseEntity<CourseInfoResponseDto> findByCourseId(@PathVariable Long courseId) {
return new ResponseEntity<>(courseService.findByCourseId(courseId), HttpStatus.OK);
return ResponseEntity.status(HttpStatus.OK)
.body(courseService.findByCourseId(courseId));
}

// 전체 강의 조회
@GetMapping
public ResponseEntity<CourseListResponseDto> findAllCourses() {
return new ResponseEntity<>(courseService.findAllCourses(), HttpStatus.OK);
return ResponseEntity.status(HttpStatus.OK)
.body(courseService.findAllCourses());
}

// courseId 로 강의 수정
@PatchMapping("/{courseId}")
public ResponseEntity<CourseInfoResponseDto> updateByCourseId(@PathVariable Long courseId
, @RequestBody CourseSaveRequestDto dto) {
return new ResponseEntity<>(courseService.updateByCourseId(courseId, dto), HttpStatus.OK);
public ResponseEntity<CourseInfoResponseDto> updateByCourseId(@PathVariable Long courseId, @RequestBody CourseSaveRequestDto dto) {
return ResponseEntity.status(HttpStatus.OK)
.body(courseService.updateByCourseId(courseId, dto));
}

// courseId 로 강의 삭제
@DeleteMapping("/{courseId}")
public ResponseEntity<String> deleteByCourseId(@PathVariable Long courseId) {
courseService.deleteByCourseId(courseId);
String deleteResponseMessage = courseId + "번 강의가 성공적으로 삭제되었습니다.";
return new ResponseEntity<>(deleteResponseMessage, HttpStatus.OK);
return ResponseEntity.status(HttpStatus.OK)
.body(courseService.deleteByCourseId(courseId));
}

}
}
Original file line number Diff line number Diff line change
@@ -1,58 +1,52 @@
package org.example.gdgweek4assignmentktw.controller;

import lombok.RequiredArgsConstructor;
import org.example.gdgweek4assignmentktw.domain.Course;
import org.example.gdgweek4assignmentktw.domain.Student;
import org.example.gdgweek4assignmentktw.dto.courseRegistration.request.CourseRegistrationRequestDto;
import org.example.gdgweek4assignmentktw.dto.courseRegistration.response.CourseRegistrationListResponseDto;
import org.example.gdgweek4assignmentktw.dto.courseRegistration.response.CourseRegistrationResponseDto;
import org.example.gdgweek4assignmentktw.repository.CourseRepository;
import org.example.gdgweek4assignmentktw.repository.StudentRepository;
import org.example.gdgweek4assignmentktw.service.CourseRegistrationService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/courseRegistration")
public class CourseRegistrationController {

private final CourseRegistrationService courseRegistrationService;
private final CourseRepository courseRepository;
private final StudentRepository studentRepository;

// 수강신청 등록
@PostMapping
public ResponseEntity<CourseRegistrationResponseDto> doRegistration(@RequestBody CourseRegistrationRequestDto dto) {
CourseRegistrationResponseDto responseDto = courseRegistrationService.doRegistration(dto);
return new ResponseEntity<>(responseDto, HttpStatus.OK);
}

// courseRegistrationId 로 수강신청 조회
@GetMapping("/{courseRegistrationId}")
public ResponseEntity<CourseRegistrationResponseDto> checkRegistration(@PathVariable Long courseRegistrationId) {
return new ResponseEntity<>(courseRegistrationService.checkByCourseRegistrationId(courseRegistrationId),HttpStatus.OK);
}

// 전체 수강신청 조회
@GetMapping("/findAll")
public ResponseEntity<CourseRegistrationListResponseDto> findAllRegistrations() {
return new ResponseEntity<>(courseRegistrationService.findAllRegistrations(), HttpStatus.OK);
}

// 수강신청 취소
@DeleteMapping("/{courseRegistrationId}")
public ResponseEntity<String> cancelRegistration(@PathVariable Long courseRegistrationId) {
courseRegistrationService.cancelByCourseRegistrationId(courseRegistrationId);
String cancelResponseMessage = "요청하신 " + courseRegistrationId + "번 수강신청의 취소가 완료되었습니다";
return new ResponseEntity<>(cancelResponseMessage,HttpStatus.OK);
}

// 학번을 통해 해당 학생의 수강신청 내역 조회
@GetMapping("/findAll/{studentNumber}")
public ResponseEntity<CourseRegistrationListResponseDto> findAllRegistrationsByStudnetNumber(@PathVariable Long studentNumber) {
return new ResponseEntity<>(courseRegistrationService.findAllRegistrationsByStudentNumber(studentNumber),HttpStatus.OK);
}

}
package org.example.gdgweek4assignmentktw.controller;

import lombok.RequiredArgsConstructor;
import org.example.gdgweek4assignmentktw.dto.courseRegistration.request.CourseRegistrationRequestDto;
import org.example.gdgweek4assignmentktw.dto.courseRegistration.response.CourseRegistrationListResponseDto;
import org.example.gdgweek4assignmentktw.dto.courseRegistration.response.CourseRegistrationResponseDto;
import org.example.gdgweek4assignmentktw.service.CourseRegistrationService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/courseRegistration")
public class CourseRegistrationController {
private final CourseRegistrationService courseRegistrationService;

// 수강신청 등록
@PostMapping
public ResponseEntity<CourseRegistrationResponseDto> doRegistration(@RequestBody CourseRegistrationRequestDto dto) {
return ResponseEntity.ok()
.body(courseRegistrationService.doRegistration(dto));
}

// courseRegistrationId 로 수강신청 조회
@GetMapping("/{courseRegistrationId}")
public ResponseEntity<CourseRegistrationResponseDto> checkRegistration(@PathVariable Long courseRegistrationId) {
return ResponseEntity.ok()
.body(courseRegistrationService.checkByCourseRegistrationId(courseRegistrationId));
}

// 전체 수강신청 조회
@GetMapping("/findAll")
public ResponseEntity<CourseRegistrationListResponseDto> findAllRegistrations() {
return ResponseEntity.ok()
.body(courseRegistrationService.findAllRegistrations());
}

// 수강신청 취소
@DeleteMapping("/{courseRegistrationId}")
public ResponseEntity<String> cancelRegistration(@PathVariable Long courseRegistrationId) {
return ResponseEntity.ok()
.body(courseRegistrationService.cancelByCourseRegistrationId(courseRegistrationId));
}

// 학번을 통해 해당 학생의 수강신청 내역 조회
@GetMapping("/findAll/{studentNumber}")
public ResponseEntity<CourseRegistrationListResponseDto> findAllRegistrationsByStudnetNumber(@PathVariable Long studentNumber) {
return ResponseEntity.ok()
.body(courseRegistrationService.findAllRegistrationsByStudentNumber(studentNumber));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.example.gdgweek4assignmentktw.service.StudentService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
Expand All @@ -19,34 +18,36 @@ public class StudentController {
//학생 생성
@PostMapping
public ResponseEntity<StudentInfoResponseDto> saveStudent(@RequestBody StudentSaveRequestDto dto) {
return new ResponseEntity<>(studentService.save(dto), HttpStatus.OK);
return ResponseEntity.status(HttpStatus.CREATED)
.body(studentService.save(dto));
}

// studentId로 학생 조회
@GetMapping("/{studentId}")
public ResponseEntity<StudentInfoResponseDto> findByStudentId(@PathVariable Long studentId) {
return new ResponseEntity<>(studentService.findByStudentId(studentId), HttpStatus.OK);
return ResponseEntity.status(HttpStatus.CREATED)
.body(studentService.findByStudentId(studentId));
}

// 전체 학생 조회
@GetMapping
public ResponseEntity<StudentListResponseDto> findAllStudents() {
return new ResponseEntity<>(studentService.findAllStudents(), HttpStatus.OK);
return ResponseEntity.status(HttpStatus.OK)
.body(studentService.findAllStudents());
}

// studentId로 학생 정보 수정
@PatchMapping("/{studentId}")
public ResponseEntity<StudentInfoResponseDto> updateByStudentId(@PathVariable Long studentId
,@RequestBody StudentSaveRequestDto dto) {
return new ResponseEntity<>(studentService.updateByStudentId(studentId, dto), HttpStatus.OK);
public ResponseEntity<StudentInfoResponseDto> updateByStudentId(@PathVariable Long studentId, @RequestBody StudentSaveRequestDto dto) {
return ResponseEntity.status(HttpStatus.OK)
.body(studentService.updateByStudentId(studentId, dto));
}

// studentId로 학생 삭제
@DeleteMapping("{studentId}")
public ResponseEntity<String> deleteByStudentId(@PathVariable Long studentId) {
studentService.deleteByStudentId(studentId);
String deleteResponseMessage = studentId + "번 학생이 성공적으로 삭제되었습니다";
return new ResponseEntity<>(deleteResponseMessage, HttpStatus.OK);
return ResponseEntity.status(HttpStatus.OK)
.body(studentService.deleteByStudentId(studentId));
}

}
Original file line number Diff line number Diff line change
@@ -1,64 +1,64 @@
package org.example.gdgweek4assignmentktw.domain;

import jakarta.persistence.*;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.example.gdgweek4assignmentktw.dto.course.request.CourseSaveRequestDto;


@Entity
@Getter
@NoArgsConstructor
public class Course {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) // 1씩 자동으로 올라가도록 설정
@Column(name = "course_id")
private Long courseId;

@Column(name = "course_number")
private Long courseNumber;

@Column(name = "course_name")
private String courseName; // 운영체제

@Column(name = "course_room")
private String courseRoom; // M404

@Column(name = "course_time")
private String courseTime; // 12:00 ~ 13:15 와 같이 String으로 저장

@Column(name = " course_professor")
private String courseProfessor;

/*
연관관계의 주인은 별도의 설정을 할 필요 없다.
OneToOne 과 ManyToOne의 fetch 속성의 기본값은 EAGER 이다.
EAGAR 일 때 : lecture을 조회할 때마다 lecture과 연관된 student 정보가 항상 같이 로드된다
LAZY 일 때 : Lecture 만 조회할 때는 student 정보가 로드되지 않고,
실제로 student 필드에 접근하는 시점에 정보가 조회된다
*/
@ManyToOne(fetch = FetchType.LAZY)
private Student student;

@Builder
public Course(Long courseNumber, String courseName, String courseRoom
, String courseTime, String courseProfessor, Student student){
this.courseNumber = courseNumber;
this.courseName = courseName;
this.courseRoom = courseRoom;
this.courseTime = courseTime;
this.courseProfessor = courseProfessor;
this.student = student;
}

public void update(CourseSaveRequestDto dto) {
this.courseNumber = dto.getCourseNumber();
this.courseName = dto.getCourseName();
this.courseRoom = dto.getCourseRoom();
this.courseTime = dto.getCourseTime();
this.courseProfessor = dto.getCourseProfessor();
}

}
package org.example.gdgweek4assignmentktw.domain;

import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.example.gdgweek4assignmentktw.dto.course.request.CourseSaveRequestDto;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Course {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) // 1씩 자동으로 올라가도록 설정
@Column(name = "course_id")
private Long courseId;

@Column(name = "course_number")
private Long courseNumber;

@Column(name = "course_name")
private String courseName; // 운영체제

@Column(name = "course_room")
private String courseRoom; // M404

@Column(name = "course_time")
private String courseTime; // 12:00 ~ 13:15 와 같이 String으로 저장

@Column(name = " course_professor")
private String courseProfessor;

/*
연관관계의 주인은 별도의 설정을 할 필요 없다.
OneToOne 과 ManyToOne의 fetch 속성의 기본값은 EAGER 이다.
EAGAR 일 때 : lecture을 조회할 때마다 lecture과 연관된 student 정보가 항상 같이 로드된다
LAZY 일 때 : Lecture 만 조회할 때는 student 정보가 로드되지 않고,
실제로 student 필드에 접근하는 시점에 정보가 조회된다
*/
@ManyToOne(fetch = FetchType.LAZY)
private Student student;

@Builder
public Course(Long courseNumber, String courseName, String courseRoom
, String courseTime, String courseProfessor, Student student){
this.courseNumber = courseNumber;
this.courseName = courseName;
this.courseRoom = courseRoom;
this.courseTime = courseTime;
this.courseProfessor = courseProfessor;
this.student = student;
}

public void update(CourseSaveRequestDto dto) {
this.courseNumber = dto.getCourseNumber();
this.courseName = dto.getCourseName();
this.courseRoom = dto.getCourseRoom();
this.courseTime = dto.getCourseTime();
this.courseProfessor = dto.getCourseProfessor();
}

}
Loading

0 comments on commit 326721c

Please sign in to comment.