forked from codesquad-members-2023/second-hand
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
248 additions
and
202 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
28 changes: 28 additions & 0 deletions
28
be/src/main/java/codesquad/secondhand/controller/DummyController.java
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,28 @@ | ||
package codesquad.secondhand.controller; | ||
|
||
import static codesquad.secondhand.exception.code.CommonResponseCode.*; | ||
|
||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import codesquad.secondhand.dto.ResponseDto; | ||
import codesquad.secondhand.service.DummyService; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/dummy") | ||
public class DummyController { | ||
|
||
private final DummyService dummyService; | ||
|
||
@GetMapping | ||
public ResponseDto<?> creatDummy() { | ||
log.info("더미호출"); | ||
dummyService.creatDummy(); | ||
return ResponseDto.of(RESPONSE_SUCCESS, null); | ||
} | ||
} |
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
66 changes: 66 additions & 0 deletions
66
be/src/main/java/codesquad/secondhand/service/DummyService.java
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,66 @@ | ||
package codesquad.secondhand.service; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Random; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
import org.springframework.mock.web.MockMultipartFile; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import com.github.javafaker.Faker; | ||
|
||
import codesquad.secondhand.dto.item.ItemDetailDto; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class DummyService { | ||
|
||
private final ItemService itemService; | ||
|
||
public void creatDummy() { | ||
Random random = new Random(); | ||
for (int i = 1; i <= 10; i++) { | ||
Long randomMemberIdx = (long) random.nextInt(5) + 1; | ||
int randomImageNumber = random.nextInt(10); | ||
String randomImageURL = "https://codesquadsecondhandteam01.s3.ap-northeast-2.amazonaws.com/dummy-image/" | ||
+ randomImageNumber + ".jpg"; | ||
|
||
Faker faker = new Faker(new Locale("ko")); | ||
|
||
List<MultipartFile> list = new ArrayList<>(); | ||
list.add(downloadImageAsMultipartFile(randomImageURL, "tempImageName")); | ||
|
||
ItemDetailDto itemDetailDto = new ItemDetailDto( | ||
faker.commerce().productName(), | ||
faker.number().randomDigitNotZero() * 1000, | ||
faker.lorem().sentence(), | ||
(long)faker.number().numberBetween(1, 6), | ||
(long)faker.number().numberBetween(1, 18), | ||
list, | ||
"판매중", | ||
randomMemberIdx); | ||
|
||
itemService.creatItem(itemDetailDto); | ||
} | ||
} | ||
|
||
private MultipartFile downloadImageAsMultipartFile(String imageUrl, String imageName) { | ||
try { | ||
URL url = new URL(imageUrl); | ||
InputStream in = url.openStream(); | ||
byte[] imageBytes = IOUtils.toByteArray(in); | ||
in.close(); | ||
|
||
return new MockMultipartFile("image", imageName, "image/jpeg", imageBytes); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Failed to download image from URL", e); | ||
} | ||
} | ||
} |
Oops, something went wrong.