Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ out/

### VS Code ###
.vscode/

### macOS ###
.DS_Store
#*.properties


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
import org.layer.domain.notice.entity.NoticeCategory;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;

public class AdminNoticeRequest {

public record CreateNoticeRequest(
@NotNull
@Size(max = 100)
String title,
@NotNull
@Size(max = 800)
String content,
@NotNull
NoticeCategory category,
Expand All @@ -38,8 +41,10 @@ public Notice toEntity() {

public record UpdateNoticeRequest(
@NotNull
@Size(max = 100)
String title,
@NotNull
@Size(max = 800)
String content,
@NotNull
NoticeCategory category,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.layer.admin.popup.controller;

import org.layer.admin.popup.controller.dto.AdminPopupRequest.CreatePopupRequest;
import org.layer.admin.popup.controller.dto.AdminPopupRequest.UpdatePopupActiveRequest;
import org.layer.admin.popup.controller.dto.AdminPopupRequest.UpdatePopupRequest;
import org.layer.admin.popup.controller.dto.AdminPopupResponse.PopupCreateResponse;
import org.layer.admin.popup.controller.dto.AdminPopupResponse.PopupDetail;
import org.layer.admin.popup.controller.dto.AdminPopupResponse.PopupPage;
import org.layer.admin.popup.service.AdminPopupService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/popup")
public class AdminPopupController {

private final AdminPopupService adminPopupService;

@GetMapping("")
public ResponseEntity<PopupPage> getPopups(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "20") int size) {
return ResponseEntity.ok(adminPopupService.getPopups(page, size));
}

@GetMapping("/{popupId}")
public ResponseEntity<PopupDetail> getPopup(@PathVariable Long popupId) {
return ResponseEntity.ok(adminPopupService.getPopup(popupId));
}

@PostMapping("")
public ResponseEntity<PopupCreateResponse> createPopup(@RequestBody @Valid CreatePopupRequest request) {
Long popupId = adminPopupService.createPopup(request);
return ResponseEntity.ok(PopupCreateResponse.builder().popupId(popupId).build());
}

@PutMapping("/{popupId}")
public ResponseEntity<Void> updatePopup(@PathVariable Long popupId,
@RequestBody @Valid UpdatePopupRequest request) {
adminPopupService.updatePopup(popupId, request);
return ResponseEntity.ok().build();
}

@PatchMapping("/{popupId}/active")
public ResponseEntity<Void> updatePopupActive(@PathVariable Long popupId,
@RequestBody @Valid UpdatePopupActiveRequest request) {
adminPopupService.updatePopupActive(popupId, request);
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.layer.admin.popup.controller.dto;

import org.layer.domain.popup.entity.Popup;
import org.layer.domain.popup.entity.PopupIconType;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;

public class AdminPopupRequest {

public record CreatePopupRequest(
@NotNull
PopupIconType iconType,
@NotNull
@Size(max = 30)
String title,
@NotNull
@Size(max = 800)
String content,
String moveButtonUrl,
@NotNull
Boolean isActive
) {
public Popup toEntity() {
return Popup.builder()
.iconType(iconType)
.title(title)
.content(content)
.moveButtonUrl(moveButtonUrl)
.isActive(isActive)
.build();
}
}

public record UpdatePopupRequest(
@NotNull
PopupIconType iconType,
@NotNull
@Size(max = 30)
String title,
@NotNull
@Size(max = 800)
String content,
String moveButtonUrl,
@NotNull
Boolean isActive
) {
}

public record UpdatePopupActiveRequest(
@NotNull
Boolean isActive
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.layer.admin.popup.controller.dto;

import java.time.LocalDateTime;
import java.util.List;

import org.layer.domain.popup.entity.Popup;
import org.layer.domain.popup.entity.PopupIconType;
import org.springframework.data.domain.Page;

import lombok.Builder;

public class AdminPopupResponse {

@Builder
public record PopupCreateResponse(
Long popupId
) {
}

@Builder
public record PopupSummary(
Long id,
PopupIconType iconType,
String title,
Boolean isActive,
LocalDateTime createdAt
) {
public static PopupSummary of(Popup popup) {
return PopupSummary.builder()
.id(popup.getId())
.iconType(popup.getIconType())
.title(popup.getTitle())
.isActive(popup.getIsActive())
.createdAt(popup.getCreatedAt())
.build();
}
}

@Builder
public record PopupDetail(
Long id,
PopupIconType iconType,
String title,
String content,
String moveButtonUrl,
Boolean isActive,
LocalDateTime createdAt,
LocalDateTime updatedAt
) {
public static PopupDetail of(Popup popup) {
return PopupDetail.builder()
.id(popup.getId())
.iconType(popup.getIconType())
.title(popup.getTitle())
.content(popup.getContent())
.moveButtonUrl(popup.getMoveButtonUrl())
.isActive(popup.getIsActive())
.createdAt(popup.getCreatedAt())
.updatedAt(popup.getUpdatedAt())
.build();
}
}

@Builder
public record PopupPage(
List<PopupSummary> popups,
long totalCount,
int totalPages
) {
public static PopupPage of(Page<Popup> page) {
return PopupPage.builder()
.popups(page.getContent().stream().map(PopupSummary::of).toList())
.totalCount(page.getTotalElements())
.totalPages(page.getTotalPages())
.build();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.layer.admin.popup.service;

import org.layer.admin.popup.controller.dto.AdminPopupRequest.CreatePopupRequest;
import org.layer.admin.popup.controller.dto.AdminPopupRequest.UpdatePopupActiveRequest;
import org.layer.admin.popup.controller.dto.AdminPopupRequest.UpdatePopupRequest;
import org.layer.admin.popup.controller.dto.AdminPopupResponse.PopupDetail;
import org.layer.admin.popup.controller.dto.AdminPopupResponse.PopupPage;
import org.layer.domain.popup.entity.Popup;
import org.layer.domain.popup.repository.PopupRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class AdminPopupService {

private final PopupRepository popupRepository;

public PopupPage getPopups(int page, int size) {
Page<Popup> popups = popupRepository.findAll(
PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "createdAt")));
return PopupPage.of(popups);
}

public PopupDetail getPopup(Long popupId) {
Popup popup = popupRepository.findByIdOrThrow(popupId);
return PopupDetail.of(popup);
}

@Transactional
public Long createPopup(CreatePopupRequest request) {
Popup popup = popupRepository.save(request.toEntity());
return popup.getId();
}

@Transactional
public void updatePopup(Long popupId, UpdatePopupRequest request) {
Popup popup = popupRepository.findByIdOrThrow(popupId);
popup.update(request.iconType(), request.title(), request.content(), request.moveButtonUrl(),
request.isActive());
}

@Transactional
public void updatePopupActive(Long popupId, UpdatePopupActiveRequest request) {
Popup popup = popupRepository.findByIdOrThrow(popupId);
popup.updateActive(request.isActive());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public record NoticeSummary(
Long id,
@Schema(description = "제목")
String title,
@Schema(description = "본문")
String content,
@Schema(description = "카테고리")
NoticeCategory category,
@Schema(description = "상단 고정 여부")
Expand All @@ -33,6 +35,7 @@ public static NoticeSummary of(Notice notice) {
return NoticeSummary.builder()
.id(notice.getId())
.title(notice.getTitle())
.content(notice.getContent())
.category(notice.getCategory())
.isPinned(notice.getIsPinned())
.startAt(notice.getStartAt())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.layer.domain.popup.controller;

import org.layer.domain.popup.controller.dto.PopupResponse.PopupListResponse;
import org.layer.domain.popup.service.PopupService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/popup")
public class PopupController {

private final PopupService popupService;

@GetMapping("/list")
public ResponseEntity<PopupListResponse> getPopups() {
return ResponseEntity.ok(popupService.getExposedPopups());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.layer.domain.popup.controller.dto;

import java.util.List;

import org.layer.domain.popup.entity.Popup;
import org.layer.domain.popup.entity.PopupIconType;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;

public class PopupResponse {

@Builder
@Schema(description = "팝업 정보")
public record PopupElement(
@Schema(description = "팝업 아이디")
Long id,
@Schema(description = "아이콘 id", example = "default")
PopupIconType iconType,
@Schema(description = "제목")
String title,
@Schema(description = "내용")
String content,
@Schema(description = "이동 버튼 URL. null이면 이동 버튼 없이 확인 버튼만 노출")
String moveButtonUrl
) {
public static PopupElement of(Popup popup) {
return PopupElement.builder()
.id(popup.getId())
.iconType(popup.getIconType())
.title(popup.getTitle())
.content(popup.getContent())
.moveButtonUrl(popup.getMoveButtonUrl())
.build();
}
}

@Builder
@Schema
public record PopupListResponse(
List<PopupElement> popups
) {
public static PopupListResponse of(List<Popup> popups) {
return PopupListResponse.builder()
.popups(popups.stream().map(PopupElement::of).toList())
.build();
}
}
}
Loading
Loading