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 API 개발 및 중간 model 공유 #64

Merged
merged 5 commits into from
Nov 24, 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,6 +5,7 @@
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 @@ -13,9 +14,12 @@
import org.springframework.web.bind.annotation.*;
import site.billbill.apiserver.api.users.dto.request.BlacklistRequest;
import site.billbill.apiserver.api.users.dto.response.BlacklistResponse;
import site.billbill.apiserver.api.users.dto.response.BorrowHistoryResponse;
import site.billbill.apiserver.api.users.dto.response.PostHistoryResponse;
import site.billbill.apiserver.api.users.dto.response.ProfileResponse;
import site.billbill.apiserver.api.users.service.UserService;
import site.billbill.apiserver.common.response.BaseResponse;
import site.billbill.apiserver.common.utils.posts.ItemHistoryType;

import java.util.List;

Expand Down Expand Up @@ -81,4 +85,27 @@ public BaseResponse<String> withdraw() {
userService.withdraw();
return new BaseResponse<>(null);
}

@Operation(summary = "내 글 조회", description = "대여중인 글 조회 API")
@ResponseStatus(HttpStatus.OK)
@GetMapping("/history/posts")
public BaseResponse<List<PostHistoryResponse>> postsHistory(
@RequestParam(name = "size", defaultValue = "20") int size,
@RequestParam(name = "page", defaultValue = "1") int page
) {
Pageable pageable = PageRequest.of((page < 1 ? 0 : page - 1), size);
return new BaseResponse<>(userService.getPostHistory(pageable));
}

