Skip to content

Commit

Permalink
feat: Add User's lecture API (#14)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
namgus and cosmoquester authored Oct 29, 2022
1 parent eba09db commit 6fe65c3
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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<Lecture> lectures = lectureRepository.findAll();

return ResponseEntity.status(HttpStatus.CREATED).body(new LectureResponse("ok", lectures));
}
}
43 changes: 40 additions & 3 deletions src/main/java/com/de_alone/dokkang/controllers/UserController.java
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -23,6 +28,12 @@ public class UserController {
@Autowired
UserRepository userRepository;

@Autowired
UserLectureRepository userlectureRepository;

@Autowired
LectureRepository lectureRepository;

@Autowired
PasswordEncoder encoder;

Expand All @@ -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<Lecture> 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<Long> 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"));
}
}
29 changes: 29 additions & 0 deletions src/main/java/com/de_alone/dokkang/models/Lecture.java
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 1 addition & 1 deletion src/main/java/com/de_alone/dokkang/models/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import javax.validation.constraints.Size;

@Entity
@Table(name = "users",
@Table(name = "user",
uniqueConstraints = {
@UniqueConstraint(columnNames = "username"),
@UniqueConstraint(columnNames = "email")
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/com/de_alone/dokkang/models/UserLecture.java
Original file line number Diff line number Diff line change
@@ -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;
}

}

Original file line number Diff line number Diff line change
@@ -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<Long> lecture_ids;
}
Original file line number Diff line number Diff line change
@@ -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<Lecture> lectures;

}
Original file line number Diff line number Diff line change
@@ -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<Lecture, Long> {
}
Original file line number Diff line number Diff line change
@@ -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<UserLecture, Long> {
List<UserLecture> findByUserId(Optional<User> userId);

@Query(value = "select ul.lectureId.id " +
"from UserLecture ul " +
"where ul.userId.id = :user_id")
List<Long> 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);
}

0 comments on commit 6fe65c3

Please sign in to comment.