Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: User -> Member 키워드 변경 #75

Merged
merged 3 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.ioteatime.meonghanyangserver.auth.dto.request.LoginRequest;
import org.ioteatime.meonghanyangserver.auth.dto.request.VerifyEmailRequest;
import org.ioteatime.meonghanyangserver.common.api.Api;
import org.ioteatime.meonghanyangserver.user.dto.request.JoinRequest;
import org.ioteatime.meonghanyangserver.member.dto.request.JoinRequest;
import org.springframework.web.bind.annotation.RequestBody;

@Tag(name = "Auth Api", description = "인증 관련 API 목록입니다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.ioteatime.meonghanyangserver.auth.service.AuthService;
import org.ioteatime.meonghanyangserver.common.api.Api;
import org.ioteatime.meonghanyangserver.common.type.AuthSuccessType;
import org.ioteatime.meonghanyangserver.user.dto.request.JoinRequest;
import org.ioteatime.meonghanyangserver.member.dto.request.JoinRequest;
import org.springframework.web.bind.annotation.*;

@RestController
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package org.ioteatime.meonghanyangserver.auth.dto.reponse;

import lombok.AllArgsConstructor;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class LoginResponse {
private Long userId;
private String accessToken;
private String refreshToken;
public record LoginResponse(
@NotNull @Schema(description = "회원 ID", example = "1") Long memberId,
@NotBlank @Schema(description = "회원 AccessToken", example = "Bearer abcd...")
String accessToken,
@NotBlank @Schema(description = "회원 RefreshToken", example = "Bearer abcd...")
String refreshToken) {

@Builder
public LoginResponse(Long memberId, String accessToken, String refreshToken) {
this.memberId = memberId;
this.accessToken = accessToken;
this.refreshToken = refreshToken;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.ioteatime.meonghanyangserver.auth.mapper;

import org.ioteatime.meonghanyangserver.user.domain.UserEntity;
import org.ioteatime.meonghanyangserver.user.dto.request.JoinRequest;
import org.ioteatime.meonghanyangserver.member.domain.MemberEntity;
import org.ioteatime.meonghanyangserver.member.dto.request.JoinRequest;

public class AuthEntityMapper {
public static UserEntity of(JoinRequest userDto, String encodedPassword) {
return UserEntity.builder()
public static MemberEntity of(JoinRequest userDto, String encodedPassword) {
return MemberEntity.builder()
.nickname(userDto.getNickname())
.email(userDto.getEmail())
.password(encodedPassword)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

import org.ioteatime.meonghanyangserver.auth.dto.reponse.LoginResponse;
import org.ioteatime.meonghanyangserver.auth.dto.reponse.RefreshResponse;
import org.ioteatime.meonghanyangserver.user.dto.response.UserSimpleResponse;
import org.ioteatime.meonghanyangserver.member.dto.response.MemberSimpleResponse;

public class AuthResponseMapper {
public static UserSimpleResponse from(Long id, String email) {
return new UserSimpleResponse(id, email);
public static MemberSimpleResponse from(Long id, String email) {
return new MemberSimpleResponse(id, email);
}

public static LoginResponse from(Long id, String accessToken, String refreshToken) {
return LoginResponse.builder()
.userId(id)
.memberId(id)
.accessToken(accessToken)
.refreshToken(refreshToken)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@
import org.ioteatime.meonghanyangserver.common.type.AuthErrorType;
import org.ioteatime.meonghanyangserver.common.utils.JwtUtils;
import org.ioteatime.meonghanyangserver.device.repository.DeviceRepository;
import org.ioteatime.meonghanyangserver.member.domain.MemberEntity;
import org.ioteatime.meonghanyangserver.member.dto.request.JoinRequest;
import org.ioteatime.meonghanyangserver.member.dto.response.MemberSimpleResponse;
import org.ioteatime.meonghanyangserver.member.repository.MemberRepository;
import org.ioteatime.meonghanyangserver.redis.EmailCode;
import org.ioteatime.meonghanyangserver.redis.EmailCodeRepository;
import org.ioteatime.meonghanyangserver.redis.RefreshToken;
import org.ioteatime.meonghanyangserver.redis.RefreshTokenRepository;
import org.ioteatime.meonghanyangserver.user.domain.UserEntity;
import org.ioteatime.meonghanyangserver.user.dto.request.JoinRequest;
import org.ioteatime.meonghanyangserver.user.dto.response.UserSimpleResponse;
import org.ioteatime.meonghanyangserver.user.repository.UserRepository;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class AuthService {
private final UserRepository userRepository;
private final MemberRepository memberRepository;
private final GoogleMailClient googleMailClient;
private final BCryptPasswordEncoder bCryptPasswordEncoder;
private final JwtUtils jwtUtils;
Expand All @@ -39,19 +39,19 @@ public class AuthService {
private final DeviceRepository deviceRepository;

public LoginResponse login(LoginRequest loginRequest) {
UserEntity userEntity =
userRepository
MemberEntity memberEntity =
memberRepository
.findByEmail(loginRequest.email())
.orElseThrow(() -> new NotFoundException(AuthErrorType.NOT_FOUND));

boolean passwordMatch =
bCryptPasswordEncoder.matches(loginRequest.password(), userEntity.getPassword());
bCryptPasswordEncoder.matches(loginRequest.password(), memberEntity.getPassword());
if (!passwordMatch) {
throw new BadRequestException(AuthErrorType.PASSWORD_NOT_MATCH);
}

String accessToken = jwtUtils.generateAccessToken(userEntity);
String refreshToken = jwtUtils.generateRefreshToken(userEntity);
String accessToken = jwtUtils.generateAccessToken(memberEntity);
String refreshToken = jwtUtils.generateRefreshToken(memberEntity);

if (accessToken.isEmpty() || refreshToken.isEmpty()) {
throw new NotFoundException(AuthErrorType.TOKEN_NOT_FOUND);
Expand All @@ -62,15 +62,15 @@ public LoginResponse login(LoginRequest loginRequest) {
accessToken = jwtUtils.includeBearer(accessToken);
refreshToken = jwtUtils.includeBearer(refreshToken);

return AuthResponseMapper.from(userEntity.getId(), accessToken, refreshToken);
return AuthResponseMapper.from(memberEntity.getId(), accessToken, refreshToken);
}

public UserSimpleResponse joinProcess(JoinRequest userDto) {
public MemberSimpleResponse joinProcess(JoinRequest userDto) {
String encodedPassword = bCryptPasswordEncoder.encode(userDto.getPassword());
verifyEmail(userDto.getEmail());
UserEntity user = userRepository.save(AuthEntityMapper.of(userDto, encodedPassword));
MemberEntity member = memberRepository.save(AuthEntityMapper.of(userDto, encodedPassword));

return AuthResponseMapper.from(user.getId(), user.getEmail());
return AuthResponseMapper.from(member.getId(), member.getEmail());
}

public void send(String email) {
Expand Down Expand Up @@ -103,7 +103,7 @@ private static String getCode() {
}

public void verifyEmail(String email) {
if (userRepository.findByEmail(email).isPresent()) {
if (memberRepository.findByEmail(email).isPresent()) {
throw new BadRequestException(AuthErrorType.EMAIL_DUPLICATED);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.Objects;
import javax.crypto.SecretKey;
import lombok.extern.slf4j.Slf4j;
import org.ioteatime.meonghanyangserver.user.domain.UserEntity;
import org.ioteatime.meonghanyangserver.member.domain.MemberEntity;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

Expand All @@ -25,14 +25,14 @@ public JwtUtils(Environment env) {
this.key = Keys.hmacShaKeyFor(decodedKey);
}

public String generateAccessToken(UserEntity userEntity) {
public String generateAccessToken(MemberEntity memberEntity) {
Date nowDate = new Date();
Date expiration = new Date(nowDate.getTime() + Duration.ofHours(2).toMillis());
String jwtToken =
Jwts.builder()
.claim("name", userEntity.getNickname())
.claim("name", memberEntity.getNickname())
.claim("sub", "meong-ha-nyang")
.claim("jti", String.valueOf(userEntity.getId()))
.claim("jti", String.valueOf(memberEntity.getId()))
.claim("iat", nowDate)
.claim("exp", expiration)
.signWith(key)
Expand All @@ -41,14 +41,14 @@ public String generateAccessToken(UserEntity userEntity) {
return jwtToken;
}

public String generateRefreshToken(UserEntity userEntity) {
public String generateRefreshToken(MemberEntity memberEntity) {
Date nowDate = new Date();
Date expiration = new Date(nowDate.getTime() + Duration.ofDays(30).toMillis());
String jwtToken =
Jwts.builder()
.claim("name", userEntity.getNickname())
.claim("name", memberEntity.getNickname())
.claim("sub", "meong-ha-nyang")
.claim("jti", String.valueOf(userEntity.getId()))
.claim("jti", String.valueOf(memberEntity.getId()))
.claim("iat", nowDate)
.claim("exp", expiration)
.signWith(key)
Expand Down Expand Up @@ -81,13 +81,13 @@ private boolean isTokenExpired(String token) {
return expiration.before(new Date());
}

public boolean validateToken(String token, UserEntity userEntity) {
public boolean validateToken(String token, MemberEntity memberEntity) {
if (isTokenExpired(token)) {
return false;
}

Long jwtId = getIdFromToken(token);
Long id = userEntity.getId();
Long id = memberEntity.getId();

return id != null && Objects.equals(jwtId, id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.ioteatime.meonghanyangserver.cctv.domain.CctvEntity;
import org.ioteatime.meonghanyangserver.device.doamin.enums.DeviceRole;
import org.ioteatime.meonghanyangserver.group.domain.GroupEntity;
import org.ioteatime.meonghanyangserver.user.domain.UserEntity;
import org.ioteatime.meonghanyangserver.member.domain.MemberEntity;

@Data
@Entity
Expand All @@ -33,8 +33,8 @@ public class DeviceEntity {
private GroupEntity group;

@ManyToOne
@JoinColumn(name = "user_id", unique = true)
private UserEntity user;
@JoinColumn(name = "member_id", unique = true)
private MemberEntity member;

@OneToOne(mappedBy = "device")
private CctvEntity cctv;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@

@Schema(description = "디바이스 정보 응답")
public record DeviceInfoResponse(
@NotNull Long userId, @NotBlank String nickname, @NotBlank DeviceRole role) {}
@NotNull Long memberId, @NotBlank String nickname, @NotBlank DeviceRole role) {}
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
import org.ioteatime.meonghanyangserver.device.doamin.DeviceEntity;
import org.ioteatime.meonghanyangserver.device.doamin.enums.DeviceRole;
import org.ioteatime.meonghanyangserver.group.domain.GroupEntity;
import org.ioteatime.meonghanyangserver.user.domain.UserEntity;
import org.ioteatime.meonghanyangserver.member.domain.MemberEntity;

public class DeviceEntityMapper {

// master mapper
public static DeviceEntity from(
GroupEntity groupEntity,
UserEntity userEntity,
MemberEntity memberEntity,
DeviceRole deviceRole,
String deviceUuid) {
return DeviceEntity.builder()
.id(groupEntity.getId())
.user(userEntity)
.member(memberEntity)
.role(deviceRole)
.group(groupEntity)
.user(userEntity)
.member(memberEntity)
.deviceUuid(deviceUuid)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
public class DeviceResponseMapper {
public static DeviceInfoResponse from(DeviceEntity deviceEntity) {
return new DeviceInfoResponse(
deviceEntity.getUser().getId(),
deviceEntity.getUser().getNickname(),
deviceEntity.getMember().getId(),
deviceEntity.getMember().getNickname(),
deviceEntity.getRole());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
public interface DeviceRepository {
DeviceEntity createDevice(DeviceEntity deviceEntity);

boolean existsDevice(Long userId);
boolean existsDevice(Long memberId);

Optional<DeviceEntity> findByDeviceId(Long userId);
Optional<DeviceEntity> findByDeviceId(Long memberId);

GroupEntity findDevice(Long userId);
GroupEntity findDevice(Long memberId);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.ioteatime.meonghanyangserver.device.repository;

import static org.ioteatime.meonghanyangserver.device.doamin.QDeviceEntity.deviceEntity;

import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.ioteatime.meonghanyangserver.device.doamin.DeviceEntity;
import org.ioteatime.meonghanyangserver.device.doamin.QDeviceEntity;
import org.ioteatime.meonghanyangserver.group.domain.GroupEntity;
import org.springframework.stereotype.Repository;

Expand All @@ -21,23 +22,21 @@ public DeviceEntity createDevice(DeviceEntity deviceEntity) {
}

@Override
public boolean existsDevice(Long userId) {
return jpaDeviceRepository.existsByUserId(userId);
public boolean existsDevice(Long memberId) {
return jpaDeviceRepository.existsByMemberId(memberId);
}

@Override
public Optional<DeviceEntity> findByDeviceId(Long userId) {
return jpaDeviceRepository.findByUserId(userId);
public Optional<DeviceEntity> findByDeviceId(Long memberId) {
return jpaDeviceRepository.findByMemberId(memberId);
}

@Override
public GroupEntity findDevice(Long userId) {
QDeviceEntity groupUserEntity = QDeviceEntity.deviceEntity;

public GroupEntity findDevice(Long memberId) {
return jpaQueryFactory
.select(groupUserEntity.group)
.where(groupUserEntity.user.id.eq(userId))
.from(groupUserEntity)
.select(deviceEntity.group)
.from(deviceEntity)
.where(deviceEntity.member.id.eq(memberId))
.fetchOne();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.springframework.data.jpa.repository.JpaRepository;

public interface JpaDeviceRepository extends JpaRepository<DeviceEntity, Long> {
boolean existsByUserId(Long userId);
boolean existsByMemberId(Long memberId);

Optional<DeviceEntity> findByUserId(Long userId);
Optional<DeviceEntity> findByMemberId(Long memberId);
}
Loading
Loading