Skip to content

Commit

Permalink
refactor: project, admin 검색 기능 추가, participant - student 연결
Browse files Browse the repository at this point in the history
  • Loading branch information
singsangssong committed Nov 1, 2024
1 parent 3ad7708 commit aacc960
Show file tree
Hide file tree
Showing 20 changed files with 256 additions and 102 deletions.
30 changes: 17 additions & 13 deletions src/main/java/kr/tgwing/tech/admin/controller/AdminController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import kr.tgwing.tech.admin.dto.AdminCheckUserDto;
import kr.tgwing.tech.admin.dto.AllStudentsDto;
import kr.tgwing.tech.admin.dto.StudentQuery;
import kr.tgwing.tech.admin.service.AdminService;
import kr.tgwing.tech.common.ApiResponse;
import lombok.RequiredArgsConstructor;
Expand All @@ -14,8 +15,6 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin")
@RequiredArgsConstructor
Expand All @@ -26,35 +25,40 @@ public class AdminController {
private final AdminService adminService;

@Operation(summary = "동아리 가입요청 목록 조회")
@GetMapping("")
@GetMapping
public ResponseEntity<ApiResponse<Page<AdminCheckUserDto>>> checkAllAssignments(
@PageableDefault Pageable pageable) {
Page<AdminCheckUserDto> allAssignments = adminService.checkAssingments(pageable);
@PageableDefault Pageable pageable,
@ModelAttribute StudentQuery studentQuery
) {
Page<AdminCheckUserDto> allAssignments = adminService.checkAssingments(studentQuery, pageable);
return ResponseEntity.ok(ApiResponse.ok(allAssignments));
}

@Operation(summary = "동아리원 목록 조회")
@GetMapping("/student")
public ResponseEntity<ApiResponse<?>> checkAllStudents(
@PageableDefault Pageable pageable,
@ModelAttribute StudentQuery studentQuery
) {
Page<AllStudentsDto> allStudents = adminService.checkAllStudents(studentQuery, pageable);
return ResponseEntity.ok(ApiResponse.ok(allStudents));
}

@Operation(summary = "가입요청 수락")
@PostMapping("/{id}")
public ResponseEntity<ApiResponse<Long>> registerAssignment(@PathVariable("id") Long id) {
Long registerId = adminService.registerUsers(id);

return ResponseEntity.ok(ApiResponse.updated(registerId));
}

@Operation(summary = "가입요청 거부")
@DeleteMapping("/{id}")
public ResponseEntity<ApiResponse<Long>> refuseAssignment(@PathVariable("id") Long id) {
Long refusedId = adminService.refuseUsers(id);

return ResponseEntity.ok(ApiResponse.delete(refusedId));
}

@Operation(summary = "동아리원 목록 조회")
@GetMapping("/student")
public ResponseEntity<ApiResponse<?>> checkAllStudents(@PageableDefault Pageable pageable) {
Page<AllStudentsDto> allStudents = adminService.checkAllStudents(pageable);
return ResponseEntity.ok(ApiResponse.ok(allStudents));
}


@Operation(summary = "동아리원 강제 삭제")
@DeleteMapping("/stdent/{id}")
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/kr/tgwing/tech/admin/dto/StudentQuery.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package kr.tgwing.tech.admin.dto;

import lombok.Data;

@Data
public class StudentQuery {
private String keyword = "";
}
23 changes: 14 additions & 9 deletions src/main/java/kr/tgwing/tech/admin/service/AdminService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@

import kr.tgwing.tech.admin.dto.AdminCheckUserDto;
import kr.tgwing.tech.admin.dto.AllStudentsDto;
import kr.tgwing.tech.admin.dto.StudentQuery;
import kr.tgwing.tech.blog.entity.Comment;
import kr.tgwing.tech.blog.entity.Post;
import kr.tgwing.tech.blog.repository.PostRepository;
import kr.tgwing.tech.blog.repository.ReplyRepository;
import kr.tgwing.tech.user.entity.TempUser;
import kr.tgwing.tech.user.entity.User;
import kr.tgwing.tech.user.entity.UserSpecification;
import kr.tgwing.tech.user.exception.UserNotFoundException;
import kr.tgwing.tech.user.repository.TempUserRepository;
import kr.tgwing.tech.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -27,10 +30,10 @@ public class AdminService {
private final UserRepository userRepository;
private final TempUserRepository tempUserRepository;
private final PostRepository postRepository;
private final ReplyRepository replyRepository;

public Page<AdminCheckUserDto> checkAssingments(Pageable pageable) {
Page<TempUser> allAssignments = tempUserRepository.findAll(pageable);
public Page<AdminCheckUserDto> checkAssingments(StudentQuery studentQuery, Pageable pageable) {
Specification<TempUser> spec = UserSpecification.hasKeywordInTempUser(studentQuery.getKeyword());
Page<TempUser> allAssignments = tempUserRepository.findAll(spec, pageable);
if(allAssignments.isEmpty()) return null; // 여기 값은 예외로 보내야하느건지, 어떻게 보내야하는거지?
return allAssignments.map(assignment -> AdminCheckUserDto.of(assignment));

Expand All @@ -43,6 +46,13 @@ public Page<AdminCheckUserDto> checkAssingments(Pageable pageable) {
// return dtoList;
}

public Page<AllStudentsDto> checkAllStudents(StudentQuery studentQuery, Pageable pageable) {
Specification<User> spec = UserSpecification.hasKeywordInUser(studentQuery.getKeyword());
Page<User> allStudents = userRepository.findAll(spec, pageable);
if(allStudents.isEmpty()) return null;
return allStudents.map( student -> AllStudentsDto.of(student));
}

@Transactional
public Long registerUsers(Long studentId) {
TempUser notUser = tempUserRepository.findById(studentId).orElseThrow(UserNotFoundException::new);
Expand All @@ -61,12 +71,7 @@ public Long refuseUsers(Long studentId) {
return user.getStudentId();
}

public Page<AllStudentsDto> checkAllStudents(Pageable pageable) {
Page<User> allStudents = userRepository.findAll(pageable);
if(allStudents.isEmpty()) return null;
return allStudents.map( student -> AllStudentsDto.of(student));
}


@Transactional
public void deleteStudent(Long studentId) {
User user = userRepository.findById(studentId).orElseThrow(UserNotFoundException::new);
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/kr/tgwing/tech/config/TestDataLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ public CommandLineRunner loadTestData(PostRepository postRepository,
.participants(new ArrayList<>())
.build();


Image image1 = Image.builder()
.imageUrl("img1")
.project(project)
Expand All @@ -184,21 +185,26 @@ public CommandLineRunner loadTestData(PostRepository postRepository,

Participant participant1 = Participant.builder()
.part(Part.DESIGNER)
.studentNumber("2020211121")
.name("design")
.major("디자인과")
.project(project)
.build();
Participant participant2 = Participant.builder()
.part(Part.BACK)
.name("song")
.studentNumber("2018000000")
.name("늙은이")
.major("컴공")
.project(project)
.user(writer1)
.build();
Participant participant3 = Participant.builder()
.part(Part.FRONT)
.name("jin")
.studentNumber("2022000000")
.name("젊은이")
.project(project)
.major("컴공")
.user(writer2)
.build();

project.getParticipants().add(participant1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import kr.tgwing.tech.common.ApiResponse;
import kr.tgwing.tech.project.dto.ProjectBriefDTO;
import kr.tgwing.tech.project.dto.ProjectCreateDTO;
import kr.tgwing.tech.project.dto.ProjectDetailDTO;
import kr.tgwing.tech.project.dto.ProjectUpdateDTO;
import kr.tgwing.tech.project.dto.*;
import kr.tgwing.tech.project.service.ProjectServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
Expand All @@ -26,9 +25,12 @@ public class ProjectController {
private final ProjectServiceImpl projectServiceImpl;

@Operation(summary = "프로젝트 전체 조회")
@GetMapping("")
public ResponseEntity<?> getProjects(){
List<ProjectBriefDTO> projects = projectServiceImpl.getProjects();
@GetMapping
public ResponseEntity<?> getProjects(
Pageable pageable,
@ModelAttribute ProjectQuery query
){
Page<ProjectBriefDTO> projects = projectServiceImpl.getProjects(pageable, query);
return ResponseEntity.ok(ApiResponse.ok(projects));
}

Expand All @@ -40,7 +42,7 @@ public ResponseEntity<?> getOneProject(@PathVariable("project_id") Long project_
}

@Operation(summary = "프로젝트 생성")
@PostMapping("")
@PostMapping
public ResponseEntity<?> postProject(
@Valid @RequestBody ProjectCreateDTO projectCreateDTO) {
Long projectId = projectServiceImpl.createProjects(projectCreateDTO);
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/kr/tgwing/tech/project/domain/Participant.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import kr.tgwing.tech.common.BaseEntity;
import kr.tgwing.tech.project.domain.Enum.Part;
import kr.tgwing.tech.project.dto.ParticipantDTO;
import kr.tgwing.tech.user.entity.User;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -26,6 +27,12 @@ public class Participant extends BaseEntity {
@JoinColumn(name="project_id")
private Project project;

@ManyToOne
@JoinColumn(name="student_id")
private User user;

private String studentNumber;

@Enumerated(EnumType.STRING)
private Part part;

Expand All @@ -36,19 +43,26 @@ public class Participant extends BaseEntity {
public void setProject(Project project) {
this.project = project;
}
public void setUser(User user) {
this.user = user;
}

public static ParticipantDTO toDTO(Participant participant) {
return ParticipantDTO.builder()
.username(participant.getName())
.studentNumber(participant.getStudentNumber())
.major(participant.getMajor())
.part(participant.getPart())
.user(participant.getUser())
.build();
}

@Builder
public Participant(Long id, Project project, Part part, String name, String major) {
public Participant(Long id, Project project, Part part, String name, String major, User user, String studentNumber) {
this.id = id;
this.studentNumber = studentNumber;
this.project = project;
this.user = user;
this.part = part;
this.name = name;
this.major = major;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/kr/tgwing/tech/project/domain/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class Project extends BaseEntity {
@Lob
@Column(columnDefinition = "TEXT")
private String description;

@DateTimeFormat(pattern = "yyyy-MM-dd")
@Column(name = "start_date")
private LocalDate startDate;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package kr.tgwing.tech.project.domain;

import jakarta.persistence.criteria.Join;
import jakarta.persistence.criteria.Predicate;
import kr.tgwing.tech.user.entity.User;
import org.springframework.data.jpa.domain.Specification;

public class ProjectSpecification {
public static Specification<Project> hasKeywordInProject(String keyword) {
return (root, query, cb) -> {
String pattern = "%" + keyword + "%";

Predicate title = cb.like(root.get("title"), pattern);
Predicate description = cb.like(root.get("description"), pattern);

Join<Project, Participant> participantJoin = root.join("participant");
Predicate name = cb.like(participantJoin.get("name"), pattern);
Predicate studentNumber = cb.like(participantJoin.get("studentNumber"), pattern);

return cb.or(title, description, name, studentNumber);
};
}

public static Specification<Project> hasKeywordInMyProject(String keyword) {
return (root, query, cb) -> {
String pattern = "%" + keyword + "%";

Predicate title = cb.like(root.get("title"), pattern);
Predicate description = cb.like(root.get("description"), pattern);

Join<Project, Participant> participantJoin = root.join("participant");
Predicate studentNumber = cb.equal(participantJoin.get("studentNumber"), pattern);

return cb.or(title, description, studentNumber);
};
}

}
10 changes: 9 additions & 1 deletion src/main/java/kr/tgwing/tech/project/dto/ParticipantDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import kr.tgwing.tech.project.domain.Enum.Part;
import kr.tgwing.tech.project.domain.Participant;
import kr.tgwing.tech.user.entity.User;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Data;
Expand All @@ -11,19 +12,26 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ParticipantDTO {
private Part part;
private String studentNumber;
private String username;
private String major;
private User user;

@Builder
public ParticipantDTO(Part part, String username, String major) {
public ParticipantDTO(Part part, String username, String major, String studentNumber, User user) {
this.part = part;
this.studentNumber = studentNumber;
this.user = user;
this.username = username;
this.major = major;
}


public static Participant toParticipantEntity(ParticipantDTO participantDTO){
return Participant.builder()
.part(participantDTO.getPart())
.studentNumber(participantDTO.getStudentNumber())
.user(participantDTO.getUser())
.name(participantDTO.getUsername())
.major(participantDTO.getMajor())
.build();
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/kr/tgwing/tech/project/dto/ProjectBriefDTO.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package kr.tgwing.tech.project.dto;

import kr.tgwing.tech.project.domain.Project;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Data
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand All @@ -31,4 +31,17 @@ public ProjectBriefDTO(Long id, String title, LocalDate start, LocalDate end, St
this.devStatus = devStatus;
this.devType = devType;
}

public static ProjectBriefDTO of(Project project) {
return ProjectBriefDTO.builder()
.id(project.getId())
.title(project.getTitle())
.start(project.getStartDate())
.end(project.getEndDate())
.description(project.getDescription())
.thumbnail(project.getImageUrls().stream().findFirst().get().getImageUrl())
.devStatus(project.getDevStatus())
.devType(project.getDevType())
.build();
}
}
8 changes: 8 additions & 0 deletions src/main/java/kr/tgwing/tech/project/dto/ProjectQuery.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package kr.tgwing.tech.project.dto;

import lombok.Data;

@Data
public class ProjectQuery {
private String keyword = "";
}
Loading

0 comments on commit aacc960

Please sign in to comment.