Skip to content

Commit

Permalink
[fix] MVC 컨트롤러에서 byte[] 아닌 String으로 받도록 수정 (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
jinkonu committed Jul 15, 2024
1 parent 5526edf commit 1116297
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 18 deletions.
17 changes: 13 additions & 4 deletions src/main/java/org/recordy/server/keyword/domain/Keyword.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;

public enum Keyword {
Expand All @@ -20,12 +21,20 @@ public enum Keyword {
트렌디한,
;

public byte[] encode() {
return name().getBytes(StandardCharsets.UTF_8);
public static String encode() {
return Base64.getEncoder().encodeToString(
Arrays.stream(Keyword.values())
.map(Enum::name)
.reduce((a, b) -> a + "," + b)
.orElseThrow()
.getBytes(StandardCharsets.UTF_8)
);
}

public static List<Keyword> decode(byte[] utf8Bytes) {
String[] keywords = new String(utf8Bytes, StandardCharsets.UTF_8).split(",");
public static List<Keyword> decode(String utf8Bytes) {
String[] keywords = new String(Base64.getDecoder().decode(utf8Bytes), StandardCharsets.UTF_8).split(",");

System.out.println(Arrays.toString(keywords));

return Arrays.stream(keywords)
.map(Keyword::valueOf)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@ public class KeywordEntity extends JpaMetaInfoEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String name;

@Column(unique = true, nullable = false)
@Enumerated(EnumType.STRING)
private Keyword keyword;

public KeywordEntity(Keyword keyword) {
this.keyword = keyword;
this.name = keyword.name();
}

public static KeywordEntity from(Keyword keyword) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@
import org.recordy.server.auth.security.UserId;
import org.recordy.server.common.message.ErrorMessage;
import org.recordy.server.record.controller.dto.request.RecordCreateRequest;
import org.recordy.server.record.domain.File;
import org.recordy.server.record.domain.Record;
import org.springframework.data.domain.Slice;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Tag(name = "기록 관련 API")
public interface RecordApi {

Expand Down Expand Up @@ -245,7 +242,7 @@ public ResponseEntity<Void> watch(
}
)
public ResponseEntity<Slice<Record>> getFamousRecords(
@RequestParam(required = false) byte[] keywords,
@RequestParam(required = false) String keywords,
@RequestParam(required = false, defaultValue = "0") int pageNumber,
@RequestParam(required = false, defaultValue = "10") int pageSize
) ;
Expand Down Expand Up @@ -287,7 +284,7 @@ public ResponseEntity<Slice<Record>> getFamousRecords(
}
)
public ResponseEntity<Slice<Record>> getRecentRecords(
@RequestParam(required = false) byte[] keywords,
@RequestParam(required = false) String keywords,
@RequestParam(required = false, defaultValue = "0") Long cursorId,
@RequestParam(required = false, defaultValue = "10") int size
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public ResponseEntity<Record> createRecord(
@RequestPart MultipartFile thumbnail,
@RequestPart MultipartFile video
) {
RecordCreate recordCreate = RecordCreate.from(uploaderId, request);
RecordCreate recordCreate = RecordCreate.of(uploaderId, request);
Record record = recordService.create(recordCreate, File.of(video, thumbnail));

return ResponseEntity
Expand All @@ -56,7 +56,7 @@ public ResponseEntity<Void> deleteRecord(
@Override
@GetMapping("/recent")
public ResponseEntity<Slice<Record>> getRecentRecords(
@RequestParam(required = false) byte[] keywords,
@RequestParam(required = false) String keywords,
@RequestParam(required = false, defaultValue = "0") Long cursorId,
@RequestParam(required = false, defaultValue = "10") int size
) {
Expand All @@ -70,7 +70,7 @@ public ResponseEntity<Slice<Record>> getRecentRecords(
@Override
@GetMapping("/famous")
public ResponseEntity<Slice<Record>> getFamousRecords(
@RequestParam(required = false) byte[] keywords,
@RequestParam(required = false) String keywords,
@RequestParam(required = false, defaultValue = "0") int pageNumber,
@RequestParam(required = false, defaultValue = "10") int pageSize
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
public record RecordCreateRequest(
String location,
String content,
byte[] keywords) {
String keywords) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ public record RecordCreate(
List<Keyword> keywords
) {

public static RecordCreate from(Long uploaderId, RecordCreateRequest recordCreateRequest) {
public static RecordCreate of(Long uploaderId, RecordCreateRequest recordCreateRequest) {
return new RecordCreate(
uploaderId,
recordCreateRequest.location(),
recordCreateRequest.content(),
Keyword.decode(recordCreateRequest.keywords()));
Keyword.decode(recordCreateRequest.keywords())
);
}
}
39 changes: 39 additions & 0 deletions src/test/java/org/recordy/server/keyword/domain/KeywordTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.recordy.server.keyword.domain;

import org.junit.jupiter.api.Test;

import java.util.Base64;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;

class KeywordTest {

@Test
void encode_통해_모든_키워드를_utf8_인코딩할__있다() {
// given, when
String encodedKeywords = Keyword.encode();
List<Keyword> keywords = Keyword.decode(encodedKeywords);

// then
assertAll(
() -> assertThat(keywords.size()).isEqualTo(Keyword.values().length)
);
}

@Test
void decode_통해_콤마로_연결된__인코딩된_문자열_utf8_코드를_해독하여_키워드_리스트로_디코딩할__있다() {
// given
String code = Base64.getEncoder().encodeToString("감각적인,강렬한".getBytes());

// when
List<Keyword> keywords = Keyword.decode(code);

// then
assertAll(
() -> assertThat(keywords.get(0)).isEqualTo(Keyword.감각적인),
() -> assertThat(keywords.get(1)).isEqualTo(Keyword.강렬한)
);
}
}

0 comments on commit 1116297

Please sign in to comment.