Skip to content
Closed
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
Binary file modified .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ services:
- web-net
ports:
- "6379:6379"

networks:
web-net:
external: true
Binary file added postgres-data/base/1/1259
Binary file not shown.
Binary file added postgres-data/base/1/2619
Binary file not shown.
Binary file added postgres-data/base/1/2619_fsm
Binary file not shown.
Binary file added postgres-data/base/1/2619_vm
Binary file not shown.
Binary file added postgres-data/base/1/2696
Binary file not shown.
Binary file added postgres-data/base/1/2840
Binary file not shown.
Binary file added postgres-data/base/1/2841
Binary file not shown.
Binary file added postgres-data/base/1/pg_internal.init
Binary file not shown.
Binary file added postgres-data/base/16384/1247
Binary file not shown.
Binary file added postgres-data/base/16384/1247_vm
Binary file not shown.
Binary file added postgres-data/base/16384/1249
Binary file not shown.
Binary file added postgres-data/base/16384/1249_fsm
Binary file not shown.
Binary file added postgres-data/base/16384/1249_vm
Binary file not shown.
Binary file added postgres-data/base/16384/1259
Binary file not shown.
Binary file added postgres-data/base/16384/1259_vm
Binary file not shown.
Empty file added postgres-data/base/16384/2224
Empty file.
Binary file added postgres-data/base/16384/2579
Binary file not shown.
Empty file added postgres-data/base/16384/2604
Empty file.
Binary file added postgres-data/base/16384/2606
Binary file not shown.
Binary file added postgres-data/base/16384/2606_vm
Binary file not shown.
Binary file added postgres-data/base/16384/2608
Binary file not shown.
Binary file added postgres-data/base/16384/2608_fsm
Binary file not shown.
Binary file added postgres-data/base/16384/2608_vm
Binary file not shown.
Binary file added postgres-data/base/16384/2610
Binary file not shown.
Binary file added postgres-data/base/16384/2610_vm
Binary file not shown.
Binary file added postgres-data/base/16384/2656
Binary file not shown.
Binary file added postgres-data/base/16384/2657
Binary file not shown.
Binary file added postgres-data/base/16384/2658
Binary file not shown.
Binary file added postgres-data/base/16384/2659
Binary file not shown.
Binary file added postgres-data/base/16384/2662
Binary file not shown.
Binary file added postgres-data/base/16384/2663
Binary file not shown.
Binary file added postgres-data/base/16384/2664
Binary file not shown.
Binary file added postgres-data/base/16384/2665
Binary file not shown.
Binary file added postgres-data/base/16384/2666
Binary file not shown.
Binary file added postgres-data/base/16384/2667
Binary file not shown.
Binary file added postgres-data/base/16384/2673
Binary file not shown.
Binary file added postgres-data/base/16384/2674
Binary file not shown.
Binary file added postgres-data/base/16384/2678
Binary file not shown.
Binary file added postgres-data/base/16384/2679
Binary file not shown.
Binary file added postgres-data/base/16384/2703
Binary file not shown.
Binary file added postgres-data/base/16384/2704
Binary file not shown.
Binary file added postgres-data/base/16384/3455
Binary file not shown.
Binary file added postgres-data/base/16384/5002
Binary file not shown.
Binary file added postgres-data/base/5/pg_internal.init
Binary file not shown.
Binary file added postgres-data/global/1260
Binary file not shown.
Binary file added postgres-data/global/pg_control
Binary file not shown.
Binary file added postgres-data/global/pg_internal.init
Binary file not shown.
Binary file added postgres-data/pg_stat/pgstat.stat
Binary file not shown.
Binary file added postgres-data/pg_wal/000000010000000000000001
Binary file not shown.
Binary file added postgres-data/pg_xact/0000
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package hello.cluebackend.domain.classroom.domain;

import hello.cluebackend.domain.classroom.presentation.dto.ClassRoomDTO;
import hello.cluebackend.domain.classroomuser.domain.ClassRoomUser;
import jakarta.persistence.*;
import lombok.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name="class_room")
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ClassRoom {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false)
private Long classRoomId;

@Column(nullable = false)
private String name;

@Column(nullable = false)
private String description;

@Column(nullable = false)
private String code;

@Column(nullable = false)
private LocalDateTime createdAt;

@PrePersist
protected void onCreate() {
createdAt = LocalDateTime.now();
}

@OneToMany(mappedBy = "classRoom", cascade = CascadeType.ALL)
private List<ClassRoomUser> classRoomUserList = new ArrayList<>();

