Skip to content

Commit

Permalink
KNU-HAEDAL#1 feat: response dto 생성 및 그에 맞게 컨트롤러 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
momnpa333 committed Apr 5, 2024
1 parent 43d3ab6 commit 9d7daa0
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import team.haedal.gifticionfunding.dto.gifticon.request.GifticonCreateRequest;
import team.haedal.gifticionfunding.dto.gifticon.request.GifticonUpdateRequest;
import team.haedal.gifticionfunding.dto.gifticon.response.GifticonDetailResponse;
import team.haedal.gifticionfunding.dto.gifticon.response.GifticonResponse;
import team.haedal.gifticionfunding.entity.gifticon.Gifticon;
import team.haedal.gifticionfunding.entity.gifticon.GifticonCreate;
import team.haedal.gifticionfunding.service.gifticon.GifticonService;

import java.net.URI;
import java.util.List;

@Slf4j
Expand All @@ -22,33 +26,42 @@ public class GifticonController {

@GetMapping()
@ResponseStatus(HttpStatus.OK)
public List<Gifticon> getGifticons() {
return gifticonService.findGifticonAll();
public ResponseEntity<List<GifticonResponse>> getGifticons() {
log.info("상품 전체조회");
List<Gifticon> gifticons = gifticonService.findGifticonAll();
return ResponseEntity.ok(GifticonResponse.fromList(gifticons));
}

@GetMapping("/{gifticonId}")
@ResponseStatus(HttpStatus.OK)
public Gifticon getGifticon(@PathVariable("gifticonId") Long gifticonId){
log.info("gifticonId : {}", gifticonId);
return gifticonService.findGifticon(gifticonId);
public ResponseEntity<GifticonDetailResponse> getGifticon(@PathVariable("gifticonId") Long gifticonId) {
log.info("gifticonId : {} 상품조회", gifticonId);
Gifticon gifticon = gifticonService.findGifticon(gifticonId);
return ResponseEntity.ok(GifticonDetailResponse.from(gifticon));
}

@PostMapping()
@ResponseStatus(HttpStatus.CREATED)
public Long createGifticon(@RequestBody @Valid GifticonCreateRequest request) {
return gifticonService.registerGifticon(request.toCommand());
public ResponseEntity<Void> createGifticon(@RequestBody @Valid GifticonCreateRequest request) {
log.info("상품 생성 name: {},price:{}",request.getName(), request.getPrice());
final Long id = gifticonService.registerGifticon(request.toCommand());
return ResponseEntity.created(URI.create("/api/gifticons/"+id)).build();
}

@PutMapping("/{gifticonId}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public Gifticon putGifticon(@PathVariable("gifticonId") Long gifticonId, @RequestBody GifticonUpdateRequest request) {
return gifticonService.updateGifticon(gifticonId, request.toCommand());
public ResponseEntity<GifticonDetailResponse> putGifticon(@PathVariable("gifticonId") Long gifticonId, @RequestBody GifticonUpdateRequest request) {
log.info("gifticonId : {} 상품 수정", gifticonId);
Gifticon gifticon=gifticonService.updateGifticon(gifticonId, request.toCommand());
return ResponseEntity.ok(GifticonDetailResponse.from(gifticon));
}

@DeleteMapping("/{gifticonId}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deleteGifticon(@PathVariable("gifticonId") Long gifticonId) {
public ResponseEntity<Void> deleteGifticon(@PathVariable("gifticonId") Long gifticonId) {
log.info("gifticonId : {} 상품 삭제", gifticonId);
gifticonService.deleteGifticon(gifticonId);
return ResponseEntity.noContent().build();
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package team.haedal.gifticionfunding.dto.gifticon.response;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import team.haedal.gifticionfunding.entity.gifticon.Category;
import team.haedal.gifticionfunding.entity.gifticon.Gifticon;

import java.time.LocalDate;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
public class GifticonDetailResponse {
private Long id;
private Long price;
private String name;
private Category category;
private Long stock;
private String imageUrl;
private String description;
private String brand;
private LocalDate expirationPeriod;

@Builder
private GifticonDetailResponse(final Long id, final Long price, final String name, final Category category, final Long stock, final String imageUrl, final String description, final String brand, final LocalDate expirationPeriod) {
this.id = id;
this.price = price;
this.name = name;
this.category = category;
this.stock = stock;
this.imageUrl = imageUrl;
this.description = description;
this.brand = brand;
this.expirationPeriod = expirationPeriod;
}

public static GifticonDetailResponse from(final Gifticon gifticon){
return GifticonDetailResponse.builder()
.id(gifticon.getId())
.price(gifticon.getPrice())
.name(gifticon.getName())
.category(gifticon.getCategory())
.stock(gifticon.getStock())
.imageUrl(gifticon.getImageUrl())
.description(gifticon.getDescription())
.brand(gifticon.getBrand())
.expirationPeriod(gifticon.getExpirationPeriod())
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package team.haedal.gifticionfunding.dto.gifticon.response;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.cglib.core.Local;
import team.haedal.gifticionfunding.entity.gifticon.Category;
import team.haedal.gifticionfunding.entity.gifticon.Gifticon;

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

@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class GifticonResponse {
private Long id;
private Long price;
private String name;
private Category category;
private Long stock;
private String imageUrl;
private String description;
private String brand;
private LocalDate expirationPeriod;


@Builder
private GifticonResponse(final Long id, final Long price, final String name, final Category category, final Long stock, final String imageUrl, final String description, final String brand, final LocalDate expirationPeriod) {
this.id = id;
this.price = price;
this.name = name;
this.category = category;
this.stock = stock;
this.imageUrl = imageUrl;
this.description = description;
this.brand = brand;
this.expirationPeriod = expirationPeriod;
}

public static GifticonResponse from(final Gifticon gifticon){
return GifticonResponse.builder()
.id(gifticon.getId())
.price(gifticon.getPrice())
.name(gifticon.getName())
.category(gifticon.getCategory())
.stock(gifticon.getStock())
.imageUrl(gifticon.getImageUrl())
.description(gifticon.getDescription())
.brand(gifticon.getBrand())
.expirationPeriod(gifticon.getExpirationPeriod())
.build();
}

public static List<GifticonResponse> fromList(final List<Gifticon> gifticons){
return gifticons.stream()
.map(GifticonResponse::from)
.collect(Collectors.toUnmodifiableList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import team.haedal.gifticionfunding.entity.gifticon.Gifticon;
import team.haedal.gifticionfunding.entity.gifticon.GifticonCreate;
import team.haedal.gifticionfunding.entity.gifticon.GifticonUpdate;
import team.haedal.gifticionfunding.global.error.NotFoundGifticonException;
import team.haedal.gifticionfunding.global.validation.GifticonUpdateValidator;
import team.haedal.gifticionfunding.repository.gifticon.GifticonJpaRepository;

Expand Down Expand Up @@ -37,7 +38,7 @@ public List<Gifticon> findGifticonAll(){
public Gifticon findGifticon(Long gifticonId){
Gifticon findGifticon= gifticonJpaRepository.findById(gifticonId).orElse(null);
if(findGifticon == null){
throw new IllegalArgumentException("해당 상품이 존재하지 않습니다.");
throw new NotFoundGifticonException("해당 상품이 존재하지 않습니다.");
}
return findGifticon;
}
Expand All @@ -47,7 +48,7 @@ public Gifticon findGifticon(Long gifticonId){
public Gifticon updateGifticon(Long gifticonId, GifticonUpdate gifticonUpadate){
Gifticon findGifticon = gifticonJpaRepository.findById(gifticonId).orElse(null);
if(findGifticon == null){
throw new IllegalArgumentException("해당 상품이 존재하지 않습니다.");
throw new NotFoundGifticonException("해당 상품이 존재하지 않습니다.");
}
//gifticonUpdate 검증
GifticonUpdateValidator.validate(gifticonUpadate);
Expand All @@ -58,6 +59,11 @@ public Gifticon updateGifticon(Long gifticonId, GifticonUpdate gifticonUpadate){
//상품 삭제
@Transactional
public void deleteGifticon(Long gifticonId){
//상품이 있는지 확인
Gifticon findGifticon = gifticonJpaRepository.findById(gifticonId).orElse(null);
if(findGifticon == null){
throw new NotFoundGifticonException("해당 상품이 존재하지 않습니다.");
}
gifticonJpaRepository.deleteById(gifticonId);
}

Expand All @@ -68,7 +74,7 @@ public void deleteGifticon(Long gifticonId){
public void addStock(Long gifticonId, Long stock){
Gifticon findGifticon = gifticonJpaRepository.findById(gifticonId).orElse(null);
if(findGifticon == null){
throw new IllegalArgumentException("해당 상품이 존재하지 않습니다.");
throw new NotFoundGifticonException("해당 상품이 존재하지 않습니다.");
}
findGifticon.addStock(stock);
}
Expand All @@ -80,7 +86,7 @@ public void addStock(Long gifticonId, Long stock){
public void removeStock(Long gifticonId, Long stock){
Gifticon findGifticon = gifticonJpaRepository.findById(gifticonId).orElse(null);
if(findGifticon == null){
throw new IllegalArgumentException("해당 상품이 존재하지 않습니다.");
throw new NotFoundGifticonException("해당 상품이 존재하지 않습니다.");
}
findGifticon.removeStock(stock);
}
Expand Down

0 comments on commit 9d7daa0

Please sign in to comment.