feat: Lecture 강의 정보 수정, 내 강의목록 조회 추가

This commit is contained in:
kgc9007 2024-07-19 13:44:32 +09:00
parent 7bf30dcbec
commit c04142afd1
5 changed files with 68 additions and 20 deletions

View File

@ -1,5 +1,6 @@
package com.edufocus.edufocus.lecture.controller; package com.edufocus.edufocus.lecture.controller;
import com.edufocus.edufocus.lecture.entity.Lecture;
import com.edufocus.edufocus.lecture.entity.LectureCreateRequest; import com.edufocus.edufocus.lecture.entity.LectureCreateRequest;
import com.edufocus.edufocus.lecture.entity.LectureSearchResponse; import com.edufocus.edufocus.lecture.entity.LectureSearchResponse;
import com.edufocus.edufocus.lecture.entity.LectureDetailResponse; import com.edufocus.edufocus.lecture.entity.LectureDetailResponse;
@ -24,7 +25,6 @@ public class LectureController {
private final LectureService lectureService; private final LectureService lectureService;
private final JWTUtil jwtUtil; private final JWTUtil jwtUtil;
private final UserService userService;
@PostMapping @PostMapping
public ResponseEntity<?> createLecture(@RequestHeader("Authorization") String accessToken, @RequestBody LectureCreateRequest lectureCreateRequest) { public ResponseEntity<?> createLecture(@RequestHeader("Authorization") String accessToken, @RequestBody LectureCreateRequest lectureCreateRequest) {
@ -34,6 +34,18 @@ public class LectureController {
return new ResponseEntity<>(HttpStatus.CREATED); return new ResponseEntity<>(HttpStatus.CREATED);
} }
@PutMapping("/{lectureId}")
public ResponseEntity<?> updateLecture(@RequestHeader("Authorization") String accessToken, @PathVariable Long lectureId, @RequestBody LectureCreateRequest lectureCreateRequest) {
Long userId = Long.parseLong(jwtUtil.getUserId(accessToken));
if (!lectureService.updateLecture(userId, lectureId, lectureCreateRequest)) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<>(HttpStatus.OK);
}
@DeleteMapping("/{lectureId}") @DeleteMapping("/{lectureId}")
public ResponseEntity<?> deleteLecture(@RequestBody long userId, @PathVariable long lectureId) { public ResponseEntity<?> deleteLecture(@RequestBody long userId, @PathVariable long lectureId) {
if (!lectureService.deleteLecture(userId, lectureId)) { if (!lectureService.deleteLecture(userId, lectureId)) {

View File

@ -3,16 +3,16 @@ package com.edufocus.edufocus.lecture.entity;
import com.edufocus.edufocus.user.model.entity.User; import com.edufocus.edufocus.user.model.entity.User;
import jakarta.persistence.*; import jakarta.persistence.*;
import lombok.Getter; import lombok.*;
import lombok.Setter;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List;
@Entity @Entity
@Getter @Getter
@Setter @Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Lecture { public class Lecture {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Id @GeneratedValue(strategy = GenerationType.IDENTITY)

View File

@ -10,8 +10,10 @@ import java.util.List;
@Repository @Repository
public interface LectureRepository extends JpaRepository<Lecture, Long> { public interface LectureRepository extends JpaRepository<Lecture, Long> {
Lecture findByTitle(@Param("title") String title); Lecture findByTitle(String title);
List<Lecture> findAllByUserId(@Param("userId") Long userId); List<Lecture> findAllByUserId(Long userId);
List<Lecture> findLecturesByUserId(Long userId);
} }

View File

@ -13,6 +13,8 @@ public interface LectureService {
void createLecture(long userId, LectureCreateRequest lectureCreateRequest); void createLecture(long userId, LectureCreateRequest lectureCreateRequest);
boolean updateLecture(long userId, long lectureId, LectureCreateRequest lectureCreateRequest);
boolean deleteLecture(long userId, long LectureId); boolean deleteLecture(long userId, long LectureId);
List<LectureSearchResponse> findAllLecture(); List<LectureSearchResponse> findAllLecture();

View File

@ -8,6 +8,7 @@ import com.edufocus.edufocus.lecture.repository.LectureRepository;
import com.edufocus.edufocus.registration.entity.Registration; import com.edufocus.edufocus.registration.entity.Registration;
import com.edufocus.edufocus.registration.repository.RegistrationRepository; import com.edufocus.edufocus.registration.repository.RegistrationRepository;
import com.edufocus.edufocus.user.model.entity.User; import com.edufocus.edufocus.user.model.entity.User;
import com.edufocus.edufocus.user.model.entity.UserRole;
import com.edufocus.edufocus.user.model.repository.UserRepository; import com.edufocus.edufocus.user.model.repository.UserRepository;
import jakarta.transaction.Transactional; import jakarta.transaction.Transactional;
import lombok.Builder; import lombok.Builder;
@ -31,22 +32,54 @@ public class LectureServiceImpl implements LectureService {
@Override @Override
public void createLecture(long userId, LectureCreateRequest lectureCreateRequest) { public void createLecture(long userId, LectureCreateRequest lectureCreateRequest) {
User user = userRepository.findById(userId).get(); User user = userRepository.findById(userId).get();
Lecture lecture = new Lecture(); Lecture lecture = new Lecture().builder()
lecture.setUser(user); .user(user)
.title(lectureCreateRequest.getTitle())
lecture.setTitle(lectureCreateRequest.getTitle()); .description(lectureCreateRequest.getDescription())
lecture.setDescription(lectureCreateRequest.getDescription()); .image(lectureCreateRequest.getImage())
lecture.setImage(lectureCreateRequest.getImage()); .startDate(lectureCreateRequest.getStartDate())
lecture.setStartDate(lectureCreateRequest.getStartDate()); .endDate(lectureCreateRequest.getEndDate())
lecture.setEndDate(lectureCreateRequest.getEndDate()); .plan(lectureCreateRequest.getPlan())
lecture.setPlan(lectureCreateRequest.getPlan()); .build();
lectureRepository.save(lecture); lectureRepository.save(lecture);
} }
@Override
public boolean updateLecture(long userId, long lectureId, LectureCreateRequest lectureCreateRequest) {
User user = userRepository.findById(userId).get();
Lecture lecture = lectureRepository.findById(lectureId).get();
if (lecture.getUser().getId() != user.getId()) {
return false;
}
if (lectureCreateRequest.getTitle() != null) {
lecture.setTitle(lectureCreateRequest.getTitle());
}
if (lectureCreateRequest.getDescription() != null) {
lecture.setDescription(lectureCreateRequest.getDescription());
}
if (lectureCreateRequest.getImage() != null) {
lecture.setImage(lectureCreateRequest.getImage());
}
if (lectureCreateRequest.getStartDate() != null) {
lecture.setStartDate(lectureCreateRequest.getStartDate());
}
if (lectureCreateRequest.getEndDate() != null) {
lecture.setEndDate(lectureCreateRequest.getEndDate());
}
if (lectureCreateRequest.getPlan() != null) {
lecture.setPlan(lectureCreateRequest.getPlan());
}
lectureRepository.save(lecture);
return true;
}
@Override @Override
public boolean deleteLecture(long userId, long lectureId) { public boolean deleteLecture(long userId, long lectureId) {
Lecture lecture = lectureRepository.findById(lectureId).get(); Lecture lecture = lectureRepository.findById(lectureId).get();
@ -105,9 +138,8 @@ public class LectureServiceImpl implements LectureService {
List<LectureSearchResponse> myLectureList = new ArrayList<>(); List<LectureSearchResponse> myLectureList = new ArrayList<>();
if (user.getRole().equals("ADMIN")) { if (user.getRole() == UserRole.ADMIN) {
List<Lecture> lectureList = lectureRepository.findAllByUserId(userId); List<Lecture> lectureList = lectureRepository.findLecturesByUserId(userId);
for (Lecture lecture : lectureList) { for (Lecture lecture : lectureList) {
LectureSearchResponse lectureSearchResponse = new LectureSearchResponse().builder() LectureSearchResponse lectureSearchResponse = new LectureSearchResponse().builder()
.id(lecture.getId()) .id(lecture.getId())