@Operation(summary = "내 대여 기록 보기", description = "내 대여기록 보기 API")
@ResponseStatus(HttpStatus.OK)
@GetMapping("/history/{type}")
public BaseResponse<List<BorrowHistoryResponse>> postsHistory(
@RequestParam(name = "size", defaultValue = "20") int size,
@RequestParam(name = "page", defaultValue = "1") int page,
@PathVariable ItemHistoryType type
) {
Pageable pageable = PageRequest.of((page < 1 ? 0 : page - 1), size);
return new BaseResponse<>(userService.getPostHistory(pageable, type));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package site.billbill.apiserver.api.users.dto.response;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import lombok.AllArgsConstructor;
import lombok.Data;
import site.billbill.apiserver.common.utils.posts.ItemHistoryType;

import java.time.OffsetDateTime;
import java.util.List;

@Data
@AllArgsConstructor
public class BorrowHistoryResponse {
@Schema(description = "대여 기록 번호", example = "2")
private long borrowSeq;
@Schema(description = "물품 ID", example = "ITEM-XXXXX...")
private String itemId;
@Schema(description = "대여자 ID", example = "USER-XXXXX...")
private String borrowerId;
@Enumerated(EnumType.STRING)
@Schema(description = "물품 타입", example = "BORROWING / BORROWED / EXCHANGE")
private ItemHistoryType type;
@Schema(
description = "물품 이미지 리스트",
type = "array",
example = "[\"image1.jpg\", \"image2.jpg\"]"
)
private List<String> itemImages;
@Schema(description = "물품 이름", example = "Pronto600 폴라로이드 카메라")
private String title;
@Schema(description = "대여 시작 일자", example = "yyyy-MM-dd")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private OffsetDateTime startedAt;
@Schema(description = "대여 종료 일자", example = "yyyy-MM-dd")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private OffsetDateTime endedAt;
@Schema(description = "아이템 상태", example = "3")
private int itemStatus;
@Schema(description = "좋아요 수", example = "12")
private long likeCount;
@Schema(description = "채팅 수", example = "1")
private long chatCount;
@Schema(description = "조회 수", example = "12")
private int viewCount;
@Schema(description = "생성일시", example = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Seoul")
private OffsetDateTime createdAt;
@Schema(description = "수정일시", example = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Seoul")
private OffsetDateTime updatedAt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package site.billbill.apiserver.api.users.dto.response;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import lombok.AllArgsConstructor;
import lombok.Data;
import site.billbill.apiserver.common.utils.posts.ItemType;

import java.time.OffsetDateTime;
import java.util.List;

@Data
@AllArgsConstructor
public class PostHistoryResponse {
@Schema(description = "물품 ID", example = "ITEM-XXXXX...")
private String itemId;
// @Enumerated(EnumType.STRING)
// @Schema(description = "물품 타입", example = "BORROW")
// private ItemType type;
@Schema(
description = "물품 이미지 리스트",
type = "array",
example = "[\"image1.jpg\", \"image2.jpg\"]"
)
private List<String> itemImages;
@Schema(description = "물품 이름", example = "Pronto600 폴라로이드 카메라")
private String title;
@Schema(description = "아이템 상태", example = "3")
private int itemStatus;
@Schema(description = "좋아요 수", example = "12")
private long likeCount;
@Schema(description = "채팅 수", example = "1")
private long chatCount;
@Schema(description = "조회 수", example = "12")
private int viewCount;
@Schema(description = "생성일시", example = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Seoul")
private OffsetDateTime createdAt;
@Schema(description = "수정일시", example = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Seoul")
private OffsetDateTime updatedAt;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import org.springframework.data.domain.Pageable;
import site.billbill.apiserver.api.users.dto.response.BlacklistResponse;
import site.billbill.apiserver.api.users.dto.response.BorrowHistoryResponse;
import site.billbill.apiserver.api.users.dto.response.PostHistoryResponse;
import site.billbill.apiserver.api.users.dto.response.ProfileResponse;
import site.billbill.apiserver.common.utils.posts.ItemHistoryType;

import java.util.List;

Expand All @@ -18,4 +21,8 @@ public interface UserService {
void blockCancel(String userId);

void withdraw();

List<PostHistoryResponse> getPostHistory(Pageable pageable);

List<BorrowHistoryResponse> getPostHistory(Pageable pageable, ItemHistoryType type);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import site.billbill.apiserver.api.users.dto.response.BlacklistResponse;
import site.billbill.apiserver.api.users.dto.response.BorrowHistoryResponse;
import site.billbill.apiserver.api.users.dto.response.PostHistoryResponse;
import site.billbill.apiserver.api.users.dto.response.ProfileResponse;
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.user.UserBlacklistJpaEntity;
import site.billbill.apiserver.model.user.UserIdentityJpaEntity;
import site.billbill.apiserver.model.user.UserJpaEntity;
import site.billbill.apiserver.repository.borrowPosts.ItemsRepository;
import site.billbill.apiserver.repository.user.UserBlacklistRepository;
import site.billbill.apiserver.repository.user.UserIdentityRepository;
import site.billbill.apiserver.repository.user.UserRepository;
Expand All @@ -29,6 +33,7 @@ public class UserServiceImpl implements UserService {
private final UserRepository userRepository;
private final UserIdentityRepository userIdentityRepository;
private final UserBlacklistRepository userBlacklistRepository;
private final ItemsRepository itemsRepository;
private final JWTUtil jWTUtil;

@Override
Expand Down Expand Up @@ -94,8 +99,34 @@ public void withdraw() {

Optional<UserJpaEntity> user = userRepository.findById(userId);

if(user.isEmpty()) throw new CustomException(ErrorCode.NotFound, "존재하지 않는 회원입니다.", HttpStatus.NOT_FOUND);
if (user.isEmpty()) throw new CustomException(ErrorCode.NotFound, "존재하지 않는 회원입니다.", HttpStatus.NOT_FOUND);

userRepository.withdrawUserById(userId);
}

@Override
public List<PostHistoryResponse> getPostHistory(Pageable pageable) {
String userId = MDC.get(JWTUtil.MDC_USER_ID);

return itemsRepository.getPostHistory(userId, pageable);
}

@Override
public List<BorrowHistoryResponse> getPostHistory(Pageable pageable, ItemHistoryType type) {
String userId = MDC.get(JWTUtil.MDC_USER_ID);

List<BorrowHistoryResponse> borrowHistoryList;

// switch (type) {
// case BORROWED:
// borrowHistoryList =
// break;
// case BORROWING:
// break;
// case EXCHANGE:
// break;
// }

return itemsRepository.getBorrowHistory(userId,pageable,type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
@RequiredArgsConstructor
@AllArgsConstructor
public class PortOneUtil {
@Value("${portone.api-secret}")
private String apiSecret;
// @Value("${portone.api-secret}")
// private String apiSecret;
private WebClient webClient = WebClient.builder().baseUrl("https://api.portone.io").build();

public boolean getIdentityVerification(IdentityVerificationRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public JWTUtil(

public JwtDto generateJwtDto(String userId, UserRole role) {
Date now = new Date();
Date accessTokenExpiresIn = new Date(now.getTime() + ACCESS_TOKEN_EXPIRE_TIME*1000);
Date refreshTokenExpiresIn = new Date(now.getTime() + REFRESH_TOKEN_EXPIRE_TIME*1000);
Date accessTokenExpiresIn = new Date(now.getTime() + ACCESS_TOKEN_EXPIRE_TIME * 1000);
Date refreshTokenExpiresIn = new Date(now.getTime() + REFRESH_TOKEN_EXPIRE_TIME * 1000);

String issuer = "BillBillServer";

Expand All @@ -69,12 +69,18 @@ public JwtDto generateJwtDto(String userId, UserRole role) {
.claim("type", "RT")
.compact();

return new JwtDto(accessToken, refreshToken, "Bearer", accessTokenExpiresIn.getTime() / 1000, role.name());
return JwtDto.builder()
.accessToken(accessToken)
.refreshToken(refreshToken)
.grantType("Bearer")
.expiresIn(accessTokenExpiresIn.getTime() / 1000)
.role(role.name())
.build();
}

public boolean isValidAccessToken(String token) {
try {
if(getClaims(token).get("type").equals("AT")) return true;
if (getClaims(token).get("type").equals("AT")) return true;
} catch (ExpiredJwtException e) {
log.error("Bill Access 토큰 시간이 만료 되었습니다. {}", e.getMessage());
return false;
Expand All @@ -95,7 +101,7 @@ public Claims getClaims(String token) {
}

public String putUserMDC(Claims claims) {
String userId= claims.getSubject();
String userId = claims.getSubject();
String role = claims.get("role", String.class);

MDC.put(MDC_USER_ID, userId);
Expand All @@ -119,13 +125,13 @@ public boolean isExpired(String token) {

public UserRole getUserRole(String token) {
Claims claims = getClaims(token);
String role = claims.get("role", String.class);
return UserRole.valueOf(role);
String role = claims.get("role", String.class);
return UserRole.valueOf(role);
}

public boolean isValidRefreshToken(String token) {
try {
if(getClaims(token).get("type").equals("RT")) return true;
if (getClaims(token).get("type").equals("RT")) return true;
} catch (ExpiredJwtException e) {
log.error("Bill Refresh 토큰 시간이 만료 되었습니다. {}", e.getMessage());
throw new CustomException(ErrorCode.BadRequest, "Bill Refresh 토큰 시간이 만료되었습니다. ", HttpStatus.BAD_REQUEST);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package site.billbill.apiserver.common.utils.posts;

public enum ItemHistoryType {
BORROWING,
BORROWED,
EXCHANGE
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package site.billbill.apiserver.common.utils.posts;

public enum ItemType {
BORROW,
EXCHANGE
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package site.billbill.apiserver.model.chat;


import jakarta.persistence.*;
import lombok.*;
import site.billbill.apiserver.common.converter.BooleanConverter;
import site.billbill.apiserver.model.BaseTime;
import site.billbill.apiserver.model.post.ItemsJpaEntity;
import site.billbill.apiserver.model.user.UserJpaEntity;

@Entity @Builder
@Table(name = "chat_channel")
@Getter @Setter
@NoArgsConstructor
@AllArgsConstructor
public class ChatChannelJpaEntity extends BaseTime {
@Id
@Column(name = "channel_id", nullable = false)
private String channelId;
@ManyToOne
@JoinColumn(name="item_id")
private ItemsJpaEntity item;
@ManyToOne
@JoinColumn(name="owner_id")
private UserJpaEntity owner;
@ManyToOne
@JoinColumn(name="contact_id")
private UserJpaEntity contact;
@Column(name = "del_yn", nullable = false)
@Convert(converter = BooleanConverter.class)
private boolean delYn = false;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package site.billbill.apiserver.model.post;

import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;
import site.billbill.apiserver.common.converter.BooleanConverter;
import site.billbill.apiserver.model.BaseTime;
import site.billbill.apiserver.model.user.UserJpaEntity;

import java.time.OffsetDateTime;

@Entity
@Builder
@Table(name = "borrow_hist")
@Getter @Setter
@NoArgsConstructor
@AllArgsConstructor
public class BorrowHistJapEntity extends BaseTime {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "borrow_seq", nullable = false)
private long borrowSeq;
@ManyToOne
@JoinColumn(name = "item_id", nullable = false)
private ItemsJpaEntity item;
@ManyToOne
@JoinColumn(name = "borrower_id", nullable = false)
private UserJpaEntity borrower;
@Column(name = "started_at", nullable = false)
private OffsetDateTime startedAt;
@Column(name = "ended_at", nullable = false)
private OffsetDateTime endedAt;
@Convert(converter = BooleanConverter.class)
@Column(name = "use_yn", nullable = false)
private boolean useYn = true;
@Convert(converter = BooleanConverter.class)
@Column(name = "del_yn", nullable = false)
private boolean delYn = false;
}
Loading
Loading