Merge pull request #55 from TeamBNBN/be/Lecture

feat: Lecture 강의 정보 수정, 내 강의목록 조회 추가
This commit is contained in:
Jungmin 2024-07-19 13:49:14 +09:00 committed by GitHub
commit 787fd419ec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 68 additions and 20 deletions

View File

@ -1,5 +1,6 @@
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.LectureSearchResponse;
import com.edufocus.edufocus.lecture.entity.LectureDetailResponse;
@ -24,7 +25,6 @@ public class LectureController {
private final LectureService lectureService;
private final JWTUtil jwtUtil;
private final UserService userService;
@PostMapping
public ResponseEntity<?> createLecture(@RequestHeader("Authorization") String accessToken, @RequestBody LectureCreateRequest lectureCreateRequest) {
@ -34,6 +34,18 @@ public class LectureController {
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}")
public ResponseEntity<?> deleteLecture(@RequestBody long userId, @PathVariable long 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 jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import lombok.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Entity
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Lecture {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)

View File

@ -10,8 +10,10 @@ import java.util.List;
@Repository
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);
boolean updateLecture(long userId, long lectureId, LectureCreateRequest lectureCreateRequest);
boolean deleteLecture(long userId, long LectureId);
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.repository.RegistrationRepository;
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 jakarta.transaction.Transactional;
import lombok.Builder;
@ -31,22 +32,54 @@ public class LectureServiceImpl implements LectureService {
@Override
public void createLecture(long userId, LectureCreateRequest lectureCreateRequest) {
User user = userRepository.findById(userId).get();
Lecture lecture = new Lecture();
lecture.setUser(user);
lecture.setTitle(lectureCreateRequest.getTitle());
lecture.setDescription(lectureCreateRequest.getDescription());
lecture.setImage(lectureCreateRequest.getImage());
lecture.setStartDate(lectureCreateRequest.getStartDate());
lecture.setEndDate(lectureCreateRequest.getEndDate());
lecture.setPlan(lectureCreateRequest.getPlan());
Lecture lecture = new Lecture().builder()
.user(user)
.title(lectureCreateRequest.getTitle())
.description(lectureCreateRequest.getDescription())
.image(lectureCreateRequest.getImage())
.startDate(lectureCreateRequest.getStartDate())
.endDate(lectureCreateRequest.getEndDate())
.plan(lectureCreateRequest.getPlan())
.build();
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
public boolean deleteLecture(long userId, long lectureId) {
Lecture lecture = lectureRepository.findById(lectureId).get();
@ -105,9 +138,8 @@ public class LectureServiceImpl implements LectureService {
List<LectureSearchResponse> myLectureList = new ArrayList<>();
if (user.getRole().equals("ADMIN")) {
List<Lecture> lectureList = lectureRepository.findAllByUserId(userId);
if (user.getRole() == UserRole.ADMIN) {
List<Lecture> lectureList = lectureRepository.findLecturesByUserId(userId);
for (Lecture lecture : lectureList) {
LectureSearchResponse lectureSearchResponse = new LectureSearchResponse().builder()
.id(lecture.getId())