Skip to content
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

taehyeon 깃 충돌 test #41

Open
wants to merge 21 commits into
base: 8th
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions taehyeon/onboarding/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ build/
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
./src/main/resources/**

### IntelliJ IDEA ###
.idea
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.validation.Valid;
import jpabook.onboarding.course.controller.dto.request.CourseRequestDto;
import jpabook.onboarding.course.controller.dto.request.CourseUpdateRequestDto;
import jpabook.onboarding.course.controller.dto.response.CourseResponseDto;
Expand All @@ -20,30 +21,31 @@
@RequestMapping("/courses")
@RequiredArgsConstructor
public class CourseController {
private final CourseService service;
private final CourseService courseService;

@PostMapping
public ResponseEntity<CourseResponseDto> createCourse(@RequestBody final CourseRequestDto request) {
final CourseResponseDto response = service.create(request);
public ResponseEntity<CourseResponseDto> createCourse(@Valid @RequestBody final CourseRequestDto request) {
final CourseResponseDto response = courseService.create(request);

return ResponseEntity.status(HttpStatus.CREATED).body(response);
}

@PutMapping("/{courseId}")
public ResponseEntity<CourseResponseDto> updateCourse(@PathVariable final Long courseId,
@RequestBody final CourseUpdateRequestDto request) {
final CourseResponseDto response = service.update(courseId, request);
@Valid @RequestBody final CourseUpdateRequestDto request) {
final CourseResponseDto response = courseService.update(courseId, request);
return ResponseEntity.status(HttpStatus.OK).body(response);
}

@PatchMapping("/delete/{courseId}")
public ResponseEntity<CourseResponseDto> deleteCourse(@PathVariable final Long courseId) {
final CourseResponseDto response = service.delete(courseId);
final CourseResponseDto response = courseService.delete(courseId);
return ResponseEntity.status(HttpStatus.OK).body(response);
}

@PatchMapping("/complete/{courseId}")
public ResponseEntity<CourseResponseDto> completeCourse(@PathVariable final Long courseId) {
final CourseResponseDto response = service.complete(courseId);
final CourseResponseDto response = courseService.complete(courseId);
return ResponseEntity.status(HttpStatus.OK).body(response);
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package jpabook.onboarding.course.controller.dto.request;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class CourseRequestDto {
@NotBlank(message = "교수 이름은 필수입니다.")
private final String professorName;

@NotBlank(message = "강의 이름은 필수입니다.")
private final String name;

@NotBlank(message = "현재 수강 인원은 필수입니다.")
@NotNull(message = "현재 수강 인원은 필수입니다.")
private final int count;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jpabook.onboarding.course.controller.dto.request;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jpabook.onboarding.data.status.CourseStatus;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
Expand All @@ -15,9 +16,9 @@ public class CourseUpdateRequestDto {
@NotBlank(message = "강의 이름은 필수입니다.")
private final String name;

@NotBlank(message = "학점은 필수입니다.")
@NotNull(message = "학점은 필수입니다.")
private final int grade;

@NotBlank(message = "강의 상태는 필수입니다.")
@NotNull(message = "강의 상태는 필수입니다.")
private final CourseStatus status;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package jpabook.onboarding.course.service;

import java.util.Optional;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -9,54 +9,78 @@
import jpabook.onboarding.course.controller.dto.request.CourseUpdateRequestDto;
import jpabook.onboarding.course.controller.dto.response.CourseResponseDto;
import jpabook.onboarding.data.entity.Course;
import jpabook.onboarding.data.entity.Sugang;
import jpabook.onboarding.data.repository.CourseRepository;
import jpabook.onboarding.data.repository.SugangRepository;
import jpabook.onboarding.data.status.CourseStatus;
import jpabook.onboarding.data.status.SugangStatus;
import jpabook.onboarding.exception.CustomError;
import jpabook.onboarding.exception.CustomException;
import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class CourseServiceImpl implements CourseService {
private final CourseRepository repository;
private final CourseRepository courseRepository;
private final SugangRepository sugangRepository;

@Override
public CourseResponseDto create(final CourseRequestDto request) {
if (courseRepository.findByNameAndProfessorName(request.getName(), request.getProfessorName()).isPresent()) {
throw new CustomException(CustomError.COURSE_CREATE);
}
final Course course = new Course(request);
repository.save(course);
courseRepository.save(course);
return new CourseResponseDto(course);
}

@Transactional
@Override
public CourseResponseDto complete(final Long courseId) {
final Optional<Course> course = repository.findById(courseId);
if (course.isEmpty()) {
return null;
public CourseResponseDto update(final Long courseId, final CourseUpdateRequestDto request) {
final Course course = courseRepository.findById(courseId)
.orElseThrow(() -> new CustomException(CustomError.COURSE_NOT_FOUND));
if (course.getStatus() != request.getStatus()) {
if (request.getStatus() == CourseStatus.COMPLETED) {
complete(courseId);
}
if (request.getStatus() == CourseStatus.DELETED) {
delete(courseId);
}
}
course.get().updateStatus(CourseStatus.COMPLETED);
return new CourseResponseDto(course.get());
course.update(request);
return new CourseResponseDto(course);
}

@Transactional
@Override
public CourseResponseDto update(final Long courseId, final CourseUpdateRequestDto request) {
final Optional<Course> course = repository.findById(courseId);
if (course.isEmpty()) {
final Course newCourse = repository.save(new Course(request));
return new CourseResponseDto(newCourse);
public CourseResponseDto complete(final Long courseId) {
final Course course = courseRepository.findById(courseId)
.orElseThrow(() -> new CustomException(CustomError.COURSE_NOT_FOUND));
if (course.getStatus() != CourseStatus.REGISTERED) {
throw new CustomException(CustomError.COURSE_COMPLETE);
}
course.get().update(request);
return new CourseResponseDto(course.get());
final List<Sugang> sugangs = sugangRepository.findAllByCourseIdAndStatus(courseId, SugangStatus.ONGOING);
for (final Sugang sugang : sugangs) {
sugang.getStudent().addTotalGrade(course.getGrade());
}
course.updateStatus(CourseStatus.COMPLETED);
return new CourseResponseDto(course);
}

@Transactional
@Override
public CourseResponseDto delete(final Long courseId) {
final Optional<Course> course = repository.findById(courseId);
if (course.isEmpty()) {
return null;
final Course course = courseRepository.findById(courseId)
.orElseThrow(() -> new CustomException(CustomError.COURSE_NOT_FOUND));
if (course.getStatus() != CourseStatus.REGISTERED) {
throw new CustomException(CustomError.COURSE_DELETE);
}
course.get().updateStatus(CourseStatus.DELETED);
return new CourseResponseDto(course.get());

final List<Sugang> sugangs = sugangRepository.findAllByCourseIdAndStatus(courseId, SugangStatus.ONGOING);
for (final Sugang sugang : sugangs) {
sugang.updateStatus(SugangStatus.CANCELED);
sugang.getStudent().addCurrentGrade(-course.getGrade());
}
course.updateStatus(CourseStatus.DELETED);
return new CourseResponseDto(course);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
import jpabook.onboarding.course.controller.dto.request.CourseRequestDto;
import jpabook.onboarding.course.controller.dto.request.CourseUpdateRequestDto;
import jpabook.onboarding.data.status.CourseStatus;
import jpabook.onboarding.exception.CustomError;
import jpabook.onboarding.exception.CustomException;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Entity
@Getter
Expand Down Expand Up @@ -47,20 +48,20 @@ public class Course {
@JdbcTypeCode(SqlTypes.VARCHAR)
private CourseStatus status;

public Course(CourseRequestDto request) {
public Course(final CourseRequestDto request) {
this.professorName = request.getProfessorName();
this.name = request.getName();
this.count = request.getCount();
this.grade = 3;
this.status = CourseStatus.REGISTERED;
}

public Course(CourseUpdateRequestDto request) {
this.professorName = request.getProfessorName();
this.name = request.getName();
public Course(String professorName, String name) {
this.professorName = professorName;
this.name = name;
this.count = 0;
this.grade = request.getGrade();
this.status = request.getStatus();
this.grade = 3;
this.status = CourseStatus.REGISTERED;
}

public void updateStatus(final CourseStatus status) {
Expand All @@ -73,4 +74,11 @@ public void update(final CourseUpdateRequestDto request) {
this.professorName = request.getProfessorName();
this.status = request.getStatus();
}

public void addCount(final int count) {
if (this.count + count > MAX_COUNT) {
throw new CustomException(CustomError.BAD_REQUEST);
}
this.count += count;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package jpabook.onboarding.data.entity;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.HashSet;
import java.util.Set;

import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.type.SqlTypes;
Expand All @@ -17,6 +17,8 @@
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jpabook.onboarding.data.status.StudentStatus;
import jpabook.onboarding.exception.CustomError;
import jpabook.onboarding.exception.CustomException;
import jpabook.onboarding.student.controller.dto.request.StudentRequestDto;
import lombok.AccessLevel;
import lombok.Getter;
Expand All @@ -26,13 +28,17 @@
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Student {

private static final int MAX_CURRENT_GRADE = 15;
private static final int MAX_TOTAL_GRADE = 60;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "student_id")
private Long id;

@OneToMany(mappedBy = "student", cascade = CascadeType.ALL)
private List<Sugang> sugangs = new ArrayList<>();
private Set<Sugang> sugangs = new HashSet<>();

@Column(nullable = false)
private String name;
Expand All @@ -51,7 +57,7 @@ public class Student {
@JdbcTypeCode(SqlTypes.VARCHAR)
private StudentStatus status;

public Student(StudentRequestDto request) {
public Student(final StudentRequestDto request) {
this.name = request.getName();
this.birth = request.getBirth();
this.status = StudentStatus.ENROLLED;
Expand All @@ -60,4 +66,21 @@ public Student(StudentRequestDto request) {
public void updateStatus(final StudentStatus status) {
this.status = status;
}

public void addCurrentGrade(final int grade) {
if (this.currentGrade + grade > MAX_CURRENT_GRADE) {
throw new CustomException(CustomError.BAD_REQUEST);
}
this.currentGrade += grade;
}

public void addTotalGrade(final int grade) {
if (this.totalGrade == MAX_TOTAL_GRADE) {
throw new CustomException(CustomError.BAD_REQUEST);
}
this.totalGrade = Integer.min(this.totalGrade + grade, MAX_TOTAL_GRADE);
if (this.totalGrade == MAX_TOTAL_GRADE) {
this.status = StudentStatus.GRADUATED;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package jpabook.onboarding.data.repository;

import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import jpabook.onboarding.data.entity.Course;
import jpabook.onboarding.data.status.CourseStatus;

@Repository
public interface CourseRepository extends JpaRepository<Course, Long> {
Optional<Course> findByIdAndStatus(Long id, CourseStatus status);

Optional<Course> findByNameAndProfessorName(String name, String professorName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@
public interface StudentRepository extends JpaRepository<Student, Long> {
Optional<Student> findByNameAndBirth(String name, LocalDate birth);

Optional<Student> findByNameAndBirthAndStatus(String name, LocalDate birth, StudentStatus status);

Page<Student> findAllByStatus(StudentStatus status, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package jpabook.onboarding.data.repository;

import java.util.List;
import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -8,8 +9,11 @@
import jpabook.onboarding.data.entity.Course;
import jpabook.onboarding.data.entity.Student;
import jpabook.onboarding.data.entity.Sugang;
import jpabook.onboarding.data.status.SugangStatus;

@Repository
public interface SugangRepository extends JpaRepository<Sugang, Long> {
Optional<Sugang> findByStudentAndCourse(Student student, Course course);

List<Sugang> findAllByCourseIdAndStatus(Long courseId, SugangStatus status);
}
Loading