Skip to content

Commit f659f26

Browse files
authored
feat: Item 구매 API 추가 (#130)
1 parent d5fe3f3 commit f659f26

File tree

2 files changed

+47
-15
lines changed

2 files changed

+47
-15
lines changed

backend/src/main/java/com/back/domain/member/controller/ApiV1MemberController.java

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,21 @@ public ResponseEntity<ApiResponse<MemberDto>> me() {
190190
);
191191
}
192192

193+
@GetMapping("/me/statistic")
194+
@Operation(summary = "회원 통계 확인", description = "클리어한 미션 카운트")
195+
public ResponseEntity<ApiResponse<StatisticDto>> meStatistic() {
196+
Member actor = rq.getActorFromDb();
197+
198+
return ResponseEntity
199+
.status(HttpStatus.OK)
200+
.body(new ApiResponse<>(
201+
"200",
202+
"[Member] Success: 사용자 통계 확인 (%s)".formatted(actor.getEmail()),
203+
new StatisticDto(actor.getStatistic())
204+
)
205+
);
206+
}
207+
193208
@GetMapping("/me/items")
194209
@Operation(summary = "보유한 아이템 확인", description = "보유한 아이템 리스트 확인")
195210
public ResponseEntity<ApiResponse<ItemListDto>> myItems() {
@@ -288,53 +303,56 @@ public ResponseEntity<ApiResponse<MemberDto>> unequipTitle() {
288303
);
289304
}
290305

291-
@PutMapping("/obtain/item/{id}")
292-
@Operation(summary = "[Test] 아이템 획득", description = "아이템 획득")
293-
public ResponseEntity<ApiResponse<MemberDto>> obtainItem(
306+
@PutMapping("/buy/item/{id}")
307+
@Operation(summary = "아이템 구매", description = "아이템 획득")
308+
public ResponseEntity<ApiResponse<MemberDto>> buyItem(
294309
@PathVariable String id
295310
) {
296311
Member actor = rq.getActorFromDb();
297-
memberService.addItem(actor, Integer.parseInt(id));
312+
memberService.buyItem(actor, Integer.parseInt(id));
298313

299314
return ResponseEntity
300315
.status(HttpStatus.OK)
301316
.body(new ApiResponse<>(
302317
"200",
303-
"[Member] Success: 아이템 획득",
318+
"[Member] Success: 아이템 구매 (%s)".formatted(id),
304319
new MemberDto(actor)
305320
)
306321
);
307322
}
308323

309-
@PutMapping("/obtain/title/{id}")
310-
@Operation(summary = "[Test] 칭호 획득", description = "칭호 획득")
311-
public ResponseEntity<ApiResponse<MemberDto>> obtainTitle(
324+
@PutMapping("/obtain/item/{id}")
325+
@Operation(summary = "[Test] 아이템 획득", description = "아이템 획득")
326+
public ResponseEntity<ApiResponse<MemberDto>> obtainItem(
312327
@PathVariable String id
313328
) {
314329
Member actor = rq.getActorFromDb();
315-
memberService.addTitle(actor, Integer.parseInt(id));
330+
memberService.addItem(actor, Integer.parseInt(id));
316331

317332
return ResponseEntity
318333
.status(HttpStatus.OK)
319334
.body(new ApiResponse<>(
320335
"200",
321-
"[Member] Success: 칭호 획득",
336+
"[Member] Success: 아이템 획득",
322337
new MemberDto(actor)
323338
)
324339
);
325340
}
326341

327-
@GetMapping("/me/statistic")
328-
@Operation(summary = "회원 통계 확인", description = "클리어한 미션 카운트")
329-
public ResponseEntity<ApiResponse<StatisticDto>> meStatistic() {
342+
@PutMapping("/obtain/title/{id}")
343+
@Operation(summary = "[Test] 칭호 획득", description = "칭호 획득")
344+
public ResponseEntity<ApiResponse<MemberDto>> obtainTitle(
345+
@PathVariable String id
346+
) {
330347
Member actor = rq.getActorFromDb();
348+
memberService.addTitle(actor, Integer.parseInt(id));
331349

332350
return ResponseEntity
333351
.status(HttpStatus.OK)
334352
.body(new ApiResponse<>(
335353
"200",
336-
"[Member] Success: 사용자 통계 확인 (%s)".formatted(actor.getEmail()),
337-
new StatisticDto(actor.getStatistic())
354+
"[Member] Success: 칭호 획득",
355+
new MemberDto(actor)
338356
)
339357
);
340358
}

backend/src/main/java/com/back/domain/member/service/MemberService.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,20 @@ public void unequipTitle(Member member) {
139139
member.setTitle(null);
140140
}
141141

142+
//아이템 구매
143+
public void buyItem(Member member, int itemId) {
144+
Item item = itemRepository.findById(itemId)
145+
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND, "[Member] Fail: 존재하지 않는 아이템"));
146+
if(member.getOwnedItems().contains(item)) {
147+
throw new CustomException(ErrorCode.BAD_REQUEST, "[Member] Fail: 이미 보유중인 아이템");
148+
}
149+
if(member.getMoney() < item.getPrice()) {
150+
throw new CustomException(ErrorCode.BAD_REQUEST, "[Member] Fail: 돈 부족");
151+
}
152+
member.addItem(item);
153+
member.setMoney(member.getMoney() - item.getPrice());
154+
}
155+
142156
// *** 미션 클리어 카운트 ***
143157
public void clearDaily(Member member) {
144158
member.getStatistic().clearDaily();

0 commit comments

Comments
 (0)