Skip to content

Commit

Permalink
[#70] refactor: 커서와 sort 관련 errorcode 공통처리에 따른 참조 변경 사항 반영
Browse files Browse the repository at this point in the history
  • Loading branch information
hyerinhwang-sailin committed Jan 18, 2025
1 parent 8fbebaa commit eed82ec
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.napzak.global.common.exception.code.ErrorCode;
import com.napzak.domain.product.api.dto.request.cursor.HighPriceCursor;
import com.napzak.domain.product.api.dto.request.cursor.LowPriceCursor;
import com.napzak.domain.product.api.dto.request.cursor.PopularCursor;
import com.napzak.domain.product.api.dto.request.cursor.RecentCursor;
import com.napzak.domain.product.api.dto.response.ProductBuyListResponse;
import com.napzak.domain.product.api.dto.response.ProductSellListResponse;
import com.napzak.domain.product.api.exception.ProductErrorCode;
import com.napzak.domain.product.api.exception.ProductSuccessCode;
import com.napzak.domain.product.api.service.ProductPagination;
import com.napzak.domain.product.api.service.ProductService;
Expand Down Expand Up @@ -293,7 +293,7 @@ private SortOption parseSortOption(String sortOption) {
try {
return SortOption.valueOf(sortOption.toUpperCase());
} catch (IllegalArgumentException e) {
throw new NapzakException(ProductErrorCode.INVALID_SORT_OPTION);
throw new NapzakException(ErrorCode.INVALID_SORT_OPTION);
}
}

Expand All @@ -320,10 +320,10 @@ private CursorValues parseCursorValues(String cursor, SortOption sortOption) {
HighPriceCursor priceCursor = HighPriceCursor.fromString(cursor);
return new CursorValues(priceCursor.getId(), priceCursor.getPrice());
}
default -> throw new NapzakException(ProductErrorCode.INVALID_SORT_OPTION);
default -> throw new NapzakException(ErrorCode.INVALID_SORT_OPTION);
}
} catch (IllegalArgumentException e) {
throw new NapzakException(ProductErrorCode.INVALID_CURSOR_FORMAT);
throw new NapzakException(ErrorCode.INVALID_CURSOR_FORMAT);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.napzak.domain.product.api.dto.request.cursor;

import com.napzak.domain.product.api.exception.ProductErrorCode;
import com.napzak.global.common.exception.code.ErrorCode;
import com.napzak.domain.product.api.service.enums.SortOption;
import com.napzak.global.common.exception.NapzakException;

Expand All @@ -18,7 +18,7 @@ public static Cursor fromString(String cursor, SortOption sortOption) {
case HIGH_PRICE:
return HighPriceCursor.fromString(cursor);
default:
throw new NapzakException(ProductErrorCode.INVALID_SORT_OPTION);
throw new NapzakException(ErrorCode.INVALID_SORT_OPTION);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.napzak.domain.product.api.dto.request.cursor;

import com.napzak.domain.product.api.exception.ProductErrorCode;
import com.napzak.global.common.exception.NapzakException;
import com.napzak.global.common.exception.code.ErrorCode;

public class HighPriceCursor extends Cursor{
public class HighPriceCursor extends Cursor {
private final int price;
private final long id;

public HighPriceCursor(int price, long id){
public HighPriceCursor(int price, long id) {
this.price = price;
this.id = id;
}
Expand All @@ -33,24 +33,24 @@ public String toString() {
.toString();
}

public static HighPriceCursor fromString(String cursor){
public static HighPriceCursor fromString(String cursor) {
if (!cursor.startsWith("H-")) {
throw new NapzakException(ProductErrorCode.INVALID_CURSOR_FORMAT);
throw new NapzakException(ErrorCode.INVALID_CURSOR_FORMAT);
}
String[] split = cursor.split("-");

if (split.length != 3) {
throw new NapzakException(ProductErrorCode.INVALID_CURSOR_FORMAT);
throw new NapzakException(ErrorCode.INVALID_CURSOR_FORMAT);
}

final long id;
final int price;

try{
try {
price = Integer.parseInt(split[1]);
id = Long.parseLong(split[2]);
} catch (NumberFormatException e) {
throw new NapzakException(ProductErrorCode.INVALID_CURSOR_FORMAT);
throw new NapzakException(ErrorCode.INVALID_CURSOR_FORMAT);
}
return new HighPriceCursor(price, id);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.napzak.domain.product.api.dto.request.cursor;

import com.napzak.domain.product.api.exception.ProductErrorCode;
import com.napzak.global.common.exception.NapzakException;
import com.napzak.global.common.exception.code.ErrorCode;

public class LowPriceCursor extends Cursor {
private final int price;
private final long id;

public LowPriceCursor(int price, long id){
public LowPriceCursor(int price, long id) {
this.price = price;
this.id = id;
}
Expand All @@ -33,24 +33,24 @@ public String toString() {
.toString();
}

public static LowPriceCursor fromString(String cursor){
public static LowPriceCursor fromString(String cursor) {
if (!cursor.startsWith("L-")) {
throw new NapzakException(ProductErrorCode.INVALID_CURSOR_FORMAT);
throw new NapzakException(ErrorCode.INVALID_CURSOR_FORMAT);
}
String[] split = cursor.split("-");

if (split.length != 3) {
throw new NapzakException(ProductErrorCode.INVALID_CURSOR_FORMAT);
throw new NapzakException(ErrorCode.INVALID_CURSOR_FORMAT);
}

final long id;
final int price;

try{
try {
price = Integer.parseInt(split[1]);
id = Long.parseLong(split[2]);
} catch (NumberFormatException e) {
throw new NapzakException(ProductErrorCode.INVALID_CURSOR_FORMAT);
throw new NapzakException(ErrorCode.INVALID_CURSOR_FORMAT);
}
return new LowPriceCursor(price, id);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.napzak.domain.product.api.dto.request.cursor;

import com.napzak.domain.product.api.exception.ProductErrorCode;
import com.napzak.global.common.exception.NapzakException;
import com.napzak.global.common.exception.code.ErrorCode;

public class PopularCursor extends Cursor {
private final int interestCount;
Expand Down Expand Up @@ -33,24 +33,24 @@ public String toString() {
.toString();
}

public static PopularCursor fromString(String cursor){
public static PopularCursor fromString(String cursor) {
if (!cursor.startsWith("P-")) {
throw new NapzakException(ProductErrorCode.INVALID_CURSOR_FORMAT);
throw new NapzakException(ErrorCode.INVALID_CURSOR_FORMAT);
}
String[] split = cursor.split("-");

if (split.length != 3) {
throw new NapzakException(ProductErrorCode.INVALID_CURSOR_FORMAT);
throw new NapzakException(ErrorCode.INVALID_CURSOR_FORMAT);
}

final int interestCount;
final long id;

try{
try {
interestCount = Integer.parseInt(split[1]);
id = Long.parseLong(split[2]);
} catch (NumberFormatException e) {
throw new NapzakException(ProductErrorCode.INVALID_CURSOR_FORMAT);
throw new NapzakException(ErrorCode.INVALID_CURSOR_FORMAT);
}
return new PopularCursor(interestCount, id);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.napzak.domain.product.api.dto.request.cursor;

import com.napzak.domain.product.api.exception.ProductErrorCode;
import com.napzak.global.common.exception.NapzakException;
import com.napzak.global.common.exception.code.ErrorCode;

public class RecentCursor extends Cursor {
private final Long id;
Expand All @@ -26,19 +26,19 @@ public String toString() {

public static RecentCursor fromString(String cursor) {
if (!cursor.startsWith("R-")) {
throw new NapzakException(ProductErrorCode.INVALID_CURSOR_FORMAT);
throw new NapzakException(ErrorCode.INVALID_CURSOR_FORMAT);
}
String[] split = cursor.split("-");

if (split.length != 2) {
throw new NapzakException(ProductErrorCode.INVALID_CURSOR_FORMAT);
throw new NapzakException(ErrorCode.INVALID_CURSOR_FORMAT);
}
final long id;

try{
try {
id = Long.parseLong(split[1]);
} catch (NumberFormatException e) {
throw new NapzakException(ProductErrorCode.INVALID_CURSOR_FORMAT);
throw new NapzakException(ErrorCode.INVALID_CURSOR_FORMAT);
}
return new RecentCursor(id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@
@Getter
@RequiredArgsConstructor
public enum ProductErrorCode implements BaseErrorCode {
/*
400 Bad Request
*/
INVALID_CURSOR_FORMAT(HttpStatus.BAD_REQUEST, "유효하지 않은 커서입니다."),
INVALID_SORT_OPTION(HttpStatus.BAD_REQUEST, "유효하지 않은 정렬기준입니다."),

/*
404 Not Found
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import javax.annotation.Nullable;

import com.napzak.global.common.exception.code.ErrorCode;
import com.napzak.domain.product.api.dto.request.cursor.HighPriceCursor;
import com.napzak.domain.product.api.dto.request.cursor.LowPriceCursor;
import com.napzak.domain.product.api.dto.request.cursor.PopularCursor;
Expand Down Expand Up @@ -48,7 +49,7 @@ public String generateNextCursor(SortOption sortOption) {
case HIGH_PRICE:
return new HighPriceCursor(lastProduct.getPrice(), lastProduct.getId()).toString();
default:
throw new NapzakException(ProductErrorCode.INVALID_SORT_OPTION);
throw new NapzakException(ErrorCode.INVALID_SORT_OPTION);
}
}

Expand Down

0 comments on commit eed82ec

Please sign in to comment.