public ClassRoomDTO toDTO() {
return ClassRoomDTO.builder()
.classRoomId(classRoomId)
.name(name)
.description(description)
.code(code)
.createdAt(createdAt)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package hello.cluebackend.domain.classroom.domain.repository;

import hello.cluebackend.domain.classroom.domain.ClassRoom;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ClassRoomRepository extends JpaRepository<ClassRoom, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package hello.cluebackend.domain.classroom.presentation;

import hello.cluebackend.domain.classroom.presentation.dto.ClassRoomDTO;
import hello.cluebackend.domain.classroom.service.ClassRoomService;
import hello.cluebackend.domain.user.domain.Role;
import hello.cluebackend.global.config.JWTUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/api/class")
public class ClassRoomController {

private final JWTUtil jwtUtil;
private final ClassRoomService classRoomService;

public ClassRoomController(JWTUtil jwtUtil, ClassRoomService classRoomService) {
this.jwtUtil = jwtUtil;
this.classRoomService = classRoomService;
}

@GetMapping("/request")
public List<ClassRoomDTO> getClassRoom(HttpServletRequest request) {
String authHeader = request.getHeader("Authorization");
String token = authHeader.substring(7);
Long userId = jwtUtil.getUserId(token);
return classRoomService.findMyClassRoomById(userId);
}

@PostMapping("/create-room")
public ResponseEntity<HashMap<?,?>> createClassRoom(@RequestBody ClassRoomDTO classRoomDTO, HttpServletRequest request) {
String authHeader = request.getHeader("Authorization");
String token = authHeader.substring(7);
Role role = jwtUtil.getRole(token);
Long userId = jwtUtil.getUserId(token);
if(!role.name().equals("teacher")) {
return new ResponseEntity<>(HttpStatus.FORBIDDEN);
}
try {
classRoomService.createClassRoom(classRoomDTO, userId);
return new ResponseEntity<>(HttpStatus.CREATED);
} catch (RuntimeException e){
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}

@GetMapping("/{classid}")
public ClassRoomDTO getClassRoom(@PathVariable Long classid, HttpServletRequest request) {
return classRoomService.getClassRoomByClassId(classid);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package hello.cluebackend.domain.classroom.presentation.dto;

import hello.cluebackend.domain.classroom.domain.ClassRoom;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;
import java.util.Random;

@Getter
@Setter
@Builder
public class ClassRoomDTO {

private Long classRoomId;
private String name;
private String description;
private String code;
private LocalDateTime createdAt;

public void generateCode() {
int length = 6;
StringBuilder randomStringBuilder = new StringBuilder();
Random random = new Random();
String characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

for (int i = 0; i < length; i++) {
int randomIndex = random.nextInt(characters.length());
randomStringBuilder.append(characters.charAt(randomIndex));
}
this.code = randomStringBuilder.toString();
}

public ClassRoom toEntity() {
return ClassRoom.builder()
.name(name)
.description(description)
.code(code).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package hello.cluebackend.domain.classroom.service;

import hello.cluebackend.domain.classroom.domain.ClassRoom;
import hello.cluebackend.domain.classroom.domain.repository.ClassRoomRepository;
import hello.cluebackend.domain.classroom.presentation.dto.ClassRoomDTO;
import hello.cluebackend.domain.classroomuser.domain.ClassRoomUser;
import hello.cluebackend.domain.classroomuser.domain.repository.ClassRoomUserRepository;
import hello.cluebackend.domain.user.domain.UserEntity;
import hello.cluebackend.domain.user.domain.repository.UserRepository;
import jakarta.transaction.Transactional;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ClassRoomService {

private final ClassRoomUserRepository classRoomUserRepository;
private final ClassRoomRepository classRoomRepository;
private final UserRepository userRepository;
public ClassRoomService(ClassRoomUserRepository classRoomUserRepository, ClassRoomRepository classRoomRepository, UserRepository userRepository) {
this.classRoomUserRepository = classRoomUserRepository;
this.classRoomRepository = classRoomRepository;
this.userRepository = userRepository;
}

public List<ClassRoomDTO> findMyClassRoomById(Long id) {
List<ClassRoomUser> classRoomUsers = classRoomUserRepository.findByUser_UserId(id);
return classRoomUsers.stream()
.map(ClassRoomUser::getClassRoom)
.map(ClassRoom::toDTO)
.toList();
}

@Transactional
public void createClassRoom(ClassRoomDTO classRoomDTO, Long userId) {
classRoomDTO.generateCode();
ClassRoom classRoom = classRoomDTO.toEntity();
classRoomRepository.save(classRoom);

UserEntity user = userRepository.findById(userId).orElseThrow(() -> new RuntimeException("user not found"));

ClassRoomUser classRoomUser = ClassRoomUser.builder()
.classRoom(classRoom)
.user(user)
.build();
classRoomUserRepository.save(classRoomUser);
}

public ClassRoomDTO getClassRoomByClassId(Long classid) {
ClassRoom classRoom = classRoomRepository.findById(classid).orElseThrow(() -> new RuntimeException("classroom not found"));
return classRoom.toDTO();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package hello.cluebackend.domain.classroomuser.domain;

import hello.cluebackend.domain.classroom.domain.ClassRoom;
import hello.cluebackend.domain.user.domain.UserEntity;
import jakarta.persistence.*;
import lombok.*;

@Entity
@Table(name="class_room_user")
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ClassRoomUser {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false)
private Long classRoomUserId;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
private UserEntity user;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "class_room_id", nullable = false)
private ClassRoom classRoom;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package hello.cluebackend.domain.classroomuser.domain.repository;

import hello.cluebackend.domain.classroomuser.domain.ClassRoomUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ClassRoomUserRepository extends JpaRepository<ClassRoomUser, Long> {

List<ClassRoomUser> findByUser_UserId(Long userId);

}
16 changes: 14 additions & 2 deletions src/main/java/hello/cluebackend/domain/user/domain/UserEntity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hello.cluebackend.domain.user.domain;

import hello.cluebackend.domain.classroomuser.domain.ClassRoomUser;
import hello.cluebackend.domain.user.presentation.dto.UserDTO;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
Expand All @@ -10,13 +11,15 @@
import org.springframework.data.annotation.CreatedDate;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "user")
@Table(name = "user_entity")
public class UserEntity {

@Id
Expand Down Expand Up @@ -44,6 +47,15 @@ public class UserEntity {
@Column(nullable = false)
private LocalDateTime createdAt;

@PrePersist
protected void onCreate() {
createdAt = LocalDateTime.now();
}

@OneToMany(mappedBy = "userEntity", cascade = CascadeType.ALL)
private List<ClassRoomUser> classRoomUserList = new ArrayList<>();


public UserEntity(int studentId, String username, String addition, String email, Role role) {
this.studentId = studentId;
this.username = username;
Expand All @@ -53,6 +65,6 @@ public UserEntity(int studentId, String username, String addition, String email,
}

public UserDTO toUserDTO() {
return new UserDTO(email, role, username, studentId, addition);
return new UserDTO(userId, email, role, username, studentId, addition);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,12 @@ public String getName() {
public String getUsername() {
return userDTO.getUsername();
}

public Long getUserId() {
return userDTO.getUserId();
}

public int getStudentId() {
return userDTO.getStudentId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
@Getter
@Setter
public class UserDTO {
private Long userId;
private String email;
private Role role;
private String username;
private int studentId;
private String addition;

public UserDTO(String email, Role role, String username, int studentId, String addition) {
public UserDTO(Long userId, String email, Role role, String username, int studentId, String addition) {
this.userId = userId;
this.email = email;
this.role = role;
this.username = username;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,34 +37,31 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
int studentId = userDTO.getStudentId();
if (studentId == -1) {
request.getSession().setAttribute("firstUser", userDTO);
System.out.println(userDTO.getUsername() + "λ‹˜ νšŒμ›κ°€μž… 성곡");
getRedirectStrategy().sendRedirect(
request,
response,
"http://localhost:3000/register"
);
} else {
String username = customUserDetails.getUsername();
Long userId = customUserDetails.getUserId();

Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
Iterator<? extends GrantedAuthority> iterator = authorities.iterator();
GrantedAuthority auth = iterator.next();
String role = auth.getAuthority();

String access = jwtUtil.createJwt("access", username, role, 60*60*1000L);
String refresh = jwtUtil.createJwt("refresh", username, role,7 * 24 * 60 * 60 * 1000L);
String access = jwtUtil.createJwt("access", userId, username, role, 60*60*1000L);
String refresh = jwtUtil.createJwt("refresh", userId, username, role,7 * 24 * 60 * 60 * 1000L);

refreshTokenService.saveRefreshToken(refresh, username);
response.setHeader("Authorization", "Bearer " + access);
response.addCookie(createCookie("refresh_token", refresh));
System.out.println(username + "λ‹˜ 둜그인 성곡");
response.setStatus(HttpStatus.OK.value());
response.sendRedirect("http://localhost:3000/");
}
}



private Cookie createCookie(String key, String value) {
Cookie cookie = new Cookie(key, value);
cookie.setMaxAge(7 * 24 * 60 * 60);
Expand Down
Loading