Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

배너 사진 갯수 조절, 배너 삭제 가능 #407

Merged
merged 3 commits into from
Mar 4, 2025
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
13 changes: 13 additions & 0 deletions src/main/java/com/green/acamatch/academy/BannerPicRepository.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.green.acamatch.academy;


import com.green.acamatch.academy.banner.model.BannerPicCountGetRes;
import com.green.acamatch.entity.banner.BannerPic;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -16,4 +18,15 @@ public interface BannerPicRepository extends JpaRepository<BannerPic, Long> {
@Modifying
@Query(" update BannerPic a set a.bannerShow =:bannerShow where a.banner.acaId =:acaId and a.bannerPosition =:bannerPosition")
int updateBannerPicShowByAcaIdAndBannerPosition(Long acaId, int bannerPosition, int bannerShow);

//배너 pk 갯수 뽑기.
@Query(" select COUNT(a) as countBannerPic from BannerPic a WHERE a.bannerPicIds.acaId = :acaId")
Long countById(@Param("acaId") Long acaId);

//배너 사진 하나씩 삭제
@Transactional
@Modifying
@Query(" delete from BannerPic a where a.bannerPicIds.acaId =:acaId and a.bannerPosition =:bannerPosition ")
int deleteByacaIdAndBannerPosition(Long acaId, int bannerPosition);

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,10 @@ public interface BannerRepository extends JpaRepository<Banner, Long> {
@Query("select new com.green.acamatch.academy.banner.model.BannerGetRes(a.acaId, a.acaName, a.bannerType, a.startDate, a.endDate,b.bannerPicIds.bannerPic, b.bannerPosition, b.bannerShow )FROM Banner a JOIN BannerPic b ON a.acaId = b.banner.acaId ORDER BY b.bannerPosition ASC")
List<BannerGetRes> findAllBanner();

//배너 사진 없으면 배너 데이터 삭제
@Transactional
@Modifying
@Query(" delete from Banner a where a.acaId =:acaId ")
int deleteBannerByAcaId(Long acaId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.transform.Result;
import java.util.List;

@Slf4j
Expand All @@ -24,8 +25,8 @@ public class BannerController {

@PostMapping
@Operation(summary = "배너신청")
public ResultResponse<Integer> postBanner(@RequestPart MultipartFile topBannerPic, @RequestPart MultipartFile bottomBannerPic
, @RequestPart MultipartFile leftBannerPic, @RequestPart MultipartFile rightBannerPic
public ResultResponse<Integer> postBanner(@RequestPart(required = false) MultipartFile topBannerPic, @RequestPart(required = false) MultipartFile bottomBannerPic
, @RequestPart(required = false) MultipartFile leftBannerPic, @RequestPart(required = false) MultipartFile rightBannerPic
, @RequestPart BannerPostReq req) {
bannerService.postBanner(topBannerPic, bottomBannerPic, leftBannerPic, rightBannerPic, req);
return ResultResponse.<Integer>builder()
Expand Down Expand Up @@ -86,4 +87,14 @@ public ResultResponse<List<BannerGetRes>> getBannerAll() {
.resultData(res)
.build();
}

@DeleteMapping
@Operation(summary = "배너 삭제")
public ResultResponse<Integer> delBanner(BannerDeleteReq req) {
bannerService.delBanner(req.getAcaId(), req.getBannerPosition());
return ResultResponse.<Integer>builder()
.resultMessage(academyMessage.getMessage())
.resultData(1)
.build();
}
}
104 changes: 76 additions & 28 deletions src/main/java/com/green/acamatch/academy/Service/BannerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,23 @@ public class BannerService {
private final RequestBodyService requestBodyBuilder;
private final AcademyMessage academyMessage;

//배너신청
@Transactional
public int postBanner(MultipartFile topBannerPic, MultipartFile bottomBannerPic,
MultipartFile leftBannerPic, MultipartFile rightBannerPic
, BannerPostReq req) {
log.info("req: {}", req);

//특정 학원pk가 하나라도 있을때
if(bannerPicRepository.countById(req.getAcaId()) > 0) {
throw new CustomException(AcademyException.DATA_EXISTS);
}

//배너사진을 하나도 넣지 않았을때
if((topBannerPic == null && bottomBannerPic == null && leftBannerPic == null && rightBannerPic == null)) {
throw new CustomException(AcademyException.MISSING_REQUIRED_FILED_EXCEPTION);
}

long acaId = req.getAcaId();
PremiumAcademy premiumAcademy = premiumRepository.findById(acaId).orElseThrow();
Academy academy = academyRepository.findById(acaId).orElseThrow();
Expand All @@ -69,6 +80,7 @@ public int postBanner(MultipartFile topBannerPic, MultipartFile bottomBannerPic,
String bottomBannerPicName = (bottomBannerPic != null ? myFileUtils.makeRandomFileName(topBannerPic) : null);
String leftBannerPicName = (leftBannerPic != null ? myFileUtils.makeRandomFileName(leftBannerPic) : null);
String rightBannerPicName = (rightBannerPic != null ? myFileUtils.makeRandomFileName(rightBannerPic) : null);

String filePath1 = String.format("%s/%s/%s", middlePath, "top", topBannerPicName);
myFileUtils.makeFolders(filePath1);
String filePath2 = String.format("%s/%s/%s", middlePath, "bottom", bottomBannerPicName);
Expand All @@ -86,13 +98,18 @@ public int postBanner(MultipartFile topBannerPic, MultipartFile bottomBannerPic,
bannerpic.setBannerPicIds(bannerPicIds);
bannerpic.setBanner(banner);
bannerpic.setBannerPosition(1);
bannerPicRepository.save(bannerpic);
try {
myFileUtils.transferTo(topBannerPic, filePath1);
} catch (IOException e) {
String delFolderPath = String.format("%s/%s", myFileUtils.getUploadPath(), middlePath);
myFileUtils.deleteFolder(delFolderPath, true);
throw new CustomException(AcademyException.PHOTO_SAVE_FAILED);

bannerPicRepository.save(bannerpic);


if(topBannerPic != null && !topBannerPic.isEmpty()) {
try {
myFileUtils.transferTo(topBannerPic, filePath1);
} catch (IOException e) {
String delFolderPath = String.format("%s/%s", myFileUtils.getUploadPath(), middlePath);
myFileUtils.deleteFolder(delFolderPath, true);
throw new CustomException(AcademyException.PHOTO_SAVE_FAILED);
}
}


Expand All @@ -102,13 +119,18 @@ public int postBanner(MultipartFile topBannerPic, MultipartFile bottomBannerPic,
bannerpic.setBannerPicIds(bannerPicIds);
bannerpic.setBanner(banner);
bannerpic.setBannerPosition(2);
bannerPicRepository.save(bannerpic);
try {
myFileUtils.transferTo(bottomBannerPic, filePath2);
} catch (IOException e) {
String delFolderPath = String.format("%s/%s", myFileUtils.getUploadPath(), middlePath);
myFileUtils.deleteFolder(delFolderPath, true);
throw new CustomException(AcademyException.PHOTO_SAVE_FAILED);

bannerPicRepository.save(bannerpic);


if(bottomBannerPic != null && !bottomBannerPic.isEmpty()) {
try {
myFileUtils.transferTo(bottomBannerPic, filePath2);
} catch (IOException e) {
String delFolderPath = String.format("%s/%s", myFileUtils.getUploadPath(), middlePath);
myFileUtils.deleteFolder(delFolderPath, true);
throw new CustomException(AcademyException.PHOTO_SAVE_FAILED);
}
}


Expand All @@ -118,35 +140,46 @@ public int postBanner(MultipartFile topBannerPic, MultipartFile bottomBannerPic,
bannerpic.setBannerPicIds(bannerPicIds);
bannerpic.setBanner(banner);
bannerpic.setBannerPosition(3);
bannerPicRepository.save(bannerpic);
try {
myFileUtils.transferTo(leftBannerPic, filePath3);
} catch (IOException e) {
String delFolderPath = String.format("%s/%s", myFileUtils.getUploadPath(), middlePath);
myFileUtils.deleteFolder(delFolderPath, true);
throw new CustomException(AcademyException.PHOTO_SAVE_FAILED);

bannerPicRepository.save(bannerpic);


if(leftBannerPic != null && !leftBannerPic.isEmpty()) {
try {
myFileUtils.transferTo(leftBannerPic, filePath3);
} catch (IOException e) {
String delFolderPath = String.format("%s/%s", myFileUtils.getUploadPath(), middlePath);
myFileUtils.deleteFolder(delFolderPath, true);
throw new CustomException(AcademyException.PHOTO_SAVE_FAILED);
}
}


bannerPicIds.setAcaId(acaId);
bannerPicIds.setBannerPic(rightBannerPicName);
bannerPicIds.setAcaId(acaId);
bannerPicIds.setBannerPic(rightBannerPicName);

bannerpic.setBannerPicIds(bannerPicIds);
bannerpic.setBanner(banner);
bannerpic.setBannerPosition(4);

bannerpic.setBannerPicIds(bannerPicIds);
bannerpic.setBanner(banner);
bannerpic.setBannerPosition(4);
bannerPicRepository.save(bannerpic);


if(rightBannerPic != null && !rightBannerPic.isEmpty()) {
try {
myFileUtils.transferTo(rightBannerPic, filePath4);
} catch (IOException e) {
String delFolderPath = String.format("%s/%s", myFileUtils.getUploadPath(), middlePath);
myFileUtils.deleteFolder(delFolderPath, true);
throw new CustomException(AcademyException.PHOTO_SAVE_FAILED);
}
}

academyMessage.setMessage("배너신청이 완료되었습니다.");
return 1;
}

//배너승인
@Transactional
public int updateBannerType(Long acaId, int bannerType) {
int result = bannerRepository.updateBannerTypeByAcaId(acaId, bannerType);
Expand All @@ -158,7 +191,7 @@ public int updateBannerType(Long acaId, int bannerType) {
}



//배너 활성화/비활성화
@Transactional
public int updateBannerShow(Long acaId, int bannerPosition, int bannerShow) {
bannerPicRepository.updateBannerPicShowByAcaIdAndBannerPosition(acaId, bannerPosition, bannerShow);
Expand All @@ -170,24 +203,39 @@ public int updateBannerShow(Long acaId, int bannerPosition, int bannerShow) {
return 1;
}

//배너 포지션별 조회
@Transactional
public List<BannerByPositionGetRes> getBannerByPosition(Long acaId, int position) {
List<BannerByPositionGetRes> res = bannerRepository.findBannerByPosition(acaId, position);
academyMessage.setMessage("배너가 조회되었습니다.");
return res;
}

//특정 프리미엄학원의 배너 조회
@Transactional
public List<BannerGetRes> getBanner(Long acaId) {
List<BannerGetRes> res = bannerRepository.findBanner(acaId);
academyMessage.setMessage("%d번의, acaId" + "프리미엄학원의 배너가 조회되었습니다.");
academyMessage.setMessage("%d번, acaId" + " 프리미엄학원의 배너가 조회되었습니다.");
return res;
}

//프리미엄 학원의 모든 배너 조회
@Transactional
public List<BannerGetRes> getBannerAll() {
List<BannerGetRes> res = bannerRepository.findAllBanner();
academyMessage.setMessage("프리미엄학원의 모든 배너가 조회되었습니다.");
return res;
}

//배너 하나씩 삭제
@Transactional
public int delBanner(Long acaId, int bannerPosition) {
bannerPicRepository.deleteByacaIdAndBannerPosition(acaId, bannerPosition);
if(bannerPicRepository.countById(acaId) == 0) {
bannerRepository.deleteBannerByAcaId(acaId);
}

academyMessage.setMessage("배너가 삭제되었습니다.");
return 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.green.acamatch.academy.banner.model;

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

@Getter
@Setter
@ToString
public class BannerDeleteReq {
@Schema(title = "학원pk")
private Long acaId;

@Schema(title = "배너 포지션", description = "1 or 2 or 3 or 4")
private int bannerPosition;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.green.acamatch.academy.banner.model;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
@AllArgsConstructor
public class BannerPicCountGetRes {
private int countBannerPic;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ public class BannerPostReq {
@Schema(title = "학원 pk")
private Long acaId;

/*@JsonIgnore
@Schema(title = "배너 사진")
private String bannerPic;
private String bannerPic;*/

/*@JsonIgnore
@Schema(title = "배너승인여부")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public enum AcademyException implements ErrorCode{
MISSING_UPDATE_FILED_EXCEPTION(HttpStatus.BAD_REQUEST, "수정할 값을 하나 이상 입력해주세요."),
NO_SUCH_ELEMENT_EXCEPTION(HttpStatus.NOT_FOUND, "값을 가져올수 없습니다."),
ILLEGAL_ARGUMENT_EXCEPTION(HttpStatus.BAD_REQUEST, "주소를 수정하려면 주소와 관련된 값을 다 입력해주세요."),
NOT_FOUND_BUSINESSNUMBER(HttpStatus.NOT_FOUND, "조회할 수 없는 사업자등록번호입니다.");
NOT_FOUND_BUSINESSNUMBER(HttpStatus.NOT_FOUND, "조회할 수 없는 사업자등록번호입니다."),
DATA_EXISTS(HttpStatus.BAD_REQUEST, "배너가 이미 존재합니다.");


private final HttpStatus httpStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class AcademyPicIds implements Serializable {
private Long acaId;

@Column(length = 50)
@Column(length = 50, nullable = true)
private String acaPic;

}