Skip to content

Commit

Permalink
Merge pull request #11 from jjuuuunnii/main
Browse files Browse the repository at this point in the history
Feat: 서버 1차 구현 완료
  • Loading branch information
Gusionling authored Jan 10, 2024
2 parents 4248b6c + 12ae9c2 commit b675328
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public ResponseDto<?> hasUserVoted(@UserId Long userId, @PathVariable Long regio
@PostMapping("/{regionId}/poll")
@Operation(summary = "투표 하기", description = "투표를 반영합니다")
public ResponseDto<?> createVote(@UserId Long userId, @PathVariable Long regionId, @RequestBody VoteRequestDto requestDto) {

voteHistoryService.createVote(regionId, requestDto, userId);
return ResponseDto.ok(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ public class UserController {
private final VoteHistoryService voteHistoryService;

@GetMapping("/vote-history")
@Operation(summary = "아카이빙", description = "유저의 투표 기록들을 가져옵니다")
@Operation(summary = "아카이빙 모아보기", description = "유저의 투표 기록들을 가져옵니다")
public ResponseDto<?> archiving(@UserId Long userId) {

return ResponseDto.ok(voteHistoryService.getVoteHistoriesByUser(userId));
}

Expand Down
24 changes: 7 additions & 17 deletions src/main/java/com/snowthon/snowman/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,10 @@ public class User {
@Column(name = "id")
private Long id;

@Column(name = "nickname", nullable = false)
private String nickname;
@Column(name = "serial_id", nullable = false, unique = true)
private Long serialId;

@Column(name = "email", nullable = false, unique = true)
private String email;

@Column(name = "phone_number", nullable = false)
private String phoneNumber;

@Column(name = "refresh_token", nullable = false)
@Column(name = "refresh_token")
private String refreshToken;

@Column(name = "is_login", columnDefinition = "TINYINT(1)")
Expand All @@ -39,19 +33,15 @@ public class User {


@Builder
public User(String nickname, String email, String phoneNumber) {
this.nickname = nickname;
this.email = email;
this.phoneNumber = phoneNumber;
public User(Long serialId) {
this.serialId = serialId;
this.isLogin = true;
this.createdAt = LocalDateTime.now();
}

public static User signUp(String nickname, String email, String phoneNumber) {
public static User signUp(Long serialId) {
return User.builder()
.nickname(nickname)
.email(email)
.phoneNumber(phoneNumber)
.serialId(serialId)
.build();
}
}
2 changes: 0 additions & 2 deletions src/main/java/com/snowthon/snowman/domain/VoteHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ public class VoteHistory {
@Column(name = "temperature", nullable = false)
private int temperature;



@Column(name ="vote_time", nullable = false)
private LocalDateTime voteTime;

Expand Down
13 changes: 5 additions & 8 deletions src/main/java/com/snowthon/snowman/dto/request/UserLoginDto.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package com.snowthon.snowman.dto.request;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;

@Builder
public record UserLoginDto(

@NotNull(message = "이메일은 빈값이 될 수 없습니다.")
String email,

@NotNull(message = "전화번호는 빈값이 될 수 없습니다.")
String phoneNumber,

@NotNull(message = "닉네임은 빈 값이 될 수 없습니다.")
String nickname
@NotNull(message = "providerId는 빈값이 될 수 없습니다.")
@JsonProperty("providerId") @Schema(description = "프로바이더 아이디", example = "203912941")
Long providerId
) {
}

This file was deleted.

113 changes: 97 additions & 16 deletions src/main/java/com/snowthon/snowman/dto/response/ArchivingDto.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,110 @@
package com.snowthon.snowman.dto.response;

import com.snowthon.snowman.domain.wear.HeadWear;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import com.snowthon.snowman.domain.VoteHistory;
import com.snowthon.snowman.dto.type.ESky;
import com.snowthon.snowman.dto.type.wear.EHeadWear;
import com.snowthon.snowman.dto.type.wear.ENeckWear;
import com.snowthon.snowman.dto.type.wear.EOuterWear;
import com.snowthon.snowman.dto.type.wear.ETopWear;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;

@Builder
public record ArchivingDto(
@NotNull(message = "이름은 필수입니다.")
Long id,
@NotNull(message = "topWear은 필수입니다.")
ETopWear topWear,
@NotNull(message = "outerWear은 필수입니다.")
EOuterWear outerWear,
@NotNull(message = "headWear은 필수입니다.")
EHeadWear headWear,
@NotNull(message = "neckWear은 필수입니다.")
ENeckWear neckWear,
@NotNull(message = "voteTime은 필수입니다.")
LocalDateTime voteTime
) {
@Getter
@NoArgsConstructor(access = lombok.AccessLevel.PROTECTED)
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class ArchivingDto{

@JsonProperty("voteHistoryList") @Schema(description = "투표 내역 리스트")
private List<ArchivingDetailDto> voteHistoryList;

@Getter
@NoArgsConstructor(access = lombok.AccessLevel.PROTECTED)
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public static class ArchivingDetailDto {
@JsonProperty("archive_id") @Schema(description = "아카이브 아이디", example = "1")
@NotNull(message = "이름은 필수입니다.")
private Long archiveId;

@JsonProperty("topWear") @Schema(description = "상의 정보", example = "LONG_SLEEVE")
@NotNull(message = "topWear는 필수입니다.")
private ETopWear topWear;

@JsonProperty("outerWear") @Schema(description = "아우터 정보", example = "COAT")
@NotNull(message = "outerWear는 필수입니다.")
private EOuterWear outerWear;

@JsonProperty("headWear") @Schema(description = "모자 정보", example = "EAR_MUFFS")
@NotNull(message = "headWear는 필수입니다.")
private EHeadWear headWear;

@JsonProperty("neckWear") @Schema(description = "목도리 정보", example = "SCARF")
@NotNull(message = "neckWear는 필수입니다.")
private ENeckWear neckWear;

@JsonProperty("weatherStatus") @Schema(description = "날씨 정보", example = "SNOW")
@NotNull(message = "날씨 정보는 필수입니다.")
private ESky weatherStatus;

@JsonProperty("temperature") @Schema(description = "온도", example = "10")
@NotNull(message = "temperature는 필수입니다.")
private int temperature;

@JsonProperty("location") @Schema(description = "위치", example = "서울특별시 중구 명동")
@NotNull(message = "location는 필수입니다.")
private String location;

@JsonProperty("voteTime") @Schema(description = "투표 시간", example = "2021-10-10T10:10:10")
@NotNull(message = "voteTime는 필수입니다.")
private LocalDateTime voteTime;

@Builder
public ArchivingDetailDto(Long archiveId, ETopWear topWear, EOuterWear outerWear, EHeadWear headWear, ENeckWear neckWear, ESky weatherStatus, int temperature, String location, LocalDateTime voteTime) {
this.archiveId = archiveId;
this.topWear = topWear;
this.outerWear = outerWear;
this.headWear = headWear;
this.neckWear = neckWear;
this.weatherStatus = weatherStatus;
this.temperature = temperature;
this.location = location;
this.voteTime = voteTime;
}

public static ArchivingDetailDto fromEntity(VoteHistory voteHistory) {
return ArchivingDetailDto.builder()
.archiveId(voteHistory.getId())
.topWear(voteHistory.getTopWear())
.outerWear(voteHistory.getOuter())
.headWear(voteHistory.getHeadWear())
.neckWear(voteHistory.getNeckWear())
.weatherStatus(voteHistory.getSky())
.temperature(voteHistory.getTemperature())
.location(voteHistory.getLocation())
.voteTime(voteHistory.getVoteTime())
.build();
}
}

@Builder
public ArchivingDto(List<ArchivingDetailDto> voteHistoryList) {
this.voteHistoryList = voteHistoryList;
}

public static ArchivingDto fromEntity(List<ArchivingDetailDto> voteHistoryList) {
return ArchivingDto.builder()
.voteHistoryList(voteHistoryList)
.build();
}
}



Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,8 @@
@Repository
public interface UserRepository extends JpaRepository<User, Long> {

Optional<User> findByIdAndRefreshTokenAndIsLogin(Long id, String refreshToken, Boolean isLogin);

@Query("select u.id as id from User u where u.email = :email and u.phoneNumber = :phoneNumber")
Optional<UserSecurityForm> findByEmailAndPhoneNumber(@Param("email") String email, @Param("phoneNumber") String phoneNumber);

// @Query("select u.id as id, u.role as role from User u where u.serialId = :serialId")
// Optional<UserSecurityForm> findSecurityFormBySerialId(String serialId);

@Query("select u.id as id from User u")
Optional<UserSecurityForm> findBySerialId(Long serialId);

@Query("select u.id as id from User u where u.id = :id and u.isLogin = true")
Optional<UserSecurityForm> findSecurityFormById(Long id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
public class CustomJsonUsernamePasswordAuthenticationFilter extends AbstractAuthenticationProcessingFilter {


private static final String DEFAULT_LOGIN_PATH = "/api/auth/login";
private static final String DEFAULT_LOGIN_PATH = "/api/v1/auth/login";
private final CustomUserDetailService customUserDetailService;
private final ObjectMapper objectMapper;
private final DefaultSuccessHandler defaultSuccessHandler;
Expand All @@ -44,7 +44,7 @@ public CustomJsonUsernamePasswordAuthenticationFilter(
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException {
UserLoginDto userLoginDto = objectMapper.readValue(request.getInputStream(), UserLoginDto.class);
UserPrincipal userPrincipal = (UserPrincipal) customUserDetailService.loadUserByEmailAndPhoneNumber(userLoginDto.email(), userLoginDto.phoneNumber(), userLoginDto.nickname());
UserPrincipal userPrincipal = (UserPrincipal) customUserDetailService.loadUserBySerialId(userLoginDto.providerId());

UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(
userPrincipal, null, userPrincipal.getAuthorities());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ private void setSuccessAppResponse(HttpServletResponse response, JwtTokenDto tok
Map<String, Object> result = new HashMap<>();
result.put("success", true);
result.put("data", Map.of(
"access_token", tokenDto.accessToken(),
"refresh_token", tokenDto.refreshToken()
"accessToken", tokenDto.accessToken(),
"refreshToken", tokenDto.refreshToken()
)
);
result.put("error", null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
public class UserPrincipal implements UserDetails {
@Getter private final Long id;
@Getter private final ERole role;
private final String password;
private final Collection<? extends GrantedAuthority> authorities;

public static UserPrincipal createByUserSecurityForm(UserRepository.UserSecurityForm form) {
Expand All @@ -43,7 +42,7 @@ public String getUsername() {

@Override
public String getPassword() {
return password;
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@
public class CustomUserDetailService implements UserDetailsService {
private final UserRepository userRepository;

public UserDetails loadUserByEmailAndPhoneNumber(String email, String phoneNumber, String nickname) {
return userRepository.findByEmailAndPhoneNumber(email, phoneNumber)
public UserDetails loadUserBySerialId(Long serialId) {
return userRepository.findBySerialId(serialId)
.map(UserPrincipal::createByUserSecurityForm)
.orElseGet(() -> {
User newUser = User.signUp(nickname, email, phoneNumber);
User newUser = User.signUp(serialId);
userRepository.save(newUser);
return UserPrincipal.createByUserId(newUser.getId());
});
}


public UserDetails loadUserById(Long id) {
UserRepository.UserSecurityForm user = userRepository.findSecurityFormById(id)
.orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND_LOGIN_USER));
Expand Down
31 changes: 6 additions & 25 deletions src/main/java/com/snowthon/snowman/service/VoteHistoryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.snowthon.snowman.domain.*;
import com.snowthon.snowman.dto.request.VoteRequestDto;
import com.snowthon.snowman.dto.response.AchivingDetailDto;
import com.snowthon.snowman.dto.response.ArchivingDto;
import com.snowthon.snowman.dto.type.ErrorCode;
import com.snowthon.snowman.exception.CommonException;
Expand Down Expand Up @@ -58,33 +57,15 @@ public boolean checkUserVoting(Long userId, Long regionId) {
}

//4-1. 모아 보기
public List<ArchivingDto> getVoteHistoriesByUser(Long userId) {
public ArchivingDto getVoteHistoriesByUser(Long userId) {
List<VoteHistory> voteHistories = voteHistoryRepository.findByUserId(userId);

return voteHistories.stream()
.map(voteHistory -> new ArchivingDto(
voteHistory.getId(),
voteHistory.getTopWear(),
voteHistory.getOuter(),
voteHistory.getHeadWear(),
voteHistory.getNeckWear(),
voteHistory.getVoteTime()))
.collect(Collectors.toList());
return ArchivingDto.fromEntity(voteHistories.stream()
.map(ArchivingDto.ArchivingDetailDto::fromEntity).collect(Collectors.toList()));
}

//4-2. 모아 보기(상세)
public AchivingDetailDto getVoteHistoryById(Long voteHistoryId) {
VoteHistory voteHistory = voteHistoryRepository.findById(voteHistoryId)
.orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND_HISTORY));
return new AchivingDetailDto(
voteHistory.getId(),
voteHistory.getLocation(),
voteHistory.getTopWear(),
voteHistory.getOuter(),
voteHistory.getHeadWear(),
voteHistory.getNeckWear(),
voteHistory.getSky(),
voteHistory.getTemperature(),
voteHistory.getVoteTime());
public ArchivingDto.ArchivingDetailDto getVoteHistoryById(Long voteHistoryId) {
return ArchivingDto.ArchivingDetailDto.fromEntity(voteHistoryRepository.findById(voteHistoryId)
.orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND_HISTORY)));
}
}

0 comments on commit b675328

Please sign in to comment.