Skip to content

Commit

Permalink
feat: check whether email or username already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
peageon committed May 15, 2024
1 parent f942044 commit 8f3c234
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,8 @@ public ResponseEntity<Object> userSignup(@Valid @RequestBody MemberSignupRequest
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errors);
}

try {
Member newMember = memberService.signup(memberSignupRequest);
return ResponseEntity.ok().body(memberSignupRequest.getUsername() + " successfully registered.");
} catch (BadRequestException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
}
Member newMember = memberService.signup(memberSignupRequest);
return ResponseEntity.ok().body(memberSignupRequest.getUsername() + " successfully registered.");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.runningmate.backend.member.exception;

import java.util.List;

public class FieldExistsException extends RuntimeException {
private List<String> exists;

public FieldExistsException(List<String> exists) {
super("Fields already exist: " + exists);
this.exists = exists;
}

public List<String> getExists() {
return exists;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,57 @@
import com.runningmate.backend.member.Member;
import com.runningmate.backend.member.MemberSignupRequest;
import com.runningmate.backend.member.Role;
import com.runningmate.backend.member.exception.FieldExistsException;
import com.runningmate.backend.member.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
import org.apache.coyote.BadRequestException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

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

@Service
@RequiredArgsConstructor
public class MemberService {
private final MemberRepository memberRepository;
private final PasswordEncoder passwordEncoder;


public Member signup(MemberSignupRequest memberSignupRequest) throws BadRequestException {
if (memberRepository.existsByUsername(memberSignupRequest.getUsername())) {
throw new BadRequestException("User already exists");
public Member signup(MemberSignupRequest memberSignupRequest) throws FieldExistsException {
List<String> exists = checkExistingFields(memberSignupRequest);

if (!exists.isEmpty()) {
throw new FieldExistsException(exists);
}

Member member = Member.builder()
.email(memberSignupRequest.getEmail())
.name(memberSignupRequest.getName())
.username(memberSignupRequest.getUsername())
.password(passwordEncoder.encode(memberSignupRequest.getPassword()))
.role(Role.USER)
.build();
Member member = createMember(memberSignupRequest);

memberRepository.save(member);
return member;
}

private List<String> checkExistingFields(MemberSignupRequest request) {
List<String> exists = new ArrayList<>();

if (memberRepository.existsByEmail(request.getEmail())) {
exists.add("email");
}
if (memberRepository.existsByUsername(request.getUsername())) {
exists.add("username");
}

return exists;
}

private Member createMember(MemberSignupRequest request) {
return Member.builder()
.email(request.getEmail())
.name(request.getName())
.username(request.getUsername())
.password(passwordEncoder.encode(request.getPassword()))
.role(Role.USER)
.build();
}

}

0 comments on commit 8f3c234

Please sign in to comment.