-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
12 changed files
with
141 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package gift.controller; | ||
|
||
import gift.dto.point.PointRequest; | ||
import gift.dto.point.PointResponse; | ||
import gift.service.PointService; | ||
import jakarta.validation.Valid; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestAttribute; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.net.URI; | ||
|
||
@RestController | ||
@RequestMapping("/api/points") | ||
public class PointController { | ||
|
||
private final PointService pointService; | ||
|
||
public PointController(PointService pointService) { | ||
this.pointService = pointService; | ||
} | ||
|
||
@PostMapping | ||
public ResponseEntity<PointResponse> addPoint(@RequestAttribute("memberId") Long memberId, @Valid @RequestBody PointRequest pointRequest) { | ||
var point = pointService.addPoint(memberId, pointRequest.point()); | ||
return ResponseEntity.created(URI.create("/api/points")).body(point); | ||
} | ||
|
||
@GetMapping | ||
public ResponseEntity<PointResponse> getPoint(@RequestAttribute("memberId") Long memberId) { | ||
var point = pointService.getPoint(memberId); | ||
return ResponseEntity.ok(point); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package gift.dto.point; | ||
|
||
import jakarta.validation.constraints.Positive; | ||
|
||
public record PointRequest( | ||
@Positive(message = "포인트는 최소 1원 이상이어야 추가할 수 있습니다.") | ||
Integer point | ||
) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package gift.dto.point; | ||
|
||
public record PointResponse( | ||
Integer point | ||
) { | ||
public static PointResponse of(Integer point) { | ||
return new PointResponse(point); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package gift.exception; | ||
|
||
public class GiftOrderException extends RuntimeException { | ||
public GiftOrderException(String message) { | ||
super(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package gift.service; | ||
|
||
import gift.dto.point.PointResponse; | ||
import gift.exception.NotFoundElementException; | ||
import gift.repository.MemberRepository; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@Transactional | ||
public class PointService { | ||
|
||
private final MemberRepository memberRepository; | ||
|
||
public PointService(MemberRepository memberRepository) { | ||
this.memberRepository = memberRepository; | ||
} | ||
|
||
public PointResponse addPoint(Long memberId, Integer point) { | ||
var member = memberRepository.findById(memberId) | ||
.orElseThrow(() -> new NotFoundElementException(memberId + "를 가진 이용자가 존재하지 않습니다.")); | ||
member.addPoint(point); | ||
memberRepository.save(member); | ||
return PointResponse.of(member.getPoint()); | ||
} | ||
|
||
public void subtractPoint(Long memberId, Integer point) { | ||
var member = memberRepository.findById(memberId) | ||
.orElseThrow(() -> new NotFoundElementException(memberId + "를 가진 이용자가 존재하지 않습니다.")); | ||
member.subtractPoint(point); | ||
memberRepository.save(member); | ||
} | ||
|
||
public PointResponse getPoint(Long memberId) { | ||
var member = memberRepository.findById(memberId) | ||
.orElseThrow(() -> new NotFoundElementException(memberId + "를 가진 이용자가 존재하지 않습니다.")); | ||
return PointResponse.of(member.getPoint()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
insert into member(email, password, deleted) | ||
values ('[email protected]', 'password', 0); | ||
insert into member(email, password, deleted) | ||
values ('[email protected]', 'password', 0); | ||
insert into member(email, password, point, deleted) | ||
values ('[email protected]', 'password', 0, 0); | ||
insert into member(email, password, point, deleted) | ||
values ('[email protected]', 'password', 0, 0); | ||
|
||
insert into category(name, description, color, image_url, deleted) | ||
values ('디지털/가전', '가전설명', '#888888', '가전이미지', 0); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters