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

Feature/users 추가/수정 #80

Merged
merged 6 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -5,7 +5,6 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
Expand All @@ -15,10 +14,12 @@
import site.billbill.apiserver.api.auth.dto.request.DeviceRequest;
import site.billbill.apiserver.api.auth.dto.request.LocationRequest;
import site.billbill.apiserver.api.users.dto.request.BlacklistRequest;
import site.billbill.apiserver.api.users.dto.request.PasswordRequest;
import site.billbill.apiserver.api.users.dto.response.*;
import site.billbill.apiserver.api.users.service.UserService;
import site.billbill.apiserver.common.response.BaseResponse;
import site.billbill.apiserver.common.utils.posts.ItemHistoryType;
import site.billbill.apiserver.model.common.CodeDetailJpaEntity;

import java.util.List;

Expand Down Expand Up @@ -134,4 +135,26 @@ public BaseResponse<String> updateLocation(@RequestBody LocationRequest request)
userService.saveLocation(null, request);
return new BaseResponse<>(null);
}

@Operation(summary = "비밀번호 확인", description = "비밀번호를 확인하는 API")
@ResponseStatus(HttpStatus.OK)
@GetMapping("/password-check")
public BaseResponse<Boolean> checkPassword(@RequestParam(name = "password") String password) {
return new BaseResponse<>(userService.checkOriginalPassword(password));
}

@Operation(summary = "비밀번호 변경", description = "비밀번호를 변경하는 API")
@ResponseStatus(HttpStatus.OK)
@PatchMapping("/password")
public BaseResponse<String> updatePassword(@RequestBody PasswordRequest request) {
userService.updatePassword(request);
return new BaseResponse<>(null);
}

@Operation(summary = "회원 탈퇴 코드 목록 조회", description = "회원 탈퇴 코드 목록 조회 API")
@ResponseStatus(HttpStatus.OK)
@GetMapping("/withdraw/code")
public BaseResponse<List<CodeDetailJpaEntity>> getWithdrawCodeList() {
return new BaseResponse<>(userService.getWithdrawCodeList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package site.billbill.apiserver.api.users.dto.request;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

@Data
public class PasswordCheckRequest {
@Schema(description = "기존 비밀번호", example = "original password")
private String password;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package site.billbill.apiserver.api.users.dto.request;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

@Data
public class PasswordRequest {
@Schema(description = "기존 비밀번호", example = "original password")
private String password;
@Schema(description = "새 비밀번호", example = "new password")
private String newPassword;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class BorrowHistoryResponse {
example = "[\"image1.jpg\", \"image2.jpg\"]"
)
private List<String> itemImages;
@Schema(description = "물건 가격", type = "int", example = "18000")
private Integer price;
@Schema(description = "물품 이름", example = "Pronto600 폴라로이드 카메라")
private String title;
@Schema(description = "대여 시작 일자", example = "yyyy-MM-dd")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class PostHistoryResponse {
example = "[\"image1.jpg\", \"image2.jpg\"]"
)
private List<String> itemImages;
@Schema(description = "물건 가격", type = "int", example = "18000")
private Integer price;
@Schema(description = "물품 이름", example = "Pronto600 폴라로이드 카메라")
private String title;
@Schema(description = "아이템 상태", example = "3")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class ProfileResponse {
private String nickname;
@Schema(description = "전화번호", example = "010-1234-5678")
private String phoneNumber;
@Schema(description = "빌빌 페이스", example = "40")
private int billPace;
@Enumerated(EnumType.STRING)
@Schema(description = "소셜 로그인 제공사", example = "KAKAO")
private Provider provider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import org.springframework.data.domain.Pageable;
import site.billbill.apiserver.api.auth.dto.request.DeviceRequest;
import site.billbill.apiserver.api.auth.dto.request.LocationRequest;
import site.billbill.apiserver.api.users.dto.request.PasswordRequest;
import site.billbill.apiserver.api.users.dto.response.*;
import site.billbill.apiserver.common.utils.posts.ItemHistoryType;
import site.billbill.apiserver.model.common.CodeDetailJpaEntity;

import java.util.List;

Expand All @@ -30,4 +32,10 @@ public interface UserService {
void updateDevice(DeviceRequest request);

void saveLocation(String userId, LocationRequest location);

Boolean checkOriginalPassword(String password);

void updatePassword(PasswordRequest request);

List<CodeDetailJpaEntity> getWithdrawCodeList();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,25 @@
import org.slf4j.MDC;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import site.billbill.apiserver.api.auth.dto.request.DeviceRequest;
import site.billbill.apiserver.api.auth.dto.request.LocationRequest;
import site.billbill.apiserver.api.users.dto.request.PasswordRequest;
import site.billbill.apiserver.api.users.dto.response.*;
import site.billbill.apiserver.common.enums.exception.ErrorCode;
import site.billbill.apiserver.common.utils.jwt.JWTUtil;
import site.billbill.apiserver.common.utils.posts.ItemHistoryType;
import site.billbill.apiserver.exception.CustomException;
import site.billbill.apiserver.model.common.CodeDetailJpaEntity;
import site.billbill.apiserver.model.user.UserBlacklistJpaEntity;
import site.billbill.apiserver.model.user.UserDeviceJpaEntity;
import site.billbill.apiserver.model.user.UserIdentityJpaEntity;
import site.billbill.apiserver.model.user.UserJpaEntity;
import site.billbill.apiserver.model.user.UserLocationJpaEntity;
import site.billbill.apiserver.repository.borrowPosts.ItemsRepository;
import site.billbill.apiserver.repository.common.CodeDetailRepository;
import site.billbill.apiserver.repository.user.UserBlacklistRepository;
import site.billbill.apiserver.repository.user.UserDeviceRepository;
import site.billbill.apiserver.repository.user.UserIdentityRepository;
Expand All @@ -42,9 +46,11 @@ public class UserServiceImpl implements UserService {
private final UserIdentityRepository userIdentityRepository;
private final UserBlacklistRepository userBlacklistRepository;
private final ItemsRepository itemsRepository;
private final JWTUtil jWTUtil;
private final JWTUtil jwtUtil;
private final BCryptPasswordEncoder bCryptPasswordEncoder;
private final UserLocationReposity userLocationRepository;
private final UserDeviceRepository userDeviceRepository;
private final CodeDetailRepository codeDetailRepository;

private final GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(), 4326);

Expand Down Expand Up @@ -76,6 +82,7 @@ public ProfileResponse getProfileInfo(String userId) {
.profileImage(user.get().getProfile())
.nickname(user.get().getNickname())
.phoneNumber(userIdentity.get().getPhoneNumber())
.billPace(user.get().getBillPace())
.provider(user.get().getProvider())
.location(location)
.build();
Expand Down Expand Up @@ -190,4 +197,42 @@ public void saveLocation(

userLocationRepository.save(userLocation);
}

@Override
public Boolean checkOriginalPassword(String password) {
String userId = MDC.get(JWTUtil.MDC_USER_ID);
UserJpaEntity user = userRepository.findById(userId).orElseThrow();

return checkPassword(password, user.getPassword());
}

@Override
public void updatePassword(PasswordRequest request) {
String userId = MDC.get(JWTUtil.MDC_USER_ID);
UserJpaEntity user = userRepository.findById(userId).orElseThrow();

if (!checkPassword(request.getPassword(), user.getPassword()))
throw new CustomException(ErrorCode.Unauthorized, "비밀번호를 확인해 주세요.", HttpStatus.UNAUTHORIZED);

user.setPassword(bCryptPasswordEncoder.encode(request.getNewPassword()));

userRepository.save(user);
}

@Override
public List<CodeDetailJpaEntity> getWithdrawCodeList() {
return codeDetailRepository.findByIdGroupCode("WITHDRAW_CODE");
}


/**
* Method that check password is right
*
* @param password password plaintext
* @param encryptedPassword password encrypted text
* @return isPassword correct true/false
*/
private boolean checkPassword(String password, String encryptedPassword) {
return bCryptPasswordEncoder.matches(password, encryptedPassword);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package site.billbill.apiserver.model.common;

import jakarta.persistence.Column;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import lombok.*;
import site.billbill.apiserver.model.common.embedded.CodeDetailId;

@Entity
@Table(name = "code_detail")
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CodeDetailJpaEntity {
@EmbeddedId
private CodeDetailId id;

@Column(name = "name")
private String name;
@Column(name = "seq")
private Integer seq;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package site.billbill.apiserver.model.common.embedded;

import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

@Embeddable
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
public class CodeDetailId {
@Column(name = "code")
private String code;
@Column(name = "group_code")
private String groupCode;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package site.billbill.apiserver.model.post.emebeded;
package site.billbill.apiserver.model.post.embedded;

import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
Expand All @@ -15,8 +15,8 @@
@EqualsAndHashCode
@Data
public class ItemsLikeId implements Serializable {
@Column(name="item_id")
@Column(name = "item_id")
private String itemId;
@Column(name="user_id")
@Column(name = "user_id")
private String userId;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package site.billbill.apiserver.model.post;

import jakarta.mail.FetchProfile;
import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.DynamicUpdate;
import site.billbill.apiserver.common.converter.BooleanConverter;
import site.billbill.apiserver.model.BaseTime;
import site.billbill.apiserver.model.post.emebeded.ItemsLikeId;
import site.billbill.apiserver.model.post.embedded.ItemsLikeId;
import site.billbill.apiserver.model.user.UserJpaEntity;

@DynamicUpdate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class UserJpaEntity extends BaseTime {
@Column(name = "profile", nullable = true)
private String profile;
@Column(name = "bill_pace", nullable = false)
private int billPace = 0;
private int billPace = 40;
@Enumerated(EnumType.STRING)
@Column(name = "provider", nullable = true)
private Provider provider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ private BooleanExpression applyKeywordFilter(QItemsJpaEntity items, String keywo
@Override
public List<PostHistoryResponse> getPostHistory(String userId, Pageable pageable) {
QItemsJpaEntity qItems = QItemsJpaEntity.itemsJpaEntity;
QItemsBorrowJpaEntity qBorrow = QItemsBorrowJpaEntity.itemsBorrowJpaEntity;
QitemsLikeJpaEntity qLike = QitemsLikeJpaEntity.itemsLikeJpaEntity;
QChatChannelJpaEntity qChatChannel = QChatChannelJpaEntity.chatChannelJpaEntity;

Expand All @@ -150,6 +151,7 @@ public List<PostHistoryResponse> getPostHistory(String userId, Pageable pageable
PostHistoryResponse.class,
qItems.id,
qItems.images,
qBorrow.price,
qItems.title,
qItems.itemStatus,
qLike.countDistinct().as("likeCount"),
Expand All @@ -160,6 +162,7 @@ public List<PostHistoryResponse> getPostHistory(String userId, Pageable pageable
)
)
.from(qItems)
.leftJoin(qBorrow).on(qItems.id.eq(qBorrow.id))
.leftJoin(qLike).on(qItems.id.eq(qLike.items.id).and(qLike.delYn.isFalse()))
.leftJoin(qChatChannel).on(qItems.id.eq(qChatChannel.item.id).and(qChatChannel.delYn.isFalse()))
.where(qItems.owner.userId.eq(userId)
Expand All @@ -174,6 +177,7 @@ public List<PostHistoryResponse> getPostHistory(String userId, Pageable pageable
@Override
public List<BorrowHistoryResponse> getBorrowHistory(String userId, Pageable pageable, ItemHistoryType type) {
QItemsJpaEntity qItems = QItemsJpaEntity.itemsJpaEntity;
QItemsBorrowJpaEntity qBorrow = QItemsBorrowJpaEntity.itemsBorrowJpaEntity;
QitemsLikeJpaEntity qLike = QitemsLikeJpaEntity.itemsLikeJpaEntity;
QChatChannelJpaEntity qChatChannel = QChatChannelJpaEntity.chatChannelJpaEntity;
QBorrowHistJapEntity qBorrowHist = QBorrowHistJapEntity.borrowHistJapEntity;
Expand All @@ -186,6 +190,7 @@ public List<BorrowHistoryResponse> getBorrowHistory(String userId, Pageable page
qItems.owner.userId.as("borrowerId"),
Expressions.constant(type),
qItems.images,
qBorrow.price,
qItems.title,
qBorrowHist.startedAt.as("startedAt"),
qBorrowHist.endedAt.as("endedAt"),
Expand All @@ -198,6 +203,7 @@ public List<BorrowHistoryResponse> getBorrowHistory(String userId, Pageable page
)
)
.from(qItems)
.leftJoin(qBorrow).on(qItems.id.eq(qBorrow.id))
.leftJoin(qLike).on(qItems.id.eq(qLike.items.id).and(qLike.delYn.isFalse()))
.leftJoin(qChatChannel).on(qItems.id.eq(qChatChannel.item.id).and(qChatChannel.delYn.isFalse()))
.rightJoin(qBorrowHist).on(qItems.id.eq(qBorrowHist.item.id).and(qBorrowHist.delYn.isFalse()))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package site.billbill.apiserver.repository.common;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import site.billbill.apiserver.model.common.CodeDetailJpaEntity;
import site.billbill.apiserver.model.common.embedded.CodeDetailId;

import java.util.List;

@Repository
public interface CodeDetailRepository extends JpaRepository<CodeDetailJpaEntity, CodeDetailId> {
List<CodeDetailJpaEntity> findByIdGroupCode(String groupCode);
}
Loading