Skip to content

Commit

Permalink
Merge pull request #106 from Leets-Official/refactor/#102/기수-테이블-분리
Browse files Browse the repository at this point in the history
Refactor #106 기수 테이블 분리
  • Loading branch information
hyxklee authored Jan 18, 2025
2 parents 70a02e2 + f33d025 commit 8c751ab
Show file tree
Hide file tree
Showing 28 changed files with 527 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public AttendanceDTO.Main find(Long userId) {
return mapper.toMainDto(user, todayMeeting);
}

/*
todo 출석 끝난 후 다음 기수 진행 시 다음 기수의 출석 정보만 나오는지 확인 필요
*/
@Override
public AttendanceDTO.Detail findAll(Long userId) {
User user = userGetService.find(userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public void save(PenaltyDTO.Save dto) {

penaltySaveService.save(penalty);

user.addPenalty(penalty);
user.incrementPenaltyCount();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
import leets.weeth.domain.schedule.domain.service.MeetingGetService;
import leets.weeth.domain.schedule.domain.service.MeetingSaveService;
import leets.weeth.domain.schedule.domain.service.MeetingUpdateService;
import leets.weeth.domain.user.domain.entity.Cardinal;
import leets.weeth.domain.user.domain.entity.User;
import leets.weeth.domain.user.domain.service.CardinalGetService;
import leets.weeth.domain.user.domain.service.UserCardinalGetService;
import leets.weeth.domain.user.domain.service.UserGetService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -37,6 +40,7 @@ public class MeetingUseCaseImpl implements MeetingUseCase {
private final AttendanceSaveService attendanceSaveService;
private final AttendanceDeleteService attendanceDeleteService;
private final AttendanceUpdateService attendanceUpdateService;
private final CardinalGetService cardinalGetService;

@Override
public Response find(Long meetingId) {
Expand All @@ -47,7 +51,9 @@ public Response find(Long meetingId) {
@Transactional
public void save(Save dto, Long userId) {
User user = userGetService.find(userId);
List<User> userList = userGetService.findAllByCardinal(dto.cardinal());
Cardinal cardinal = cardinalGetService.find(dto.cardinal());

List<User> userList = userGetService.findAllByCardinal(cardinal);

Meeting meeting = mapper.from(dto, user);
meetingSaveService.save(meeting);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package leets.weeth.domain.user.application.dto.request;

import jakarta.validation.constraints.NotNull;

public record CardinalSaveRequest (
@NotNull Integer cardinalNumber,
@NotNull Integer year,
@NotNull Integer semester
){
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package leets.weeth.domain.user.application.dto.response;

import java.time.LocalDateTime;

public record CardinalResponse(
Long id,
Integer cardinalNumber,
Integer year,
Integer semester,
LocalDateTime createdAt,
LocalDateTime modifiedAt
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package leets.weeth.domain.user.application.dto.response;

import leets.weeth.domain.user.domain.entity.User;
import leets.weeth.domain.user.domain.entity.UserCardinal;

import java.util.List;

public record UserCardinalDto(
User user,
List<UserCardinal> cardinals
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package leets.weeth.domain.user.application.exception;

import leets.weeth.global.common.exception.BusinessLogicException;

public class DuplicateCardinalException extends BusinessLogicException {
public DuplicateCardinalException() {
super(400, "이미 존재하는 기수 입니다.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package leets.weeth.domain.user.application.mapper;

import leets.weeth.domain.user.application.dto.request.CardinalSaveRequest;
import leets.weeth.domain.user.application.dto.response.CardinalResponse;
import leets.weeth.domain.user.application.dto.response.UserCardinalDto;
import leets.weeth.domain.user.domain.entity.Cardinal;
import leets.weeth.domain.user.domain.entity.User;
import leets.weeth.domain.user.domain.entity.UserCardinal;
import org.mapstruct.Mapper;
import org.mapstruct.MappingConstants;
import org.mapstruct.ReportingPolicy;

import java.util.List;

@Mapper(componentModel = MappingConstants.ComponentModel.SPRING, unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface CardinalMapper {

Cardinal from(CardinalSaveRequest dto);

CardinalResponse to(Cardinal cardinal);

UserCardinalDto toUserCardinalDto(User user, List<UserCardinal> userCardinals);
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package leets.weeth.domain.user.application.mapper;

import leets.weeth.domain.user.application.dto.response.UserResponseDto.*;
import leets.weeth.domain.user.application.dto.response.UserResponseDto;
import leets.weeth.domain.user.application.dto.response.UserResponseDto.SummaryResponse;
import leets.weeth.domain.user.application.dto.response.UserResponseDto.UserResponse;
import leets.weeth.domain.user.domain.entity.User;
import leets.weeth.domain.user.domain.entity.UserCardinal;
import leets.weeth.domain.user.domain.entity.enums.Department;
import leets.weeth.global.auth.jwt.application.dto.JwtDto;
import org.mapstruct.*;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static leets.weeth.domain.user.application.dto.request.UserRequestDto.Register;
import static leets.weeth.domain.user.application.dto.request.UserRequestDto.SignUp;
import static leets.weeth.domain.user.application.dto.response.UserResponseDto.*;
Expand All @@ -18,27 +20,28 @@
public interface UserMapper {

@Mappings({
@Mapping(target = "cardinals", expression = "java( java.util.List.of(dto.cardinal()) )"),
@Mapping(target = "password", expression = "java( passwordEncoder.encode(dto.password()) )"),
@Mapping(target = "department", expression = "java( leets.weeth.domain.user.domain.entity.enums.Department.to(dto.department()) )")
})
User from(SignUp dto, @Context PasswordEncoder passwordEncoder);

@Mappings({
@Mapping(target = "cardinals", expression = "java( java.util.List.of(dto.cardinal()) )"),
@Mapping(target = "department", expression = "java( leets.weeth.domain.user.domain.entity.enums.Department.to(dto.department()) )")
})
User from(Register dto);

@Mapping(target = "department", expression = "java( toString(user.getDepartment()) )")
Response to(User user);
@Mapping(target = "cardinals", expression = "java( toCardinalNumbers(userCardinals) )")
Response to(User user, List<UserCardinal> userCardinals);

@Mappings({
// 수정: 출석률, 출석 횟수, 결석 횟수 매핑 추후 추가 예정
@Mapping(target = "cardinals", expression = "java( toCardinalNumbers(userCardinals) )")
})
AdminResponse toAdminResponse(User user);
AdminResponse toAdminResponse(User user, List<UserCardinal> userCardinals);

SummaryResponse toSummaryResponse(User user);
@Mapping(target = "cardinals", expression = "java( toCardinalNumbers(userCardinals) )")
SummaryResponse toSummaryResponse(User user, List<UserCardinal> userCardinals);

SocialAuthResponse toSocialAuthResponse(Long kakaoId);

Expand All @@ -56,13 +59,25 @@ public interface UserMapper {

@Mappings({
// 상세 데이터 매핑
@Mapping(target = "cardinals", expression = "java( toCardinalNumbers(userCardinals) )")
})
UserResponse toUserResponse(User user);
UserResponse toUserResponse(User user, List<UserCardinal> userCardinals);

UserResponseDto.UserInfo toUserInfoDto(User user);
@Mapping(target = "cardinals", expression = "java( toCardinalNumbers(userCardinals) )")
UserResponseDto.UserInfo toUserInfoDto(User user, List<UserCardinal> userCardinals);

default String toString(Department department) {
return department.getValue();
}

default List<Integer> toCardinalNumbers(List<UserCardinal> userCardinals) {
if (userCardinals == null || userCardinals.isEmpty()) {
return Collections.emptyList();
}

return userCardinals.stream()
.map(uc -> uc.getCardinal().getCardinalNumber())
.collect(Collectors.toList());
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package leets.weeth.domain.user.application.usecase;

import io.swagger.v3.oas.annotations.tags.Tag;
import leets.weeth.domain.user.application.dto.request.CardinalSaveRequest;
import leets.weeth.domain.user.application.dto.response.CardinalResponse;
import leets.weeth.domain.user.application.mapper.CardinalMapper;
import leets.weeth.domain.user.domain.entity.Cardinal;
import leets.weeth.domain.user.domain.service.CardinalGetService;
import leets.weeth.domain.user.domain.service.CardinalSaveService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class CardinalUseCase {

private final CardinalGetService cardinalGetService;
private final CardinalSaveService cardinalSaveService;

private final CardinalMapper cardinalMapper;

@Transactional
public void save(CardinalSaveRequest dto) {
cardinalGetService.validateCardinal(dto.cardinalNumber());

cardinalSaveService.save(cardinalMapper.from(dto));
}

public List<CardinalResponse> findAll() {
List<Cardinal> cardinals = cardinalGetService.findAll();
return cardinals.stream()
.map(cardinalMapper::to)
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
import leets.weeth.domain.schedule.domain.service.MeetingGetService;
import leets.weeth.domain.user.application.exception.InvalidUserOrderException;
import leets.weeth.domain.user.application.mapper.UserMapper;
import leets.weeth.domain.user.domain.entity.Cardinal;
import leets.weeth.domain.user.domain.entity.User;
import leets.weeth.domain.user.domain.entity.UserCardinal;
import leets.weeth.domain.user.domain.entity.enums.StatusPriority;
import leets.weeth.domain.user.domain.entity.enums.UsersOrderBy;
import leets.weeth.domain.user.domain.service.UserDeleteService;
import leets.weeth.domain.user.domain.service.UserGetService;
import leets.weeth.domain.user.domain.service.UserUpdateService;
import leets.weeth.domain.user.domain.service.*;
import leets.weeth.global.auth.jwt.service.JwtRedisService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder;
Expand All @@ -20,10 +20,9 @@
import java.util.Comparator;
import java.util.EnumSet;
import java.util.List;
import java.util.stream.Collectors;

import static leets.weeth.domain.user.application.dto.response.UserResponseDto.AdminResponse;
import static leets.weeth.domain.user.domain.entity.enums.UsersOrderBy.*;
import static leets.weeth.domain.user.domain.entity.enums.UsersOrderBy.NAME_ASCENDING;

@Service
@RequiredArgsConstructor
Expand All @@ -36,21 +35,27 @@ public class UserManageUseCaseImpl implements UserManageUseCase {
private final AttendanceSaveService attendanceSaveService;
private final MeetingGetService meetingGetService;
private final JwtRedisService jwtRedisService;
private final CardinalGetService cardinalGetService;
private final UserCardinalSaveService userCardinalSaveService;
private final UserCardinalGetService userCardinalGetService;

private final UserMapper mapper;
private final PasswordEncoder passwordEncoder;

@Override
public List<AdminResponse> findAllByAdmin(UsersOrderBy orderBy) {
if(orderBy == null || !EnumSet.allOf(UsersOrderBy.class).contains(orderBy)){
if (orderBy == null || !EnumSet.allOf(UsersOrderBy.class).contains(orderBy)) {
throw new InvalidUserOrderException();
}

if(orderBy.equals(NAME_ASCENDING)){
if (orderBy.equals(NAME_ASCENDING)) {
return userGetService.findAll().stream()
.sorted(Comparator.comparingInt((user->(StatusPriority.fromStatus(user.getStatus())).getPriority())))
.map(mapper::toAdminResponse)
.toList();
.sorted(Comparator.comparingInt((user -> (StatusPriority.fromStatus(user.getStatus())).getPriority())))
.map(user -> {
List<UserCardinal> userCardinals = userCardinalGetService.getUserCardinals(user);
return mapper.toAdminResponse(user, userCardinals);
})
.toList();
}
// To do : 추후 기수 분리 후 작업 예정

Expand All @@ -62,9 +67,11 @@ public List<AdminResponse> findAllByAdmin(UsersOrderBy orderBy) {
public void accept(Long userId) {
User user = userGetService.find(userId);

Integer cardinal = userCardinalGetService.getCurrentCardinal(user).getCardinalNumber();

if (user.isInactive()) {
userUpdateService.accept(user);
List<Meeting> meetings = meetingGetService.find(user.getCardinals().get(0));
List<Meeting> meetings = meetingGetService.find(cardinal);
attendanceSaveService.init(user, meetings);
}
}
Expand Down Expand Up @@ -95,15 +102,17 @@ public void ban(Long userId) {
@Transactional
public void applyOB(Long userId, Integer cardinal) {
User user = userGetService.find(userId);
Cardinal nextCardinal = cardinalGetService.find(cardinal);

if (user.notContains(cardinal)) {
if (user.isCurrent(cardinal)) {
if (userCardinalGetService.notContains(user, nextCardinal)) {
if (userCardinalGetService.isCurrent(user, nextCardinal)) {
user.initAttendance();
List<Meeting> meetings = meetingGetService.find(cardinal);
attendanceSaveService.init(user, meetings);
}
UserCardinal userCardinal = new UserCardinal(user, nextCardinal);

userUpdateService.applyOB(user, cardinal);
userCardinalSaveService.save(userCardinal);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import leets.weeth.domain.user.application.dto.request.UserRequestDto;
import leets.weeth.domain.user.application.dto.response.UserResponseDto;
import leets.weeth.global.auth.jwt.application.dto.JwtDto;
import org.springframework.data.domain.Slice;

import java.util.List;
import java.util.Map;
Expand All @@ -24,9 +25,7 @@ public interface UserUseCase {

UserResponseDto.Response find(Long userId);

Map<Integer, List<UserResponseDto.Response>> findAll();

Map<Integer, List<UserResponseDto.SummaryResponse>> findAllUser();
Slice<SummaryResponse> findAllUser(int pageNumber, int pageSize);

UserResponseDto.UserResponse findUserDetails(Long userId);

Expand Down
Loading

0 comments on commit 8c751ab

Please sign in to comment.