Skip to content

Commit

Permalink
Merge pull request #16 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 50fff01 + 0f7b5ca commit edd9c2b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
21 changes: 14 additions & 7 deletions src/main/java/com/snowthon/snowman/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,42 @@
import com.snowthon.snowman.contrant.Constants;
import com.snowthon.snowman.dto.common.ResponseDto;
import com.snowthon.snowman.repository.VoteHistoryRepository;
import com.snowthon.snowman.service.UserService;
import com.snowthon.snowman.service.VoteHistoryService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@RestController
@AllArgsConstructor
@RequestMapping(Constants.API_PREFIX + "/users")
public class UserController {

private final VoteHistoryService voteHistoryService;
private final UserService userService;

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

//4-2. 모아 보기(상세)
@GetMapping("/vote-history/{voteHistoryId}")
@Operation(summary = "아카이빙 상세보기", description = "상세한 투표정보를 가져옵니다.")
public ResponseDto<?> archivingDetail(@PathVariable Long voteHistoryId) {

return ResponseDto.ok(voteHistoryService.getVoteHistoryById(voteHistoryId));
return ResponseDto.ok(userService.getVoteHistoryById(voteHistoryId));
}

// @PostMapping("/make-testUser")
// @Operation(summary = "테스트 유저 만들기", description = "테스트 유저를 만듭니다.")
// public ResponseDto<?> makeTestUser() {
// userService.makeTestUser();
// return ResponseDto.ok(null);
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.minidev.json.JSONValue;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
Expand All @@ -22,6 +23,7 @@

@Component
@RequiredArgsConstructor
@Slf4j
public class DefaultSuccessHandler implements AuthenticationSuccessHandler {
private final JwtUtil jwtUtil;
private final UserRepository userRepository;
Expand Down Expand Up @@ -53,6 +55,7 @@ private void setSuccessAppResponse(HttpServletResponse response, JwtTokenDto tok
);
result.put("error", null);

log.info("====user accessToken = {}", tokenDto.accessToken());
response.getWriter().write(JSONValue.toJSONString(result));
}
}
26 changes: 26 additions & 0 deletions src/main/java/com/snowthon/snowman/service/UserService.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,46 @@
package com.snowthon.snowman.service;

import com.snowthon.snowman.domain.User;
import com.snowthon.snowman.domain.VoteHistory;
import com.snowthon.snowman.dto.response.ArchivingDto;
import com.snowthon.snowman.dto.type.ErrorCode;
import com.snowthon.snowman.exception.CommonException;
import com.snowthon.snowman.repository.UserRepository;
import com.snowthon.snowman.repository.VoteHistoryRepository;
import com.snowthon.snowman.utility.JwtUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@RequiredArgsConstructor
public class UserService {

private final UserRepository userRepository;
private final VoteHistoryRepository voteHistoryRepository;
private final JwtUtil jwtUtil;

@Transactional
public User saveUser(User user) {
return userRepository.save(user);
}

//4-1. 모아 보기
public ArchivingDto getVoteHistoriesByUser(Long userId) {
List<VoteHistory> voteHistories = voteHistoryRepository.findByUserId(userId);
return ArchivingDto.fromEntity(voteHistories.stream()
.map(ArchivingDto.ArchivingDetailDto::fromEntity).collect(Collectors.toList()));
}


//4-2. 모아 보기(상세)
public ArchivingDto.ArchivingDetailDto getVoteHistoryById(Long voteHistoryId) {
return ArchivingDto.ArchivingDetailDto.fromEntity(voteHistoryRepository.findById(voteHistoryId)
.orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND_HISTORY)));
}


}
13 changes: 2 additions & 11 deletions src/main/java/com/snowthon/snowman/service/VoteHistoryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,7 @@ public boolean checkUserVoting(Long userId, Long regionId) {
return userRegionVoteRepository.existsByUserAndRegion(user, region);
}

//4-1. 모아 보기
public ArchivingDto getVoteHistoriesByUser(Long userId) {
List<VoteHistory> voteHistories = voteHistoryRepository.findByUserId(userId);
return ArchivingDto.fromEntity(voteHistories.stream()
.map(ArchivingDto.ArchivingDetailDto::fromEntity).collect(Collectors.toList()));
}

//4-2. 모아 보기(상세)
public ArchivingDto.ArchivingDetailDto getVoteHistoryById(Long voteHistoryId) {
return ArchivingDto.ArchivingDetailDto.fromEntity(voteHistoryRepository.findById(voteHistoryId)
.orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND_HISTORY)));
}


}

0 comments on commit edd9c2b

Please sign in to comment.