-
Notifications
You must be signed in to change notification settings - Fork 2
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
8 changed files
with
189 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.listywave.admin; | ||
|
||
import static jakarta.persistence.GenerationType.IDENTITY; | ||
import static lombok.AccessLevel.PROTECTED; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.Id; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@Getter | ||
@NoArgsConstructor(access = PROTECTED) | ||
@AllArgsConstructor | ||
public class Admin { | ||
|
||
@Id | ||
@GeneratedValue(strategy = IDENTITY) | ||
private Long id; | ||
|
||
@Column(nullable = false, length = 40, unique = true) | ||
private String ip; | ||
|
||
@Column(nullable = false, length = 50, unique = true) | ||
private String account; | ||
|
||
@Column(nullable = false, length = 50) | ||
private String password; | ||
} |
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,13 @@ | ||
package com.listywave.admin; | ||
|
||
import static com.listywave.common.exception.ErrorCode.RESOURCE_NOT_FOUND; | ||
|
||
import com.listywave.common.exception.CustomException; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface AdminRepository extends JpaRepository<Admin, Long> { | ||
|
||
default Admin getById(Long id) { | ||
return findById(id).orElseThrow(() -> new CustomException(RESOURCE_NOT_FOUND)); | ||
} | ||
} |
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,17 @@ | ||
package com.listywave.admin; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class AdminService { | ||
|
||
private final AdminRepository adminRepository; | ||
|
||
public void validateExist(Long adminId) { | ||
adminRepository.getById(adminId); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/listywave/notice/application/converter/NoticeTypeConverter.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,19 @@ | ||
package com.listywave.notice.application.converter; | ||
|
||
import com.listywave.notice.application.domain.NoticeType; | ||
import jakarta.persistence.AttributeConverter; | ||
import jakarta.persistence.Converter; | ||
|
||
@Converter(autoApply = true) | ||
public class NoticeTypeConverter implements AttributeConverter<NoticeType, String> { | ||
|
||
@Override | ||
public String convertToDatabaseColumn(NoticeType noticeType) { | ||
return String.valueOf(noticeType.getCode()); | ||
} | ||
|
||
@Override | ||
public NoticeType convertToEntityAttribute(String s) { | ||
return NoticeType.codeOf(Integer.parseInt(s)); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/listywave/notice/application/domain/NoticeType.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 com.listywave.notice.application.domain; | ||
|
||
import static com.listywave.common.exception.ErrorCode.NOT_EXIST_CODE; | ||
|
||
import com.listywave.common.exception.CustomException; | ||
import java.util.Arrays; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum NoticeType { | ||
|
||
NEWS(1, "소식"), | ||
EVENT(2, "이벤트"), | ||
TIP(3, "팁"), | ||
; | ||
|
||
private final int code; | ||
private final String viewName; | ||
|
||
public static NoticeType codeOf(int code) { | ||
return Arrays.stream(NoticeType.values()) | ||
.filter(noticeType -> noticeType.getCode() == code) | ||
.findFirst() | ||
.orElseThrow(() -> new CustomException(NOT_EXIST_CODE, NOT_EXIST_CODE.getDetail() + " 입력값: " + code)); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/listywave/notice/presentation/NoticeController.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,25 @@ | ||
package com.listywave.notice.presentation; | ||
|
||
import com.listywave.admin.AdminService; | ||
import com.listywave.common.auth.Auth; | ||
import com.listywave.notice.application.domain.NoticeType; | ||
import com.listywave.notice.presentation.dto.NoticeCategoryFindResponse; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class NoticeController { | ||
|
||
private final AdminService adminService; | ||
|
||
@GetMapping("/admin/notices/categories") | ||
ResponseEntity<List<NoticeCategoryFindResponse>> findNoticeCategories(@Auth Long adminId) { | ||
adminService.validateExist(adminId); | ||
List<NoticeCategoryFindResponse> result = NoticeCategoryFindResponse.toList(NoticeType.values()); | ||
return ResponseEntity.ok(result); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/listywave/notice/presentation/dto/NoticeCategoryFindResponse.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,17 @@ | ||
package com.listywave.notice.presentation.dto; | ||
|
||
import com.listywave.notice.application.domain.NoticeType; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public record NoticeCategoryFindResponse( | ||
int code, | ||
String viewName | ||
) { | ||
|
||
public static List<NoticeCategoryFindResponse> toList(NoticeType[] types) { | ||
return Arrays.stream(types) | ||
.map(type -> new NoticeCategoryFindResponse(type.getCode(), type.getViewName())) | ||
.toList(); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/test/java/com/listywave/notice/application/domain/NoticeTypeTest.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,38 @@ | ||
package com.listywave.notice.application.domain; | ||
|
||
import static com.listywave.common.exception.ErrorCode.NOT_EXIST_CODE; | ||
import static com.listywave.notice.application.domain.NoticeType.NEWS; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import com.listywave.common.exception.CustomException; | ||
import com.listywave.common.exception.ErrorCode; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class NoticeTypeTest { | ||
|
||
@Test | ||
void 존재하지_않는_코드_번호로_생성하면_예외가_발생한다() { | ||
// given | ||
int code = 0; | ||
|
||
// when | ||
ErrorCode result = assertThrows(CustomException.class, () -> NoticeType.codeOf(code)) | ||
.getErrorCode(); | ||
|
||
// then | ||
assertThat(result).isEqualTo(NOT_EXIST_CODE); | ||
} | ||
|
||
@Test | ||
void 코드_번호로_생성한다() { | ||
// given | ||
int code = 1; | ||
|
||
// when | ||
NoticeType result = NoticeType.codeOf(code); | ||
|
||
// then | ||
assertThat(result).isEqualTo(NEWS); | ||
} | ||
} |