Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE-FEAT] 아이템 도감 API get 구현 #366

Open
wants to merge 8 commits into
base: be/develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.pokerogue.helper.item.controller;

import com.pokerogue.helper.item.dto.ItemResponse;
import com.pokerogue.helper.item.service.ItemService;
import com.pokerogue.helper.util.dto.ApiResponse;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;


@Controller
@RequiredArgsConstructor
public class ItemController {

private final ItemService itemService;

@GetMapping("/api/v1/items")
public ApiResponse<List<ItemResponse>> itemList() {
return new ApiResponse<>("아이템 리스트 불러오기에 성공했습니다.", itemService.findAll());
}

@GetMapping("/api/v1/item/{id}")
public ApiResponse<ItemResponse> itemDetails(@PathVariable("id") String id) {
return new ApiResponse<>("아이템 정보 불러오기에 성공했습니다.", itemService.findById(id));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.pokerogue.helper.item.data;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

@Getter
@Document(collection = "item")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Item {

@Id
private String id;

@Id
private String imageId;

@Field("name")
private String name;

@Field("koName")
private String koName;

@Field("description")
private String description;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.pokerogue.helper.item.dto;

import com.pokerogue.helper.item.data.Item;

public record ItemResponse(String id, String imageId, String koName, String description) {

public static ItemResponse from(Item item) {
return new ItemResponse(item.getId(), item.getImageId(), item.getKoName(), item.getDescription());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.pokerogue.helper.item.repository;

import com.pokerogue.helper.item.data.Item;
import org.springframework.data.mongodb.repository.MongoRepository;

public interface ItemRepository extends MongoRepository<Item,String> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.pokerogue.helper.item.service;


import com.pokerogue.helper.global.exception.ErrorMessage;
import com.pokerogue.helper.global.exception.GlobalCustomException;
import com.pokerogue.helper.item.dto.ItemResponse;
import com.pokerogue.helper.item.repository.ItemRepository;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class ItemService {

private final ItemRepository itemRepository;

public List<ItemResponse> findAll() {
return itemRepository.findAll().stream()
.map(ItemResponse::from)
.toList();
}

public ItemResponse findById(String id) {
return itemRepository.findById(id)
.map(ItemResponse::from)
.orElseThrow(() -> new GlobalCustomException(ErrorMessage.ITEM_NOT_FOUND));
}
}
Loading