Skip to content

Commit

Permalink
Merge pull request #23 from 2024-pre-onboarding-backend-F/feat/store
Browse files Browse the repository at this point in the history
[merge] ๋ง›์ง‘ ์กฐํšŒ API ๊ฐœ๋ฐœ
  • Loading branch information
rhaehf authored Sep 3, 2024
2 parents 4726483 + 84ef339 commit 0f4d673
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@
import wanted.ribbon.store.dto.CreateReviewRequestDto;
import wanted.ribbon.store.dto.CreateReviewResponseDto;
import wanted.ribbon.store.service.ReviewService;
import wanted.ribbon.user.config.TokenProvider;

import java.util.UUID;

@RestController
@RequestMapping("/api/reviews")
@RequiredArgsConstructor
public class ReviewController {
private final ReviewService reviewService;
private final TokenProvider tokenProvider;

@PostMapping("/{storeId}")
public ResponseEntity<CreateReviewResponseDto> createReview(@PathVariable Long storeId, @RequestBody CreateReviewRequestDto requestDto) {
CreateReviewResponseDto responseDto = reviewService.createReview(storeId, requestDto);
public ResponseEntity<CreateReviewResponseDto> createReview(@PathVariable Long storeId, @RequestHeader(value = "Authorization")String token, @RequestBody CreateReviewRequestDto requestDto) {
String accessToken = token.split("Bearer ")[1];
String id = tokenProvider.getId(accessToken); // ์•ก์„ธ์Šคํ† ํฐ์œผ๋กœ id ๋ฐ˜ํ™˜
CreateReviewResponseDto responseDto = reviewService.createReview(storeId, id, requestDto);
return ResponseEntity.status(HttpStatus.CREATED).body(responseDto);
}
}
24 changes: 7 additions & 17 deletions src/main/java/wanted/ribbon/store/controller/StoreController.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,26 @@
package wanted.ribbon.store.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import wanted.ribbon.store.dto.StoreResponseDto;
import org.springframework.web.bind.annotation.*;
import wanted.ribbon.store.dto.StoreDetailResponseDto;
import wanted.ribbon.store.dto.StoreListResponseDto;
import wanted.ribbon.store.service.StoreService;

import java.util.List;

@RestController
@RequestMapping("/api/stores")
@RequiredArgsConstructor
public class StoreController {
private final StoreService storeService;

@GetMapping
public ResponseEntity<List<StoreResponseDto>> getStoreList(@RequestParam(value = "lat") double lat,
@RequestParam(value = "lon") double lon,
@RequestParam(value = "range") double range,
@RequestParam(value = "orderBy", defaultValue = "distance", required = false) String orderBy) {

List<StoreResponseDto> storeList = storeService.findStores(lat, lon, range, orderBy);

public ResponseEntity<StoreListResponseDto> getStoreList(@RequestParam(value = "lat") double lat,
@RequestParam(value = "lon") double lon,
@RequestParam(value = "range") double range,
@RequestParam(value = "orderBy", defaultValue = "distance", required = false) String orderBy) {
StoreListResponseDto storeList = storeService.findStores(lat, lon, range, orderBy);
if (storeList.isEmpty())
return ResponseEntity.noContent().build();

return ResponseEntity.ok().body(storeList);
}

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/wanted/ribbon/store/domain/Review.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import jakarta.persistence.*;
import lombok.*;
import wanted.ribbon.user.domain.User;

@Entity
@Getter
Expand All @@ -24,4 +25,8 @@ public class Review {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "store_id", nullable = false)
private Store store;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
private User user;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package wanted.ribbon.store.dto;

public record CreateReviewResponseDto(int score, String content) {
public record CreateReviewResponseDto(String id, int score, String content) {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package wanted.ribbon.store.dto;

public record ReviewListResponseDto(int score, String content) {
public record ReviewListResponseDto(String id, int score, String content) {
}
24 changes: 24 additions & 0 deletions src/main/java/wanted/ribbon/store/dto/StoreListResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package wanted.ribbon.store.dto;


import com.fasterxml.jackson.annotation.JsonIgnore;
import wanted.ribbon.store.domain.Store;

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

public record StoreListResponseDto(int totalCount, List<StoreResponseDto> stores) {

public static StoreListResponseDto fromStoreList(List<Store> storeList) {
List<StoreResponseDto> storeResponseDto = storeList.stream()
.map(StoreResponseDto::from)
.collect(Collectors.toList());
return new StoreListResponseDto(storeList.size(), storeResponseDto);
}

// record ํƒ€์ž…์— ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ–ˆ์„ ๋•Œ Jackson ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ record์˜ ๋ชจ๋“  ๋ฉ”์„œ๋“œ๋ฅผ ์ง๋ ฌํ™” ๋Œ€์ƒ ํ•„๋“œ๋กœ ๊ฐ„์ฃผํ•˜๊ธฐ ๋•Œ๋ฌธ์—
@JsonIgnore // ์ง๋ ฌํ™” ๊ณผ์ ฑ์—์„œ ๋ฌด์‹œํ•˜๊ธฐ์œ„ํ•ด ์‚ฌ์šฉํ•จ
public boolean isEmpty() {
return stores.isEmpty();
}
}
13 changes: 11 additions & 2 deletions src/main/java/wanted/ribbon/store/service/ReviewService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,38 @@
import wanted.ribbon.store.dto.CreateReviewResponseDto;
import wanted.ribbon.store.repository.ReviewRepository;
import wanted.ribbon.store.repository.StoreRepository;
import wanted.ribbon.user.domain.User;
import wanted.ribbon.user.repository.UserRepository;

import java.util.List;
import java.util.UUID;

@Service
@RequiredArgsConstructor
public class ReviewService {
private final ReviewRepository reviewRepository;
private final StoreRepository storeRepository;
private final UserRepository userRepository;

@Transactional
public CreateReviewResponseDto createReview(Long storeId, CreateReviewRequestDto requestDto) {
public CreateReviewResponseDto createReview(Long storeId, String id, CreateReviewRequestDto requestDto) {
Store store = storeRepository.findByStoreId(storeId)
.orElseThrow(() -> new NotFoundException(ErrorCode.STORE_NOT_FOUND));
User user = userRepository.findById(id)
.orElseThrow(() -> new NotFoundException(ErrorCode.ENTITY_NOT_FOUND));

Review review = Review.builder()
.score(requestDto.score())
.content(requestDto.content())
.store(store)
.user(user)
.build();
reviewRepository.save(review);

// ์ ์ˆ˜ ์ž…๋ ฅ๋˜๋ฉด ํ•ด๋‹น ๋ง›์ง‘์˜ ํ‰์  update
updateRating(store);

CreateReviewResponseDto responseDto = new CreateReviewResponseDto(requestDto.score(), requestDto.content());
CreateReviewResponseDto responseDto = new CreateReviewResponseDto(id, requestDto.score(), requestDto.content());
return responseDto;
}

Expand Down
9 changes: 4 additions & 5 deletions src/main/java/wanted/ribbon/store/service/StoreService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import wanted.ribbon.store.domain.Store;
import wanted.ribbon.store.dto.ReviewListResponseDto;
import wanted.ribbon.store.dto.StoreDetailResponseDto;
import wanted.ribbon.store.dto.StoreResponseDto;
import wanted.ribbon.store.dto.StoreListResponseDto;
import wanted.ribbon.store.repository.ReviewRepository;
import wanted.ribbon.store.repository.StoreRepository;

Expand All @@ -26,7 +26,7 @@ public StoreDetailResponseDto getStoreDetail(Long storeId) {
.orElseThrow(() -> new NotFoundException(ErrorCode.STORE_NOT_FOUND));
List<Review> reviewList = reviewRepository.findByStore_StoreId(storeId);
List<ReviewListResponseDto> reviewListResponseDto = reviewList.stream()
.map(list -> new ReviewListResponseDto(list.getScore(), list.getContent()))
.map(list -> new ReviewListResponseDto(list.getUser().getId(), list.getScore(), list.getContent()))
.collect(Collectors.toList());

StoreDetailResponseDto responseDto = new StoreDetailResponseDto(
Expand All @@ -51,7 +51,7 @@ public StoreDetailResponseDto getStoreDetail(Long storeId) {
* @param range ์œ„๋„, ๊ฒฝ๋„๋กœ ์ง€์ •ํ•œ ์œ„์น˜ ์ฃผ๋ณ€์˜ ๊ฒ€์ƒ‰ํ•  ๋ฒ”์œ„ ์„ค์ • ๊ฐ’ (๋‹จ์œ„๋Š” km์ด๋ฉฐ, range 1.0์€ 1km์ด๋‹ค.)
* @param orderBy store ๋ฐ์ดํ„ฐ ์ •๋ ฌ ๊ธฐ์ค€ (๊ฑฐ๋ฆฌ์ˆœ๊ณผ ํ‰์ ์ˆœ 2๊ฐ€์ง€)
*/
public List<StoreResponseDto> findStores(double lat, double lon, double range, String orderBy) {
public StoreListResponseDto findStores(double lat, double lon, double range, String orderBy) {
// ์œ„๋„, ๊ฒฝ๋„์˜ ๊ณ„์‚ฐ์„ ์œ„ํ•ด km๋ฅผ m๋กœ ๋ณ€ํ™˜
double meterRange = range * 1000;
// bbox๋ฅผ ๊ตฌํ•˜๋Š” 4 ๋ชจ์„œ๋ฆฌ ์ขŒํ‘œ ๊ณ„์‚ฐ์— ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ˜์œผ๋กœ ๋‚˜๋ˆ”
Expand All @@ -60,7 +60,6 @@ public List<StoreResponseDto> findStores(double lat, double lon, double range, S
double meterToDegree = moveRange * 0.01 / 1100; // 0.01 : 1100 = meterToDegree : moveRange(๋ช‡ m)

List<Store> storeList = storeRepository.findAllStores(lat, lon, meterToDegree, meterRange, orderBy);
List<StoreResponseDto> storeListDto = storeList.stream().map(StoreResponseDto::from).collect(Collectors.toList());
return storeListDto;
return StoreListResponseDto.fromStoreList(storeList);
}
}
6 changes: 3 additions & 3 deletions src/main/java/wanted/ribbon/user/config/TokenProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ public Authentication getAuthentication(String token){
return new UsernamePasswordAuthenticationToken(new org.springframework.security.core.userdetails.User(claims.getSubject(), "", authorities), token, authorities);
}

// ํ† ํฐ ๊ธฐ๋ฐ˜์œผ๋กœ ์œ ์ € ID๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฉ”์„œ๋“œ
public UUID getUserId(String token){
// ํ† ํฐ ๊ธฐ๋ฐ˜์œผ๋กœ ํšŒ์›์˜ ID๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฉ”์„œ๋“œ
public String getId(String token){
Claims claims = getClaims(token);
return claims.get("userId", UUID.class);
return claims.getSubject();
}

private Claims getClaims(String token){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.httpBasic(AbstractHttpConfigurer::disable)
.csrf(AbstractHttpConfigurer::disable)
.authorizeRequests(auth -> auth
.requestMatchers("/api/user/login", "/api/user/signup").permitAll()
.requestMatchers("/api/users/login", "/api/users/signup").permitAll()
.anyRequest().authenticated())
.formLogin(AbstractHttpConfigurer::disable)
.addFilterBefore(new TokenAuthenticationFilter(tokenProvider), UsernamePasswordAuthenticationFilter.class);
Expand Down

0 comments on commit 0f4d673

Please sign in to comment.