Skip to content

Commit

Permalink
Merge pull request #18 from jjuuuunnii/main
Browse files Browse the repository at this point in the history
Feat: 로컬 테스트 완료
  • Loading branch information
jjuuuunnii authored Jan 10, 2024
2 parents 2e0e4bc + 76bb9aa commit 501a1d5
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping(Constants.API_PREFIX + "/weathers")
public class RegionController {

Expand Down Expand Up @@ -43,9 +45,9 @@ public ResponseDto<?> hasUserVoted(@UserId Long userId, @PathVariable Long regio

//3-2. 투표 하기
@PostMapping("/{regionId}/poll")
@Operation(summary = "투표 하기", description = "투표를 반영합니다")
public ResponseDto<?> createVote(@UserId Long userId, @PathVariable Long regionId, @RequestBody VoteRequestDto requestDto) {
voteHistoryService.createVote(regionId, requestDto, userId);
@Operation(summary = "투표 하기", description = "투표를 합니다.")
public ResponseDto<?> createVote(@UserId Long userId, @PathVariable Long regionId, @RequestBody VoteRequestDto voteRequestDto) {
voteHistoryService.createVote(regionId, voteRequestDto, userId);
return ResponseDto.ok(null);
}

Expand Down
11 changes: 5 additions & 6 deletions src/main/java/com/snowthon/snowman/domain/VoteHistory.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.snowthon.snowman.domain;

import com.snowthon.snowman.dto.request.VoteRequestDto;
import com.snowthon.snowman.dto.type.ESky;
import com.snowthon.snowman.dto.type.wear.EHeadWear;
import com.snowthon.snowman.dto.type.wear.ENeckWear;
Expand Down Expand Up @@ -43,8 +42,8 @@ public class VoteHistory {
private ETopWear topWear;

@Enumerated(EnumType.STRING)
@Column(name = "`outer`", nullable = false)
private EOuterWear outer;
@Column(name = "outer_wear", nullable = false)
private EOuterWear outerWear;

@Enumerated(EnumType.STRING)
@Column(name = "head_wear")
Expand All @@ -65,12 +64,12 @@ public class VoteHistory {
private LocalDateTime voteTime;

@Builder
public VoteHistory(User user, String location, String code, ETopWear topWear, EOuterWear outer, EHeadWear headWear, ENeckWear neckWear, ESky sky, int temperature, LocalDateTime voteTime) {
public VoteHistory(User user, String location, String code, ETopWear topWear, EOuterWear outerWear, EHeadWear headWear, ENeckWear neckWear, ESky sky, int temperature, LocalDateTime voteTime) {
this.user = user;
this.location = location;
this.code = code;
this.topWear = topWear;
this.outer = outer;
this.outerWear = outerWear;
this.headWear = headWear;
this.neckWear = neckWear;
this.sky = sky;
Expand All @@ -85,8 +84,8 @@ public static VoteHistory createFrom(User user, Region region, Branch mainBranch
.location(region.getLocation())
.code(region.getCode())
.temperature(mainBranch.getTemperature())
.outerWear(outerWear)
.topWear(topWear)
.outer(outerWear)
.headWear(headWear)
.neckWear(neckWear)
.sky(mainBranch.getSky())
Expand Down
23 changes: 10 additions & 13 deletions src/main/java/com/snowthon/snowman/dto/request/VoteRequestDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,28 @@


import com.fasterxml.jackson.annotation.JsonProperty;
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;

@Builder
public record VoteRequestDto(
public record VoteRequestDto
(

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

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

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

@NotNull(message = "모자 정보는 필수입니다.")
@JsonProperty("head_wear") @Schema(description = "모자 정보", example = "EAR_MUFFS")
EHeadWear headWear
@JsonProperty("headWear") @Schema(description = "모자 정보")
String headWear
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static ArchivingDetailDto fromEntity(VoteHistory voteHistory) {
return ArchivingDetailDto.builder()
.archiveId(voteHistory.getId())
.topWear(voteHistory.getTopWear())
.outerWear(voteHistory.getOuter())
.outerWear(voteHistory.getOuterWear())
.headWear(voteHistory.getHeadWear())
.neckWear(voteHistory.getNeckWear())
.weatherStatus(voteHistory.getSky())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public enum EHeadWear implements Wear{
this.value = value;
}

@Override
public EHeadWear fromValue(String value) {
public static EHeadWear fromValue(String value) {
for (EHeadWear headWear : EHeadWear.values()) {
if (headWear.getValue().equals(value)) {
return headWear;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public enum ENeckWear implements Wear{
this.value = value;
}

@Override
public Wear fromValue(String value) {
public static ENeckWear fromValue(String value) {
for (ENeckWear neckWear : ENeckWear.values()) {
if (neckWear.getValue().equals(value)) {
return neckWear;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public enum EOuterWear implements Wear{
this.value = value;
}

@Override
public Wear fromValue(String value) {
public static EOuterWear fromValue(String value) {
for (EOuterWear outer : EOuterWear.values()) {
if (outer.getValue().equals(value)) {
return outer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public enum ETopWear implements Wear{
this.value = value;
}

@Override
public Wear fromValue(String value) {
public static ETopWear fromValue(String value) {
for (ETopWear topWear : ETopWear.values()) {
if (topWear.getValue().equals(value)) {
return topWear;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/snowthon/snowman/dto/type/wear/Wear.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.snowthon.snowman.dto.type.wear;

public interface Wear {
Wear fromValue(String value);
// Wear fromValue(String value);
}
17 changes: 14 additions & 3 deletions src/main/java/com/snowthon/snowman/service/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,24 @@ public class AuthService {

@Transactional
public JwtTokenDto login(UserLoginDto userLoginDto) {
User user;
boolean isNewUser = false;

Optional<User> existingUser = userRepository.findBySerialId(userLoginDto.providerId());

if (existingUser.isPresent()) {
return jwtUtil.generateTokens(existingUser.get().getSerialId(), ERole.USER);
user = existingUser.get();
} else {
User newUser = userRepository.save(User.signUp(userLoginDto.providerId()));
return jwtUtil.generateTokens(newUser.getSerialId(), ERole.USER);
user = userRepository.save(User.signUp(userLoginDto.providerId()));
isNewUser = true;
}

JwtTokenDto jwtTokenDto = jwtUtil.generateTokens(user.getId(), ERole.USER);

if (isNewUser || !jwtTokenDto.refreshToken().equals(user.getRefreshToken())) {
userRepository.updateRefreshTokenAndLoginStatus(user.getId(), jwtTokenDto.refreshToken(), true);
}

return jwtTokenDto;
}
}
32 changes: 21 additions & 11 deletions src/main/java/com/snowthon/snowman/service/VoteHistoryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@

import com.snowthon.snowman.domain.*;
import com.snowthon.snowman.dto.request.VoteRequestDto;
import com.snowthon.snowman.dto.response.ArchivingDto;
import com.snowthon.snowman.dto.type.ErrorCode;
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 com.snowthon.snowman.exception.CommonException;
import com.snowthon.snowman.repository.RegionRepository;
import com.snowthon.snowman.repository.UserRegionVoteRepository;
import com.snowthon.snowman.repository.UserRepository;
import com.snowthon.snowman.repository.VoteHistoryRepository;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Slf4j
public class VoteHistoryService {

private final VoteHistoryRepository voteHistoryRepository;
Expand All @@ -27,21 +29,31 @@ public class VoteHistoryService {

//3-2. 투표 하기
@Transactional
public void createVote(Long regionId, VoteRequestDto voteRequestDto, Long userId) {
public void createVote(Long regionId, VoteRequestDto voteInfoDto, Long userId) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND_USER));

Region region = regionRepository.findById(regionId)
.orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND_REGION));

Branch mainBranch = region.getMainBranch();
mainBranch.updateVote(voteRequestDto.topWear(), voteRequestDto.outerWear(), voteRequestDto.headWear(), voteRequestDto.neckWear());

VoteHistory voteHistory = VoteHistory.createFrom(user, region, mainBranch, voteRequestDto.topWear(), voteRequestDto.outerWear(), voteRequestDto.headWear(), voteRequestDto.neckWear());
mainBranch.updateVote(
ETopWear.valueOf(voteInfoDto.topWear()),
EOuterWear.valueOf(voteInfoDto.outerWear()),
EHeadWear.valueOf(voteInfoDto.headWear()),
ENeckWear.valueOf(voteInfoDto.neckWear())
);

VoteHistory voteHistory = VoteHistory.createFrom(
user, region, mainBranch,
ETopWear.valueOf(voteInfoDto.topWear()),
EOuterWear.valueOf(voteInfoDto.outerWear()),
EHeadWear.valueOf(voteInfoDto.headWear()),
ENeckWear.valueOf(voteInfoDto.neckWear())
);

voteHistoryRepository.save(voteHistory);

//UserRegionVote 객체 생성 및 저장
userRegionVoteRepository.save(UserRegionVote.create(user, region));
}

Expand All @@ -57,6 +69,4 @@ public boolean checkUserVoting(Long userId, Long regionId) {
}




}

0 comments on commit 501a1d5

Please sign in to comment.