Skip to content

Commit

Permalink
Merge pull request #65 from AR-TTUBEOG/feature/18
Browse files Browse the repository at this point in the history
[Feat] Road Domain(산책로) 구현
  • Loading branch information
sanggae4133 authored Feb 16, 2024
2 parents 881a0a3 + bc02cce commit 17318fc
Show file tree
Hide file tree
Showing 22 changed files with 481 additions and 236 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@
import com.ttubeog.domain.store.exception.InvalidStoreIdException;
import com.ttubeog.global.payload.ApiResponse;
import com.ttubeog.global.payload.Message;
import feign.Response;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.jdbc.Null;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -130,23 +129,25 @@ public ResponseEntity<?> createGuestBook(HttpServletRequest request, CreateGuest

guestBookRepository.save(guestBook);


CreateImageRequestDto createImageRequestDto = CreateImageRequestDto.builder()
.image(createGuestBookRequestDto.getImage())
.imageType(ImageType.GUESTBOOK)
.placeId(guestBook.getId())
.build();
imageService.createImage(createImageRequestDto);


if (createGuestBookRequestDto.getGuestBookType().equals(GuestBookType.SPOT)) {
spot = spotRepository.findById(createGuestBookRequestDto.getSpotId()).orElseThrow(InvalidSpotIdException::new);

Float originStars = guestBookRepository.sumStarBySpotId(spot.getId());

Long guestBookNum = guestBookRepository.countAllBySpot_Id(spot.getId());
Long guestBookNum = guestBookRepository.countAllBySpot(spot);

float updateStarValue;

updateStarValue = ((originStars * guestBookNum) + createGuestBookRequestDto.getStar()) / (guestBookNum + 1);
updateStarValue = ((originStars + (float)createGuestBookRequestDto.getStar()) / (float)(guestBookNum + 1));

spot.updateStars(updateStarValue);

Expand All @@ -156,11 +157,11 @@ public ResponseEntity<?> createGuestBook(HttpServletRequest request, CreateGuest

Float originStars = guestBookRepository.sumStarByStoreId(store.getId());

Long guestBookNum = guestBookRepository.countAllByStore_Id(store.getId());
Long guestBookNum = guestBookRepository.countAllByStore(store);

float updateStarValue;

updateStarValue = ((originStars * guestBookNum) + createGuestBookRequestDto.getStar()) / (guestBookNum + 1);
updateStarValue = ((originStars + (float)createGuestBookRequestDto.getStar()) / (float)(guestBookNum + 1));

store.updateStars(updateStarValue);

Expand All @@ -184,17 +185,19 @@ public ResponseEntity<?> findByGuestBookId(HttpServletRequest request, Long gues
return getResponseEntity(guestBook);
}

// Spot ID 로 GuestBook 을 조회하는 Method 입니다. 현재 따로 사용하지 않습니다.
public ResponseEntity<?> findGuestBookBySpotId(HttpServletRequest request, Long spotId) {
// Spot ID 로 GuestBook 을 조회하는 Method 입니다.
public ResponseEntity<?> findGuestBookBySpotId(HttpServletRequest request, Long spotId, Integer pageNum) {
Long memberId = jwtTokenProvider.getMemberId(request);

memberRepository.findById(memberId).orElseThrow(InvalidMemberException::new);

List<GuestBook> guestBookList = guestBookRepository.findAllBySpot_Id(spotId);
Spot spot = spotRepository.findById(spotId).orElseThrow(InvalidSpotIdException::new);

Page<GuestBook> guestBookPage = guestBookRepository.findAllBySpot(spot, PageRequest.of(pageNum, 10));

List<GuestBookResponseDto> guestBookResponseDtoList = null;

for (GuestBook guestBook : guestBookList) {
for (GuestBook guestBook : guestBookPage) {
GuestBookResponseDto guestBookResponseDto = GuestBookResponseDto.builder()
.id(guestBook.getId())
.content(guestBook.getContent())
Expand All @@ -214,17 +217,19 @@ public ResponseEntity<?> findGuestBookBySpotId(HttpServletRequest request, Long
return ResponseEntity.ok(apiResponse);
}

// Store ID 로 GuestBook 을 조회하는 Method 입니다. 현재 따로 사용하지 않습니다.
public ResponseEntity<?> findGuestBookByStoreId(HttpServletRequest request, Long storeId) {
// Store ID 로 GuestBook 을 조회하는 Method 입니다.
public ResponseEntity<?> findGuestBookByStoreId(HttpServletRequest request, Long storeId, Integer pageNum) {
Long memberId = jwtTokenProvider.getMemberId(request);

memberRepository.findById(memberId).orElseThrow(InvalidMemberException::new);

List<GuestBook> guestBookList = guestBookRepository.findAllByStore_Id(storeId);
Store store = storeRepository.findById(storeId).orElseThrow(InvalidStoreIdException::new);

Page<GuestBook> guestBookPage = guestBookRepository.findAllByStore(store, PageRequest.of(pageNum, 10));

List<GuestBookResponseDto> guestBookResponseDtoList = null;

for (GuestBook guestBook : guestBookList) {
for (GuestBook guestBook : guestBookPage) {
GuestBookResponseDto guestBookResponseDto = GuestBookResponseDto.builder()
.id(guestBook.getId())
.content(guestBook.getContent())
Expand Down Expand Up @@ -279,10 +284,14 @@ public ResponseEntity<?> updateGuestBook(HttpServletRequest request, Long guestB
public ResponseEntity<?> deleteGuestBook(HttpServletRequest request, Long guestBookId) {
Long memberId = jwtTokenProvider.getMemberId(request);

memberRepository.findById(memberId).orElseThrow(InvalidMemberException::new);
Member member = memberRepository.findById(memberId).orElseThrow(InvalidMemberException::new);

GuestBook guestBook = guestBookRepository.findById(guestBookId).orElseThrow(InvalidGuestBookIdException::new);

if (!guestBook.getMember().equals(member)) {
throw new InvalidMemberException();
}

guestBookRepository.delete(guestBook);

imageService.deleteImage(imageRepository.findByGuestBookId(guestBookId).orElseThrow(InvalidImageException::new).getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,18 @@ public class GuestBook extends BaseEntity {

private Float star;

@OneToOne(mappedBy = "guestBook", cascade = CascadeType.ALL)
private Image image;

public GuestBook(Long id, Member member, GuestBookType guestBookType, Spot spot, Store store, String content, Float star, Image image) {
public GuestBook(Long id, Member member, GuestBookType guestBookType, Spot spot, Store store, String content, Float star) {
this.id = id;
this.member = member;
this.guestBookType = guestBookType;
this.spot = spot;
this.store = store;
this.content = content;
this.star = star;
this.image = image;
}

public void updateGuestBook(String content, Float star, Image image) {
this.content = content;
this.star = star;
this.image = image;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.ttubeog.domain.guestbook.domain.repository;

import com.ttubeog.domain.guestbook.domain.GuestBook;
import com.ttubeog.domain.spot.domain.Spot;
import com.ttubeog.domain.store.domain.Store;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
Expand All @@ -11,16 +15,20 @@
@Repository
public interface GuestBookRepository extends JpaRepository<GuestBook, Long> {

List<GuestBook> findAllBySpot_Id(Long spotId);
List<GuestBook> findAllBySpot(Spot spot);

Long countAllBySpot_Id(Long spotId);
Page<GuestBook> findAllBySpot(Spot spot, PageRequest pageRequest);

Long countAllBySpot(Spot spot);

@Query("SELECT SUM(g.star) FROM GuestBook g WHERE g.spot.id = :spotId")
Float sumStarBySpotId(@Param("spotId") Long spotId);

List<GuestBook> findAllByStore_Id(Long storeId);
List<GuestBook> findAllByStore(Store store);

Page<GuestBook> findAllByStore(Store store, PageRequest pageRequest);

Long countAllByStore_Id(Long storeId);
Long countAllByStore(Store store);

@Query("SELECT SUM(g.star) FROM GuestBook g WHERE g.store.id = :storeId")
Float sumStarByStoreId(@Param("storeId") Long storeId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,13 @@ public class GuestBookResponseDto {

private Float star;

private String image;


public GuestBookResponseDto(Long id, Long memberId, GuestBookType guestBookType, Long spotId, Long storeId, String content, Float star, String image) {
public GuestBookResponseDto(Long id, Long memberId, GuestBookType guestBookType, Long spotId, Long storeId, String content, Float star) {
this.id = id;
this.memberId = memberId;
this.guestBookType = guestBookType;
this.spotId = spotId;
this.storeId = storeId;
this.content = content;
this.star = star;
this.image = image;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,196 @@
package com.ttubeog.domain.guestbook.presentation;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.ttubeog.domain.guestbook.application.GuestBookService;
import com.ttubeog.domain.guestbook.dto.request.CreateGuestBookRequestDto;
import com.ttubeog.domain.guestbook.dto.response.GuestBookResponseDto;
import com.ttubeog.domain.member.exception.InvalidMemberException;
import com.ttubeog.global.config.security.token.CurrentUser;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.RestController;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@Tag(name = "GuestBook", description = "GuestBook API(방명록 API)")
@RequiredArgsConstructor
@RestController
@RequestMapping("api/v1/guestbook")
public class GuestBookController {

private final GuestBookService guestBookService;

/**
* 방명록 생성 API
* @param request 유저 검증
* @param createGuestBookRequestDto 방명록 생성 DTO
* @return ResponseEntity -> GuestBookResponseDto
* @throws JsonProcessingException json processing 에러
*/
@Operation(summary = "방명록 생성 / createGuestBook",
description = "방명록 생성 API 입니다.",
responses = {@ApiResponse(
responseCode = "200",
description = "OK",
content = {
@Content(
mediaType = "application/json",
array = @ArraySchema(schema = @Schema(implementation = GuestBookResponseDto.class))
)
}
),
@ApiResponse(
responseCode = "500 - InvalidMemberException",
description = "멤버가 올바르지 않습니다.",
content = {
@Content(
mediaType = "application/json",
array = @ArraySchema(schema = @Schema(implementation = InvalidMemberException.class))
)
}
)
}
)
@PostMapping
@ResponseStatus(value = HttpStatus.CREATED)
public ResponseEntity<?> createGuestBook(
@CurrentUser HttpServletRequest request,
@RequestBody CreateGuestBookRequestDto createGuestBookRequestDto
) throws JsonProcessingException {
return guestBookService.createGuestBook(request, createGuestBookRequestDto);
}


/**
* 산책 스팟에 속한 모든 방명록 페이징 조회 API
* @param request
* @param spotId
* @param pageNum
* @return
* @throws JsonProcessingException
*/
@Operation(summary = "산책 스팟 소속 방명록 조회 API / findGuestBookBySpotId",
description = "산책 스팟에 속한 방명록을 페이징하여 조회하는 API 입니다.",
responses = {@ApiResponse(
responseCode = "200",
description = "OK",
content = {
@Content(
mediaType = "application/json",
array = @ArraySchema(schema = @Schema(implementation = GuestBookResponseDto.class))
)
}
),
@ApiResponse(
responseCode = "500 - InvalidMemberException",
description = "멤버가 올바르지 않습니다.",
content = {
@Content(
mediaType = "application/json",
array = @ArraySchema(schema = @Schema(implementation = InvalidMemberException.class))
)
}
)
}
)
@GetMapping("/{spotId}&{pageNum}")
@ResponseStatus(value = HttpStatus.OK)
public ResponseEntity<?> findGuestBookBySpotId(
@CurrentUser HttpServletRequest request,
@RequestParam(name = "spotId") Long spotId,
@RequestParam(name = "pageNum") Integer pageNum
) throws JsonProcessingException {
return guestBookService.findGuestBookBySpotId(request, spotId, pageNum);
}


/**
* 매장에 속한 모든 방명록 페이징 조회 API
* @param request
* @param storeId
* @param pageNum
* @return
* @throws JsonProcessingException
*/
@Operation(summary = "매장 소속 방명록 조회 API / findGuestBookByStoreId",
description = "매장에 속한 방명록을 페이징하여 조회하는 API 입니다.",
responses = {@ApiResponse(
responseCode = "200",
description = "OK",
content = {
@Content(
mediaType = "application/json",
array = @ArraySchema(schema = @Schema(implementation = GuestBookResponseDto.class))
)
}
),
@ApiResponse(
responseCode = "500 - InvalidMemberException",
description = "멤버가 올바르지 않습니다.",
content = {
@Content(
mediaType = "application/json",
array = @ArraySchema(schema = @Schema(implementation = InvalidMemberException.class))
)
}
)
}
)
@GetMapping("/{storeId}&{pageNum}")
@ResponseStatus(value = HttpStatus.OK)
public ResponseEntity<?> findGuestBookByStoreId(
@CurrentUser HttpServletRequest request,
@RequestParam(name = "storeId") Long storeId,
@RequestParam(name = "pageNum") Integer pageNum
) throws JsonProcessingException {
return guestBookService.findGuestBookByStoreId(request, storeId, pageNum);
}


/**
* 방명록 삭제 API
* @param request
* @param guestBookId
* @return
* @throws JsonProcessingException
*/
@Operation(summary = "방명록 삭제 API / deleteGuestBook",
description = " 방명록을 삭제하는 API 입니다.",
responses = {@ApiResponse(
responseCode = "200",
description = "OK",
content = {
@Content(
mediaType = "application/json",
array = @ArraySchema(schema = @Schema(implementation = ResponseEntity.class))
)
}
),
@ApiResponse(
responseCode = "500 - InvalidMemberException",
description = "멤버가 올바르지 않습니다.",
content = {
@Content(
mediaType = "application/json",
array = @ArraySchema(schema = @Schema(implementation = InvalidMemberException.class))
)
}
)
}
)
@DeleteMapping("/{guestBookId}")
@ResponseStatus(value = HttpStatus.OK)
public ResponseEntity<?> deleteGuestBook(
@CurrentUser HttpServletRequest request,
@RequestParam(name = "guestBookId") Long guestBookId
) throws JsonProcessingException {
return guestBookService.deleteGuestBook(request, guestBookId);
}

}
Loading

0 comments on commit 17318fc

Please sign in to comment.