From 6fe65c36ba929347774c5ce810e1faa80f48155a Mon Sep 17 00:00:00 2001 From: NamGu Date: Sat, 29 Oct 2022 18:24:24 +0900 Subject: [PATCH] feat: Add User's lecture API (#14) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: Add lecture entity * feat: Add userlecture entity * feat: Add lecture, userlecture repo * feat: Add all lectures api * feat: Add 'Read User’s Lecture List' api * feat: Add 'Update User’s Lecture List' api * fix: Change word to fit API spec Co-authored-by: ParkSangJun <30718444+cosmoquester@users.noreply.github.com> * fix: Modify duplicated func name Co-authored-by: ParkSangJun <30718444+cosmoquester@users.noreply.github.com> * fix: Change lecture_id to fit API spec * fix: Remove path for authorization Co-authored-by: ParkSangJun <30718444+cosmoquester@users.noreply.github.com> --- .../controllers/LectureController.java | 31 +++++++++++++ .../dokkang/controllers/UserController.java | 43 +++++++++++++++++-- .../com/de_alone/dokkang/models/Lecture.java | 29 +++++++++++++ .../com/de_alone/dokkang/models/User.java | 2 +- .../de_alone/dokkang/models/UserLecture.java | 42 ++++++++++++++++++ .../payload/request/UpdateLectureRequest.java | 12 ++++++ .../payload/response/LectureResponse.java | 18 ++++++++ .../dokkang/repository/LectureRepository.java | 7 +++ .../repository/UserLectureRepository.java | 27 ++++++++++++ 9 files changed, 207 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/de_alone/dokkang/controllers/LectureController.java create mode 100644 src/main/java/com/de_alone/dokkang/models/Lecture.java create mode 100644 src/main/java/com/de_alone/dokkang/models/UserLecture.java create mode 100644 src/main/java/com/de_alone/dokkang/payload/request/UpdateLectureRequest.java create mode 100644 src/main/java/com/de_alone/dokkang/payload/response/LectureResponse.java create mode 100644 src/main/java/com/de_alone/dokkang/repository/LectureRepository.java create mode 100644 src/main/java/com/de_alone/dokkang/repository/UserLectureRepository.java diff --git a/src/main/java/com/de_alone/dokkang/controllers/LectureController.java b/src/main/java/com/de_alone/dokkang/controllers/LectureController.java new file mode 100644 index 0000000..5af2fc6 --- /dev/null +++ b/src/main/java/com/de_alone/dokkang/controllers/LectureController.java @@ -0,0 +1,31 @@ +package com.de_alone.dokkang.controllers; + +import com.de_alone.dokkang.models.Lecture; +import com.de_alone.dokkang.payload.response.LectureResponse; +import com.de_alone.dokkang.repository.LectureRepository; +import com.de_alone.dokkang.security.jwt.JwtUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@CrossOrigin(origins = "*", maxAge = 3600) +@RestController +@RequestMapping("/lectures") +public class LectureController { + @Autowired + LectureRepository lectureRepository; + + @Autowired + JwtUtils jwtUtils; + + @GetMapping + public ResponseEntity getLectures(@RequestParam(required = false) String jwt) { + + List lectures = lectureRepository.findAll(); + + return ResponseEntity.status(HttpStatus.CREATED).body(new LectureResponse("ok", lectures)); + } +} diff --git a/src/main/java/com/de_alone/dokkang/controllers/UserController.java b/src/main/java/com/de_alone/dokkang/controllers/UserController.java index f925cdd..77a022f 100644 --- a/src/main/java/com/de_alone/dokkang/controllers/UserController.java +++ b/src/main/java/com/de_alone/dokkang/controllers/UserController.java @@ -1,19 +1,24 @@ package com.de_alone.dokkang.controllers; +import com.de_alone.dokkang.models.Lecture; import com.de_alone.dokkang.models.User; +import com.de_alone.dokkang.models.UserLecture; import com.de_alone.dokkang.payload.request.SignupRequest; +import com.de_alone.dokkang.payload.request.UpdateLectureRequest; +import com.de_alone.dokkang.payload.response.LectureResponse; import com.de_alone.dokkang.payload.response.SignupResponse; +import com.de_alone.dokkang.repository.LectureRepository; +import com.de_alone.dokkang.repository.UserLectureRepository; import com.de_alone.dokkang.repository.UserRepository; import com.de_alone.dokkang.security.jwt.JwtUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.web.bind.annotation.*; -import org.springframework.http.HttpStatus; - import javax.validation.Valid; - +import java.util.List; @CrossOrigin(origins = "*", maxAge = 3600) @@ -23,6 +28,12 @@ public class UserController { @Autowired UserRepository userRepository; + @Autowired + UserLectureRepository userlectureRepository; + + @Autowired + LectureRepository lectureRepository; + @Autowired PasswordEncoder encoder; @@ -49,4 +60,30 @@ public ResponseEntity registerUser(@Valid @RequestBody SignupRequest signUpRe return ResponseEntity.status(HttpStatus.CREATED).body(new SignupResponse("User registered successfully!")); } + @GetMapping("/{user_id}/lectures") + public ResponseEntity getUserLectures(@RequestParam(required = false) String jwt, @PathVariable Long user_id) { + + List lectures = lectureRepository.findAllById(userlectureRepository.findLectureById(user_id)); + return ResponseEntity.status(HttpStatus.CREATED).body(new LectureResponse("ok", lectures)); + } + + + @PutMapping("/{user_id}/lectures") + public ResponseEntity updateUserLectures(@RequestParam(required = false) String jwt, @PathVariable Long user_id, @Valid @RequestBody UpdateLectureRequest updateLectureRequest) { + + userlectureRepository.deleteLectureById(user_id); + + List lectureIds = updateLectureRequest.getLecture_ids(); + + for(Long lectureId:lectureIds){ + UserLecture userlecture = new UserLecture(); + userlecture.setLectureId(lectureRepository.findById(lectureId) + .orElseThrow(IllegalArgumentException::new)); + userlecture.setUserId(userRepository.findById(user_id) + .orElseThrow(IllegalArgumentException::new)); + userlectureRepository.save(userlecture); + } + + return ResponseEntity.status(HttpStatus.CREATED).body(new SignupResponse("ok")); + } } diff --git a/src/main/java/com/de_alone/dokkang/models/Lecture.java b/src/main/java/com/de_alone/dokkang/models/Lecture.java new file mode 100644 index 0000000..050c1b8 --- /dev/null +++ b/src/main/java/com/de_alone/dokkang/models/Lecture.java @@ -0,0 +1,29 @@ +package com.de_alone.dokkang.models; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import javax.persistence.*; +import javax.validation.constraints.NotBlank; + + +@Entity +@NoArgsConstructor +@AllArgsConstructor +@Getter +public class Lecture { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @NotBlank + private String no; + + @NotBlank + private String name; + + @NotBlank + private String professor; +} diff --git a/src/main/java/com/de_alone/dokkang/models/User.java b/src/main/java/com/de_alone/dokkang/models/User.java index efbe4a5..ab7fde4 100644 --- a/src/main/java/com/de_alone/dokkang/models/User.java +++ b/src/main/java/com/de_alone/dokkang/models/User.java @@ -6,7 +6,7 @@ import javax.validation.constraints.Size; @Entity -@Table(name = "users", +@Table(name = "user", uniqueConstraints = { @UniqueConstraint(columnNames = "username"), @UniqueConstraint(columnNames = "email") diff --git a/src/main/java/com/de_alone/dokkang/models/UserLecture.java b/src/main/java/com/de_alone/dokkang/models/UserLecture.java new file mode 100644 index 0000000..63eed06 --- /dev/null +++ b/src/main/java/com/de_alone/dokkang/models/UserLecture.java @@ -0,0 +1,42 @@ +package com.de_alone.dokkang.models; + +import lombok.AllArgsConstructor; +import lombok.NoArgsConstructor; + +import javax.persistence.*; + + +@Entity +@NoArgsConstructor +@AllArgsConstructor +public class UserLecture { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @ManyToOne + @JoinColumn(name = "user_id") + private User userId; + + @ManyToOne + @JoinColumn(name = "lecture_id") + private Lecture lectureId; + + public User getUserId() { + return userId; + } + + public void setUserId(User userId) { + this.userId = userId; + } + + public Lecture getLectureId() { + return lectureId; + } + + public void setLectureId(Lecture lectureId) { + this.lectureId = lectureId; + } + +} + diff --git a/src/main/java/com/de_alone/dokkang/payload/request/UpdateLectureRequest.java b/src/main/java/com/de_alone/dokkang/payload/request/UpdateLectureRequest.java new file mode 100644 index 0000000..80dcf42 --- /dev/null +++ b/src/main/java/com/de_alone/dokkang/payload/request/UpdateLectureRequest.java @@ -0,0 +1,12 @@ +package com.de_alone.dokkang.payload.request; + +import lombok.Getter; +import lombok.Setter; + +import java.util.List; + +@Getter +@Setter +public class UpdateLectureRequest { + private List lecture_ids; +} diff --git a/src/main/java/com/de_alone/dokkang/payload/response/LectureResponse.java b/src/main/java/com/de_alone/dokkang/payload/response/LectureResponse.java new file mode 100644 index 0000000..0048a43 --- /dev/null +++ b/src/main/java/com/de_alone/dokkang/payload/response/LectureResponse.java @@ -0,0 +1,18 @@ +package com.de_alone.dokkang.payload.response; + +import com.de_alone.dokkang.models.Lecture; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; + +import java.util.List; + +@AllArgsConstructor +@Getter +@Setter +public class LectureResponse { + private String status; + + private List lectures; + +} diff --git a/src/main/java/com/de_alone/dokkang/repository/LectureRepository.java b/src/main/java/com/de_alone/dokkang/repository/LectureRepository.java new file mode 100644 index 0000000..4e4a1db --- /dev/null +++ b/src/main/java/com/de_alone/dokkang/repository/LectureRepository.java @@ -0,0 +1,7 @@ +package com.de_alone.dokkang.repository; + +import com.de_alone.dokkang.models.Lecture; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface LectureRepository extends JpaRepository { +} diff --git a/src/main/java/com/de_alone/dokkang/repository/UserLectureRepository.java b/src/main/java/com/de_alone/dokkang/repository/UserLectureRepository.java new file mode 100644 index 0000000..86b6085 --- /dev/null +++ b/src/main/java/com/de_alone/dokkang/repository/UserLectureRepository.java @@ -0,0 +1,27 @@ +package com.de_alone.dokkang.repository; + +import com.de_alone.dokkang.models.User; +import com.de_alone.dokkang.models.UserLecture; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; +import java.util.Optional; + +public interface UserLectureRepository extends JpaRepository { + List findByUserId(Optional userId); + + @Query(value = "select ul.lectureId.id " + + "from UserLecture ul " + + "where ul.userId.id = :user_id") + List findLectureById(@Param("user_id") Long user_id); + @Modifying + @Transactional + @Query(value = "delete " + + "from UserLecture ul " + + "where ul.userId.id = :user_id") + Integer deleteLectureById(@Param("user_id") Long user_id); +}