Skip to content

Commit

Permalink
add admin api(force delete student, student select paging)
Browse files Browse the repository at this point in the history
  • Loading branch information
singsangssong committed Oct 29, 2024
1 parent 18dd7b3 commit 7d63414
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 110 deletions.
40 changes: 29 additions & 11 deletions src/main/java/kr/tgwing/tech/admin/controller/AdminController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import kr.tgwing.tech.admin.dto.AdminCheckUserDto;
import kr.tgwing.tech.admin.service.AdminServiceImpl;
import kr.tgwing.tech.admin.dto.AllStudentsDto;
import kr.tgwing.tech.admin.service.AdminService;
import kr.tgwing.tech.common.ApiResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand All @@ -19,29 +23,43 @@
@Tag(name = "관리자")
public class AdminController {

private final AdminServiceImpl adminService;
private final AdminService adminService;

@Operation(summary = "요청 목록 확인")
@Operation(summary = "동아리 가입요청 목록 조회")
@GetMapping("")
public ResponseEntity<ApiResponse<List<AdminCheckUserDto>>> checkUsers() {
List<AdminCheckUserDto> dtoList = adminService.checkUser();

return ResponseEntity.ok(ApiResponse.ok(dtoList));
public ResponseEntity<ApiResponse<Page<AdminCheckUserDto>>> checkAllAssignments(
@PageableDefault Pageable pageable) {
Page<AdminCheckUserDto> allAssignments = adminService.checkAssingments(pageable);
return ResponseEntity.ok(ApiResponse.ok(allAssignments));
}

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

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

@Operation(summary = "회원요청 거부")
@Operation(summary = "가입요청 거부")
@DeleteMapping("/{id}")
public ResponseEntity<ApiResponse<Long>> refuseUsers(@PathVariable("id") Long 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}")
public ResponseEntity<ApiResponse<?>> deleteStudent(@PathVariable Long id) {
adminService.deleteStudent(id);
return ResponseEntity.ok(ApiResponse.ok(id));
}
}
11 changes: 11 additions & 0 deletions src/main/java/kr/tgwing/tech/admin/dto/AdminCheckUserDto.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kr.tgwing.tech.admin.dto;

import kr.tgwing.tech.user.entity.TempUser;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -15,4 +16,14 @@ public class AdminCheckUserDto {
private String email;
private String name;
private String phoneNumber;

public static AdminCheckUserDto of(TempUser tempUser) {
return AdminCheckUserDto.builder()
.studentId(tempUser.getStudentId())
.studentNumber(tempUser.getStudentNumber())
.email(tempUser.getEmail())
.name(tempUser.getName())
.phoneNumber(tempUser.getPhoneNumber())
.build();
}
}
33 changes: 33 additions & 0 deletions src/main/java/kr/tgwing/tech/admin/dto/AllStudentsDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package kr.tgwing.tech.admin.dto;

import kr.tgwing.tech.user.entity.User;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDate;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AllStudentsDto {
private Long studentId;
private String studentNumber;
private String email;
private String name;
private String phoneNumber;
private LocalDate birth;

public static AllStudentsDto of(User user) {
return AllStudentsDto.builder()
.studentId(user.getStudentId())
.studentNumber(user.getStudentNumber())
.email(user.getEmail())
.name(user.getName())
.phoneNumber(user.getPhoneNumber())
.birth(user.getBirth())
.build();
}
}
66 changes: 44 additions & 22 deletions src/main/java/kr/tgwing/tech/admin/service/AdminService.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package kr.tgwing.tech.admin.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import kr.tgwing.tech.admin.dto.AdminCheckUserDto;
import kr.tgwing.tech.admin.dto.AllStudentsDto;
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.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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
Expand All @@ -21,36 +26,53 @@ public class AdminService {

private final UserRepository userRepository;
private final TempUserRepository tempUserRepository;
private final PostRepository postRepository;
private final ReplyRepository replyRepository;

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

List<AdminCheckUserDto> dtoList = new ArrayList<>();
for(TempUser user : allUser) {
AdminCheckUserDto dto = user.toAdminCheckUserDto(user);
dtoList.add(dto);
}
// List<AdminCheckUserDto> dtoList = new ArrayList<>();
// for(TempUser user : allUser) {
// AdminCheckUserDto dto = user.toAdminCheckUserDto(user);
// dtoList.add(dto);
// }

return dtoList;
// return dtoList;
}

@Transactional
public Long registerUsers(Long id) {
TempUser notUser = tempUserRepository.findById(id).orElseThrow(UserNotFoundException::new);
public Long registerUsers(Long studentId) {
TempUser notUser = tempUserRepository.findById(studentId).orElseThrow(UserNotFoundException::new);
User user = notUser.toUser(notUser);
tempUserRepository.deleteById(id);
tempUserRepository.deleteById(studentId);

user.setRole("ROLE_USER");
userRepository.save(user);

return user.getStudentId();
}

public Long refuseUsers(Long id) {
TempUser user = tempUserRepository.findById(id).orElseThrow(UserNotFoundException::new);
userRepository.deleteById(user.getStudentId());

public Long refuseUsers(Long studentId) {
TempUser user = tempUserRepository.findById(studentId).orElseThrow(UserNotFoundException::new);
userRepository.deleteById(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);
List<Post> postByUser = postRepository.findByWriter(user);

userRepository.deleteById(studentId);
if(!postByUser.isEmpty()) postRepository.deleteAllByWriter(user);
}
}
55 changes: 0 additions & 55 deletions src/main/java/kr/tgwing/tech/admin/service/AdminServiceImpl.java

This file was deleted.

12 changes: 2 additions & 10 deletions src/main/java/kr/tgwing/tech/blog/entity/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,7 @@
import java.util.List;
import java.util.Set;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import jakarta.persistence.*;

import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down Expand Up @@ -42,6 +33,7 @@ public class Post extends BaseEntity {
@Column(nullable = false)
private String title;

@Lob
@Column(nullable = false)
private String content;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public interface PostRepository extends JpaRepository<Post, Long>, JpaSpecificat
long countByTitleContains(String search);

Page<Post> findByWriter(User writer, Pageable pageable);

void deleteAllByWriter(User writer);
@Override
long count();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import kr.tgwing.tech.user.entity.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -17,4 +18,6 @@ public interface ReplyRepository extends JpaRepository<Reply, Long> {
List<Comment> findAllByComment(Comment post);
Page<Reply> findAllByComment(Comment comment, Pageable pageable);

List<Comment> findAllByWriter(User user);

}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ public ResponseEntity<ApiResponse<Long>> setNewPassword(

return ResponseEntity.ok(ApiResponse.updated(userId));
}
//ㅎ
}


10 changes: 0 additions & 10 deletions src/main/java/kr/tgwing/tech/user/entity/TempUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,6 @@ public TempUser(String studentNumber, String password, String email, String name
super(studentNumber, password, email, name, birth, phoneNumber);
}

public AdminCheckUserDto toAdminCheckUserDto(TempUser user) {
return AdminCheckUserDto.builder()
.studentId(user.getStudentId())
.name(user.getName())
.email(user.getEmail())
.studentNumber(user.getStudentNumber())
.phoneNumber(user.getPhoneNumber())
.build();
}

public User toUser(TempUser user) {
return User.builder()
.studentNumber(user.getStudentNumber())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public interface UserRepository extends JpaRepository<User, Long> {
@Transactional
void deleteByStudentNumber(String studentNumber);


@Transactional
@Modifying
@Query("UPDATE User U SET U.name = :name, U.phoneNumber = :phoneNumber, U.profilePicture = :profilePicture " +
Expand Down

0 comments on commit 7d63414

Please sign in to comment.