Skip to content

Commit

Permalink
feat: 공지 카테고리(타입) 조회 API 구현 (#312)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdkdhoho committed Oct 30, 2024
1 parent 8d5ece2 commit 4c39059
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/main/java/com/listywave/admin/Admin.java
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;
}
13 changes: 13 additions & 0 deletions src/main/java/com/listywave/admin/AdminRepository.java
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));
}
}
17 changes: 17 additions & 0 deletions src/main/java/com/listywave/admin/AdminService.java
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);
}
}
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));
}
}
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));
}
}
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);
}
}
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();
}
}
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);
}
}

0 comments on commit 4c39059

Please sign in to comment.