Skip to content

Commit

Permalink
check nickname when create or update (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanmomo authored May 2, 2024
1 parent e6086f9 commit 076fc3b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ public Predicate toPredicate(Root<Member> root, CriteriaQuery<?> criteriaQuery,

@PostMapping("/create")
public BaseResponse createMember(@Valid @RequestBody Member member, @RequestParam String address, HttpServletRequest request) {
this.memberService.nickNameExists(member.getNickName());

member.setGithubId(HttpSessionUtils.getOAuthUserName(request.getSession(), OAuthSessionKey.GITHUB_USER_NAME));
member.setTweetId(HttpSessionUtils.getOAuthUserName(request.getSession(), OAuthSessionKey.TWITTER_USER_NAME));
member.setTweetScreenName(HttpSessionUtils.getOAuthUserName(request.getSession(), OAuthSessionKey.TWITTER_SCREEN_NAME));
Expand All @@ -142,9 +144,10 @@ public BaseResponse createMember(@Valid @RequestBody Member member, @RequestPara
return BaseResponse.successWithData(_member);
}


@PutMapping("/update")
public BaseResponse updateMemberByAddress(@RequestParam String address, @RequestBody MemberVo member, HttpServletRequest request) {
this.memberService.nickNameExists(member.getNickName());

Optional<Member> memberData = memberRepository.findByAddress(address);

if (memberData.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@ public interface MemberRepository extends JpaRepository<Member, Long>, JpaSpecif
@Query(value = "select * from member where address = :address", nativeQuery = true)
Optional<Member> findByAddress(@Param("address") String address);

@Query(value = "select * from member where nick_name = :nickName", nativeQuery = true)
Optional<Member> findByNickName(@Param("nickName") String nickName);

List<Member> findByIdIn(List<Long> ids);
}
23 changes: 16 additions & 7 deletions src/main/java/com/dl/officialsite/member/MemberService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.dl.officialsite.member;

import static com.dl.officialsite.common.enums.CodeEnums.INVALID_MEMBER;

import com.dl.officialsite.common.enums.CodeEnums;
import com.dl.officialsite.common.exception.BizException;
import com.dl.officialsite.common.utils.UserSecurityUtils;
Expand All @@ -11,16 +9,20 @@
import com.dl.officialsite.team.TeamService;
import com.dl.officialsite.team.teammember.TeamMember;
import com.dl.officialsite.team.teammember.TeamMemberRepository;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import javax.transaction.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import static com.dl.officialsite.common.enums.CodeEnums.INVALID_MEMBER;

@Service
@Slf4j
public class MemberService {
Expand Down Expand Up @@ -110,4 +112,11 @@ public void freeze(String memberAddress) {
member.setStatus(1);
memberRepository.save(member);
}


public void nickNameExists(String nickName){
if(StringUtils.isNotBlank(nickName) && this.memberRepository.findByNickName(nickName).isPresent()){
throw new RuntimeException("Nickname already exists, please change another one.") ;
}
}
}

0 comments on commit 076fc3b

Please sign in to comment.