Skip to content

Commit

Permalink
Merge pull request #59 from WE-ARE-RACCOONS/RAC-155
Browse files Browse the repository at this point in the history
RAC-155 feat : λ©˜ν†  검색 κΈ°λŠ₯ κ΅¬ν˜„
  • Loading branch information
ywj9811 authored Nov 25, 2023
2 parents 536f0f7 + a61a121 commit 9f4aa01
Show file tree
Hide file tree
Showing 15 changed files with 189 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@

import com.postgraduate.domain.account.domain.entity.Account;
import com.postgraduate.domain.senior.application.dto.req.SeniorAccountRequest;
import com.postgraduate.domain.senior.application.dto.req.SeniorMyPageUserAccountRequest;
import com.postgraduate.domain.senior.domain.entity.Senior;

public class AccountMapper {
public static Account mapToAccount(Senior senior, SeniorAccountRequest accountRequest, String accountNumber, String rrn) {
public static Account mapToAccount(Senior senior, SeniorAccountRequest accountRequest, String accountNumber) {
return Account.builder()
.senior(senior)
.accountNumber(accountNumber)
.accountHolder(accountRequest.getAccountHolder())
.bank(accountRequest.getBank())
.name(accountRequest.getName())
.rrn(rrn)
.build();
}

public static Account mapToAccount(Senior senior, SeniorMyPageUserAccountRequest myPageUserAccountRequest, String accountNumber) {
return Account.builder()
.senior(senior)
.accountNumber(accountNumber)
.accountHolder(myPageUserAccountRequest.accountHolder())
.bank(myPageUserAccountRequest.bank())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ public class Account {
@Column(nullable = false)
private String accountHolder;

@Column(nullable = false)
private String name;

@Column(nullable = false)
private String rrn;

@OneToOne
private Senior senior;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public ResponseDto<JwtTokenResponse> singUpSenior(@RequestBody SeniorSignUpReque
}

@PostMapping("/senior/change")
@Operation(summary = "μ„ λ°°λ‘œ λ³€κ²½", description = "λŒ€ν•™μƒ λŒ€ν•™μ›μƒμœΌλ‘œ λ³€κ²½")
@Operation(summary = "μ„ λ°°λ‘œ μ—…λ°μ΄νŠΈ", description = "λŒ€ν•™μƒ λŒ€ν•™μ›μƒμœΌλ‘œ λ³€κ²½")
public ResponseDto<JwtTokenResponse> changeSenior(@AuthenticationPrincipal User user,
@RequestBody SeniorChangeRequest changeRequest) {
User changeUser = signUpUseCase.changeSenior(user, changeRequest);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.postgraduate.domain.senior.application.dto.res;

import jakarta.validation.constraints.NotNull;

import java.util.List;

public record AllSeniorSearchResponse(@NotNull List<SeniorSearchResponse> seniorSearchResponses) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.postgraduate.domain.senior.application.dto.res;

import jakarta.validation.constraints.NotNull;

public record SeniorSearchResponse(
@NotNull
String profile,
@NotNull
String nickName,
@NotNull
String postgradu,
@NotNull
String major,
@NotNull
String lab,
@NotNull
String oneLiner,
@NotNull
String[] keyword
) {}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
import com.postgraduate.domain.auth.application.dto.req.SeniorChangeRequest;
import com.postgraduate.domain.auth.application.dto.req.SeniorSignUpRequest;
import com.postgraduate.domain.senior.application.dto.req.SeniorMyPageProfileRequest;
import com.postgraduate.domain.senior.application.dto.res.SeniorDetailResponse;
import com.postgraduate.domain.senior.application.dto.res.*;
import com.postgraduate.domain.senior.application.dto.req.SeniorProfileRequest;
import com.postgraduate.domain.senior.application.dto.res.SeniorMyPageProfileResponse;
import com.postgraduate.domain.senior.application.dto.res.SeniorMyPageResponse;
import com.postgraduate.domain.senior.application.dto.res.SeniorMyPageUserAccountResponse;
import com.postgraduate.domain.senior.domain.entity.Info;
import com.postgraduate.domain.senior.domain.entity.Profile;
import com.postgraduate.domain.senior.domain.entity.Senior;
Expand All @@ -33,6 +30,8 @@ public static Info mapToInfo(SeniorSignUpRequest request) {
.lab(request.getLab())
.keyword(request.getKeyword())
.field(request.getField())
.totalInfo(request.getMajor() + request.getLab() + request.getField()
+ request.getProfessor() + request.getPostgradu())
.build();
}

Expand Down Expand Up @@ -72,6 +71,8 @@ public static Info mapToInfo(SeniorChangeRequest request) {
.lab(request.getLab())
.keyword(request.getKeyword())
.field(request.getField())
.totalInfo(request.getMajor() + request.getLab() + request.getField()
+ request.getProfessor() + request.getPostgradu())
.build();
}

Expand Down Expand Up @@ -112,6 +113,14 @@ public static SeniorMyPageUserAccountResponse mapToMyPageUserAccount(Senior seni
.build();
}

public static SeniorMyPageUserAccountResponse mapToMyPageUserAccount(Senior senior) {
User user = senior.getUser();
return SeniorMyPageUserAccountResponse.builder()
.profile(user.getProfile())
.nickName(user.getNickName())
.build();
}

public static SeniorDetailResponse mapToSeniorDetail(Senior senior) {
String[] keyword = senior.getInfo().getKeyword().split(",");
return SeniorDetailResponse.builder()
Expand All @@ -128,4 +137,15 @@ public static SeniorDetailResponse mapToSeniorDetail(Senior senior) {
.time(senior.getProfile().getTime())
.build();
}

public static SeniorSearchResponse mapToSeniorSearch(Senior senior) {
User user = senior.getUser();
Info info = senior.getInfo();
Profile profile = senior.getProfile();
String[] keyword = info.getKeyword().split(",");

return new SeniorSearchResponse(user.getProfile(), user.getNickName(),
info.getPostgradu(), info.getMajor(), info.getLab(),
profile.getOneLiner(), keyword);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package com.postgraduate.domain.senior.application.usecase;

import com.postgraduate.domain.senior.application.dto.res.AllSeniorSearchResponse;
import com.postgraduate.domain.senior.application.dto.res.SeniorDetailResponse;
import com.postgraduate.domain.senior.application.dto.res.SeniorSearchResponse;
import com.postgraduate.domain.senior.domain.entity.Senior;
import com.postgraduate.domain.senior.domain.service.SeniorGetService;
import com.postgraduate.domain.senior.domain.service.SeniorUpdateService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

import static com.postgraduate.domain.senior.application.mapper.SeniorMapper.mapToSeniorDetail;
import static com.postgraduate.domain.senior.application.mapper.SeniorMapper.mapToSeniorSearch;

@Service
@Transactional
Expand All @@ -22,4 +29,13 @@ public SeniorDetailResponse getSeniorDetail(Long seniorId) {
seniorUpdateService.updateHit(senior);
return mapToSeniorDetail(senior);
}

public AllSeniorSearchResponse getSearchSenior(String search, Integer page, String sort) {
Page<Senior> allSeniors = seniorGetService.bySearch(search, page, sort);
List<SeniorSearchResponse> selectSeniors = new ArrayList<>();
for (Senior senior : allSeniors.getContent()) {
selectSeniors.add(mapToSeniorSearch(senior));
}
return new AllSeniorSearchResponse(selectSeniors);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
import com.postgraduate.domain.account.domain.service.AccountSaveService;
import com.postgraduate.domain.account.domain.service.AccountUpdateService;
import com.postgraduate.domain.senior.application.dto.req.*;
import com.postgraduate.domain.senior.application.mapper.SeniorMapper;
import com.postgraduate.domain.senior.domain.entity.Profile;
import com.postgraduate.domain.senior.domain.entity.Senior;
import com.postgraduate.domain.senior.domain.service.SeniorGetService;
import com.postgraduate.domain.senior.domain.service.SeniorUpdateService;
import com.postgraduate.domain.senior.exception.NoneAccountException;
import com.postgraduate.domain.user.domain.entity.User;
import com.postgraduate.domain.user.domain.service.UserUpdateService;
import com.postgraduate.global.config.security.util.EncryptorUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

import static com.postgraduate.domain.account.application.mapper.AccountMapper.mapToAccount;
import static com.postgraduate.domain.senior.application.mapper.SeniorMapper.mapToProfile;

Expand Down Expand Up @@ -47,8 +47,7 @@ public void signUpProfile(User user, SeniorProfileRequest profileRequest) {
public void saveAccount(User user, SeniorAccountRequest accountRequest) {
Senior senior = seniorGetService.byUser(user);
String accountNumber = encryptorUtils.encryptData(accountRequest.getAccountNumber());
String rrn = encryptorUtils.encryptData(accountRequest.getRrn());
accountSaveService.saveAccount(mapToAccount(senior, accountRequest, accountNumber, rrn));
accountSaveService.saveAccount(mapToAccount(senior, accountRequest, accountNumber));
}

public void updateSeniorMyPageProfile(User user, SeniorMyPageProfileRequest myPageProfileRequest) {
Expand All @@ -59,9 +58,21 @@ public void updateSeniorMyPageProfile(User user, SeniorMyPageProfileRequest myPa

public void updateSeniorMyPageUserAccount(User user, SeniorMyPageUserAccountRequest myPageUserAccountRequest) {
Senior senior = seniorGetService.byUser(user);
Account account = accountGetService.bySenior(senior).orElseThrow(NoneAccountException::new);
Optional<Account> optionalAccount = accountGetService.bySenior(senior);
if (optionalAccount.isEmpty()) {
updateSeniorMyPageUserAccountNoneAccount(senior, user, myPageUserAccountRequest);
return;
}
Account account = optionalAccount.get();
String accountNumber = encryptorUtils.encryptData(myPageUserAccountRequest.accountNumber());
userUpdateService.updateSeniorUserAccount(user.getUserId(), myPageUserAccountRequest);
accountUpdateService.updateAccount(account, myPageUserAccountRequest, accountNumber);
}

private void updateSeniorMyPageUserAccountNoneAccount(Senior senior, User user, SeniorMyPageUserAccountRequest myPageUserAccountRequest) {
String accountNumber = encryptorUtils.encryptData(myPageUserAccountRequest.accountNumber());
Account account = mapToAccount(senior, myPageUserAccountRequest, accountNumber);
userUpdateService.updateSeniorUserAccount(user.getUserId(), myPageUserAccountRequest);
accountSaveService.saveAccount(account);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ public SeniorMyPageProfileResponse getSeniorMyPageProfile(User user) {

public SeniorMyPageUserAccountResponse getSeniorMyPageUserAccount(User user) {
Senior senior = seniorGetService.byUser(user);
Account account = accountGetService.bySenior(senior).orElseThrow(NoneAccountException::new);
Optional<Account> optionalAccount = accountGetService.bySenior(senior);
if (optionalAccount.isEmpty())
return mapToMyPageUserAccount(senior);
Account account = optionalAccount.get();
String accountNumber = encryptorUtils.decryptData(account.getAccountNumber());
return mapToMyPageUserAccount(senior, account, accountNumber);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,17 @@ public class Info {
@Column(nullable = false)
private String field;

@Column(nullable = false)
private String totalInfo; // λͺ¨λ“  Info정보 String으둜 κ°€μ§€λŠ” 컬럼 - κ²€μƒ‰μ‹œ μ‚¬μš©

public void updateMyPage(SeniorMyPageProfileRequest request) {
this.keyword = request.getKeyword();
this.lab = request.getLab();
this.field = request.getField();
combineTotalInfo();
}

private void combineTotalInfo() {
this.totalInfo = major + lab + field + professor + postgradu + keyword;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.postgraduate.domain.senior.domain.repository;

import com.postgraduate.domain.senior.domain.entity.Senior;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

public interface SeniorDslRepository {
Page<Senior> findAllSenior(String search, String sort, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.postgraduate.domain.senior.domain.repository;

import com.postgraduate.domain.senior.domain.entity.Senior;
import com.querydsl.core.types.Order;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.List;

import static com.postgraduate.domain.senior.domain.entity.QSenior.senior;
import static com.postgraduate.domain.senior.domain.entity.constant.Status.APPROVE;

@RequiredArgsConstructor
@Repository
public class SeniorDslRepositoryImpl implements SeniorDslRepository{
private final JPAQueryFactory queryFactory;

@Override
public Page<Senior> findAllSenior(String search, String sort, Pageable pageable) {
JPAQuery<Senior> query = queryFactory.selectFrom(senior)
.where(
senior.info.totalInfo.like("%" + search + "%"),
senior.status.eq(APPROVE)
)
.orderBy(orderSpecifier(sort));

List<Senior> seniors = query.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();

long total = query.fetchCount();

return new PageImpl<>(seniors, pageable, total);
}

private OrderSpecifier<?> orderSpecifier(String sort) {
if (sort == null)
return new OrderSpecifier<>(Order.DESC, senior.hit);
return switch (sort) {
case "low" -> new OrderSpecifier<>(Order.ASC, senior.hit);
default -> new OrderSpecifier<>(Order.DESC, senior.hit);
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.List;
import java.util.Optional;

public interface SeniorRepository extends JpaRepository<Senior, Long> {
public interface SeniorRepository extends JpaRepository<Senior, Long>, SeniorDslRepository {
Optional<Senior> findByUser(User user);
Optional<Senior> findBySeniorIdAndProfileNotNullAndStatus(Long seniorId, Status status);
List<Senior> findAllByStatus(Status status);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import com.postgraduate.domain.senior.exception.NoneSeniorException;
import com.postgraduate.domain.user.domain.entity.User;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
Expand All @@ -16,6 +19,7 @@
@RequiredArgsConstructor
public class SeniorGetService {
private final SeniorRepository seniorRepository;
private static final int SENIOR_PAGE_SIZE = 10;

public Senior byUser(User user) {
return seniorRepository.findByUser(user).orElseThrow(NoneSeniorException::new);
Expand All @@ -32,4 +36,11 @@ public Senior bySeniorIdWithCertification(Long seniorId) {
public List<Senior> byStatus(Status status) {
return seniorRepository.findAllByStatus(status);
}

public Page<Senior> bySearch(String search, Integer page, String sort) {
if (page == null)
page = 1;
Pageable pageable = PageRequest.of(page-1, SENIOR_PAGE_SIZE);
return seniorRepository.findAllSenior(search, sort, pageable);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.postgraduate.domain.senior.presentation;

import com.postgraduate.domain.senior.application.dto.req.*;
import com.postgraduate.domain.senior.application.dto.res.SeniorDetailResponse;
import com.postgraduate.domain.senior.application.dto.res.SeniorMyPageProfileResponse;
import com.postgraduate.domain.senior.application.dto.res.SeniorMyPageResponse;
import com.postgraduate.domain.senior.application.dto.res.SeniorMyPageUserAccountResponse;
import com.postgraduate.domain.senior.application.dto.res.*;
import com.postgraduate.domain.senior.application.usecase.SeniorInfoUseCase;
import com.postgraduate.domain.senior.application.usecase.SeniorManageUseCase;
import com.postgraduate.domain.senior.application.usecase.SeniorMyPageUseCase;
Expand Down Expand Up @@ -96,4 +93,13 @@ public ResponseDto<SeniorDetailResponse> getSeniorDetails(@PathVariable Long sen
SeniorDetailResponse seniorDetail = seniorInfoUseCase.getSeniorDetail(seniorId);
return ResponseDto.create(SENIOR_FIND.getCode(), GET_SENIOR_INFO.getMessage(), seniorDetail);
}

@GetMapping("/search")
@Operation(summary = "λŒ€ν•™μ›μƒ 검색어 검색")
public ResponseDto<AllSeniorSearchResponse> getSearchSenior(@RequestParam String find,
@RequestParam(required = false) String sort,
@RequestParam(required = false) Integer page) {
AllSeniorSearchResponse searchSenior = seniorInfoUseCase.getSearchSenior(find, page, sort);
return ResponseDto.create(SENIOR_FIND.getCode(), GET_SENIOR_LIST_INFO.getMessage(), searchSenior);
}
}

0 comments on commit 9f4aa01

Please sign in to comment.