Skip to content

Commit

Permalink
Merge pull request #243 from TEAMMatchDev/feature/issue-242
Browse files Browse the repository at this point in the history
⚡ : 공지사항 수정 (#242)
  • Loading branch information
imenuuu authored Feb 13, 2024
2 parents 9ba3870 + ce02829 commit a388ca7
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2024-01-17T19:31:30+0900",
date = "2024-02-13T15:15:37+0900",
comments = "version: 1.5.3.Final, compiler: javac, environment: Java 11.0.19 (Oracle Corporation)"
)
public class AdminNoticeMapperImpl implements AdminNoticeMapper {
Expand Down Expand Up @@ -43,6 +43,23 @@ public Notice toEntityNotice(NoticeUploadReq noticeUploadReq) {
return notice.build();
}

@Override
public NoticeContent toEntityNoticeContent(ContentsList contentsList, Long noticeId) {
if ( contentsList == null && noticeId == null ) {
return null;
}

NoticeContent.NoticeContentBuilder<?, ?> noticeContent = NoticeContent.builder();

if ( contentsList != null ) {
noticeContent.contentsType( contentsList.getContentsType() );
noticeContent.contents( contentsList.getContents() );
}
noticeContent.noticeId( noticeId );

return noticeContent.build();
}

protected NoticeContent contentsListToNoticeContent(ContentsList contentsList) {
if ( contentsList == null ) {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.matchapi.admin.notice.controller;

import com.example.matchapi.admin.event.dto.EventUploadReq;
import com.example.matchapi.admin.notice.dto.NoticeUpdateReq;
import com.example.matchapi.admin.notice.dto.NoticeUploadReq;
import com.example.matchapi.admin.notice.mapper.AdminNoticeMapper;
import com.example.matchapi.admin.notice.service.AdminNoticeService;
Expand Down Expand Up @@ -47,4 +48,11 @@ public CommonResponse<String> deleteNotice(@PathVariable Long noticeId){
adminNoticeService.deleteNotice(noticeId);
return CommonResponse.onSuccess("삭제 성공");
}

@PatchMapping("/{noticeId}")
@Operation(summary = "ADMIN-10-03 공지사항 수정")
public CommonResponse<String> updateNotice(@PathVariable Long noticeId, @Valid @RequestBody NoticeUpdateReq noticeUploadReq){
adminNoticeService.updateNotice(noticeId, noticeUploadReq);
return CommonResponse.onSuccess("수정 성공");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.example.matchapi.admin.notice.dto;

import java.util.List;


import com.example.matchapi.common.model.ContentsList;
import com.example.matchdomain.notice.enums.NoticeType;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class NoticeUpdateReq {
@Schema(description = "공지사항 제목", required = true, example = "공지사항 제목입니다.")
private String title;

@Schema(description = "공지사항 타입", required = true, example = "EVENT")
private NoticeType noticeType;

@Schema(description = "공지사항 내용 수정 리스트", required = false)
private List<NoticeContent> noticeContents;

@Schema(description = "추가할 컨텐츠 리스트", required = false)
private List<ContentsList> contentsList;

@Schema(description = "삭제할 컨텐츠 ID 리스트", required = false)
private List<Long> deleteContentsList;

@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ToString
public static class NoticeContent{
private Long contentId;

private String contents;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public interface AdminNoticeMapper {
List<NoticeContent> toEntityNoticeContents(List<ContentsList> contentsList);

Notice toEntityNotice(NoticeUploadReq noticeUploadReq);

NoticeContent toEntityNoticeContent(ContentsList contentsList, Long noticeId);
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package com.example.matchapi.admin.notice.service;

import com.example.matchapi.admin.notice.dto.NoticeUpdateReq;
import com.example.matchapi.admin.notice.mapper.AdminNoticeMapper;
import com.example.matchapi.common.model.ContentsList;
import com.example.matchapi.notice.converter.NoticeConverter;
import com.example.matchapi.notice.dto.NoticeRes;
import com.example.matchcommon.reponse.PageResponse;
import com.example.matchdomain.notice.adaptor.NoticeAdapter;
import com.example.matchdomain.notice.adaptor.NoticeContentAdaptor;
import com.example.matchdomain.notice.entity.Notice;
import com.example.matchdomain.notice.entity.NoticeContent;
import com.example.matchdomain.notice.repository.NoticeContentRepository;
import com.example.matchdomain.notice.repository.NoticeRepository;
import com.example.matchinfrastructure.config.s3.S3UploadService;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

import static com.example.matchdomain.common.model.ContentsType.IMG;
Expand All @@ -26,6 +29,8 @@ public class AdminNoticeService {
private final NoticeAdapter noticeAdapter;
private final S3UploadService s3UploadService;
private final NoticeConverter noticeConverter;
private final AdminNoticeMapper mapper = AdminNoticeMapper.INSTANCE;


@CacheEvict(value = "noticeCache", allEntries = true, cacheManager = "ehcacheManager")
public void uploadNoticeList(List<NoticeContent> noticeContents, Notice notice) {
Expand All @@ -51,4 +56,32 @@ public PageResponse<List<NoticeRes.NoticeList>> getNoticeList(int page, int size
Page<Notice> notices = noticeAdapter.getNoticeList(page,size);
return new PageResponse<>(notices.isLast(), notices.getTotalElements(), noticeConverter.convertToNoticeList(notices.getContent()));
}

@Transactional
public void updateNotice(Long noticeId, NoticeUpdateReq noticeUploadReq) {
Notice notice = noticeAdapter.findNoticeDetail(noticeId);
notice.updateNotice(noticeUploadReq.getTitle(), noticeUploadReq.getNoticeType());
updateNoticeContent(noticeUploadReq, noticeId);
}

private void updateNoticeContent(NoticeUpdateReq noticeUploadReq, Long noticeId) {
if(noticeUploadReq.getDeleteContentsList() != null){
noticeContentAdaptor.deleteNoticeContent(noticeUploadReq.getDeleteContentsList());
}
if(noticeUploadReq.getContentsList() != null){
List<NoticeContent> noticeContents = new ArrayList<>();
for(ContentsList contentsList : noticeUploadReq.getContentsList()){
noticeContents.add(mapper.toEntityNoticeContent(contentsList, noticeId));
}
noticeContentAdaptor.saveAll(noticeContents);
}
if(noticeUploadReq.getNoticeContents() != null){
for(NoticeUpdateReq.NoticeContent noticeContent : noticeUploadReq.getNoticeContents()){
NoticeContent content = noticeContentAdaptor.findById(noticeContent.getContentId());
content.updateContents(noticeContent.getContents());
noticeContentAdaptor.save(content);
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ public abstract class ContentsEntity {

@Enumerated(EnumType.STRING)
private Status status = Status.ACTIVE;

public void updateContents(String contents) {
this.contents = contents;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.example.matchcommon.annotation.Adaptor;
import com.example.matchcommon.exception.BadRequestException;
import com.example.matchdomain.notice.entity.Notice;
import com.example.matchdomain.notice.repository.NoticeContentRepository;
import com.example.matchdomain.notice.repository.NoticeRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
Expand All @@ -11,6 +12,8 @@

import static com.example.matchdomain.notice.exception.GetNoticeErrorCode.NOT_EXIST_NOTICE;

import java.util.List;

@Adaptor
@RequiredArgsConstructor
public class NoticeAdapter {
Expand All @@ -32,4 +35,5 @@ public Notice save(Notice notice) {
public void delete(Notice notice) {
noticeRepository.deleteById(notice.getId());
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.example.matchdomain.notice.adaptor;

import static com.example.matchdomain.notice.exception.GetNoticeErrorCode.*;

import com.example.matchcommon.annotation.Adaptor;
import com.example.matchcommon.exception.BadRequestException;
import com.example.matchdomain.notice.entity.NoticeContent;
import com.example.matchdomain.notice.repository.NoticeContentRepository;
import lombok.RequiredArgsConstructor;
Expand All @@ -15,4 +18,17 @@ public class NoticeContentAdaptor {
public void saveAll(List<NoticeContent> noticeContents) {
contentRepository.saveAll(noticeContents);
}

public void deleteNoticeContent(List<Long> deleteContentsList) {
contentRepository.deleteAllByIdInBatch(deleteContentsList);
}

public NoticeContent findById(Long contentId) {
return contentRepository.findById(contentId).orElseThrow(()->
new BadRequestException(NOT_EXIST_NOTICE_CONTENT));
}

public NoticeContent save(NoticeContent content) {
return contentRepository.save(content);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@ public class Notice extends BaseEntity {

private String title;

public void updateNotice(String title, NoticeType noticeType) {
this.title = title;
this.noticeType = noticeType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
public enum GetNoticeErrorCode implements BaseErrorCode {

@ExplainError("해당 공지사항이 존재하지 않습니다.")
NOT_EXIST_NOTICE(HttpStatus.BAD_REQUEST,false,"NOTICE_001","해당 공지사항이 존재하지 않습니다..");
NOT_EXIST_NOTICE(HttpStatus.BAD_REQUEST,false,"NOTICE_001","해당 공지사항이 존재하지 않습니다..")
,NOT_EXIST_NOTICE_CONTENT(HttpStatus.BAD_REQUEST,false,"NOTICE_002","해당 공지사항의 컨텐츠가 존재하지 않습니다.");

private final HttpStatus httpStatus;
private final boolean isSuccess;
Expand Down

0 comments on commit a388ca7

Please sign in to comment.