From cbc107e3e5c797624817a496753f34fb8cad59da Mon Sep 17 00:00:00 2001 From: hyerinhwang-sailin Date: Sat, 18 Jan 2025 10:40:12 +0900 Subject: [PATCH] =?UTF-8?q?[#70]=20feat(OldestCursor):=20=EC=9E=A5?= =?UTF-8?q?=EB=A5=B4=EC=9A=A9=20=EC=BB=A4=EC=84=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/dto/request/cursor/OldestCursor.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/main/java/com/napzak/domain/genre/api/dto/request/cursor/OldestCursor.java diff --git a/src/main/java/com/napzak/domain/genre/api/dto/request/cursor/OldestCursor.java b/src/main/java/com/napzak/domain/genre/api/dto/request/cursor/OldestCursor.java new file mode 100644 index 0000000..8f5813e --- /dev/null +++ b/src/main/java/com/napzak/domain/genre/api/dto/request/cursor/OldestCursor.java @@ -0,0 +1,45 @@ +package com.napzak.domain.genre.api.dto.request.cursor; + +import com.napzak.global.common.exception.NapzakException; +import com.napzak.global.common.exception.code.ErrorCode; + +public class OldestCursor { + private final Long id; + + public OldestCursor(Long id) { + this.id = id; + } + + public Long getId() { + return id; + } + + // O-{id} + @Override + public String toString() { + final StringBuilder stringBuilder = new StringBuilder(); + return stringBuilder + .append("O-") + .append(id) + .toString(); + } + + public static OldestCursor fromString(String cursor) { + if (!cursor.startsWith("O-")) { + throw new NapzakException(ErrorCode.INVALID_CURSOR_FORMAT); + } + String[] split = cursor.split("-"); + + if (split.length != 2) { + throw new NapzakException(ErrorCode.INVALID_CURSOR_FORMAT); + } + final long id; + + try{ + id = Long.parseLong(split[1]); + } catch (NumberFormatException e) { + throw new NapzakException(ErrorCode.INVALID_CURSOR_FORMAT); + } + return new OldestCursor(id); + } +}