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

[Feat] 배너 업로드 Admin페이지 구현 #187

Merged
merged 8 commits into from
Nov 14, 2024
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
11 changes: 8 additions & 3 deletions src/main/java/team7/inplace/admin/AdminPageController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import team7.inplace.admin.persistence.BannerRepository;
import team7.inplace.global.exception.ErrorLog;
import team7.inplace.global.exception.ErrorLogRepository;
import team7.inplace.admin.banner.persistence.BannerRepository;
import team7.inplace.admin.error.ErrorLog;
import team7.inplace.admin.error.ErrorLogRepository;
import team7.inplace.global.kakao.config.KakaoApiProperties;
import team7.inplace.video.domain.Video;
import team7.inplace.video.persistence.VideoRepository;
Expand Down Expand Up @@ -52,4 +52,9 @@ public String getBanners(Model model) {
model.addAttribute("banners", banners);
return "admin/banner.html";
}

@GetMapping("/main")
public String getMainPage() {
return "admin/main.html";
}
}
34 changes: 0 additions & 34 deletions src/main/java/team7/inplace/admin/application/BannerService.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package team7.inplace.admin.banner.application;

import java.time.LocalDateTime;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import team7.inplace.admin.banner.application.command.BannerCommand.Create;
import team7.inplace.admin.banner.application.dto.BannerInfo;
import team7.inplace.admin.banner.application.dto.BannerInfo.Detail;
import team7.inplace.admin.banner.persistence.BannerRepository;
import team7.inplace.admin.banner.persistence.BannerS3Repository;
import team7.inplace.global.exception.InplaceException;
import team7.inplace.global.exception.code.BannerErrorCode;

@Service
@RequiredArgsConstructor
public class BannerService {
private final BannerS3Repository bannerS3Repository;
private final BannerRepository bannerRepository;

public void uploadBanner(Create command) {
var imgPath = bannerS3Repository.uploadBanner(command.imageFile());
var banner = command.toEntity(imgPath);
bannerRepository.save(banner);
}

public List<Detail> getBanners() {
var now = LocalDateTime.now();
var banners = bannerRepository.findActiveBanner(now);

return banners.stream()
.sorted((a, b) -> Boolean.compare(b.getIsFixed(), a.getIsFixed()))
.map(BannerInfo.Detail::from)
.toList();
}

public void deleteBanner(Long id) {
var banner = bannerRepository.findById(id)
.orElseThrow(() -> InplaceException.of(BannerErrorCode.NOT_FOUND));
bannerS3Repository.deleteBanner(banner.getImgPath());
bannerRepository.delete(banner);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package team7.inplace.admin.application.command;
package team7.inplace.admin.banner.application.command;

import java.time.LocalDateTime;
import org.springframework.web.multipart.MultipartFile;
import team7.inplace.admin.domain.Banner;
import team7.inplace.admin.banner.domain.Banner;

public class BannerCommand {
public record Create(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package team7.inplace.admin.application.dto;
package team7.inplace.admin.banner.application.dto;

import team7.inplace.admin.domain.Banner;
import team7.inplace.admin.banner.domain.Banner;

public class BannerInfo {
public record Detail(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package team7.inplace.admin.domain;
package team7.inplace.admin.banner.domain;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package team7.inplace.admin.persistence;
package team7.inplace.admin.banner.persistence;

import java.time.LocalDateTime;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import team7.inplace.admin.domain.Banner;
import team7.inplace.admin.banner.domain.Banner;

@Repository
public interface BannerRepository extends JpaRepository<Banner, Long> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package team7.inplace.admin.persistence;
package team7.inplace.admin.banner.persistence;

import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.ObjectMetadata;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.web.multipart.MultipartFile;
import team7.inplace.infra.s3.AwsProperties;

@Repository
@Slf4j
@RequiredArgsConstructor
public class BannerS3Repository {
private final AmazonS3Client amazonS3Client;
private final AwsProperties awsProperties;

public String uploadBanner(String bannerName, MultipartFile banner) {
public String uploadBanner(MultipartFile banner) {
var bucketName = awsProperties.bucketName();
var key = "banner/" + bannerName;
var key = "banner/" + UUID.randomUUID();

ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(banner.getSize());
Expand All @@ -28,4 +31,15 @@ public String uploadBanner(String bannerName, MultipartFile banner) {
throw new RuntimeException("Failed to upload banner", e);
}
}

public void deleteBanner(String imgPath) {
var bucketName = awsProperties.bucketName();
var key = imgPath.substring(imgPath.lastIndexOf("banner"));

try {
amazonS3Client.deleteObject(bucketName, key);
} catch (Exception e) {
throw new RuntimeException("Failed to delete banner", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package team7.inplace.admin.presentation;
package team7.inplace.admin.banner.presentation;

import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import team7.inplace.admin.application.BannerService;
import team7.inplace.admin.banner.application.BannerService;

@RestController
@Slf4j
Expand All @@ -33,4 +35,9 @@ public ResponseEntity<List<BannerResponse.Info>> getBanners() {
.toList();
return new ResponseEntity<>(response, HttpStatus.OK);
}

@DeleteMapping("/{id}")
public void deleteBanner(@PathVariable Long id) {
bannerService.deleteBanner(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package team7.inplace.admin.presentation;
package team7.inplace.admin.banner.presentation;

import java.time.LocalDate;
import org.springframework.web.multipart.MultipartFile;
import team7.inplace.admin.application.command.BannerCommand;
import team7.inplace.admin.banner.application.command.BannerCommand;

public class BannerRequest {
public record Create(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package team7.inplace.admin.presentation;
package team7.inplace.admin.banner.presentation;

import team7.inplace.admin.application.dto.BannerInfo;
import team7.inplace.admin.banner.application.dto.BannerInfo;

public class BannerResponse {
public record Info(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package team7.inplace.cicd;
package team7.inplace.admin.cicd;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package team7.inplace.crawling.application;
package team7.inplace.admin.crawling.application;

import com.fasterxml.jackson.databind.JsonNode;
import lombok.NoArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package team7.inplace.crawling.application;
package team7.inplace.admin.crawling.application;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import team7.inplace.crawling.application.dto.CrawlingInfo;
import team7.inplace.admin.crawling.application.dto.CrawlingInfo;
import team7.inplace.global.annotation.Facade;
import team7.inplace.place.application.command.PlacesCommand;
import team7.inplace.video.application.VideoFacade;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package team7.inplace.crawling.application;
package team7.inplace.admin.crawling.application;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import team7.inplace.crawling.client.KakaoMapClient;
import team7.inplace.crawling.client.dto.PlaceNode;
import team7.inplace.admin.crawling.client.KakaoMapClient;
import team7.inplace.admin.crawling.client.dto.PlaceNode;

@Service
@RequiredArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package team7.inplace.crawling.application;
package team7.inplace.admin.crawling.application;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team7.inplace.crawling.application.dto.CrawlingInfo;
import team7.inplace.crawling.client.YoutubeClient;
import team7.inplace.admin.crawling.application.dto.CrawlingInfo;
import team7.inplace.admin.crawling.application.dto.CrawlingInfo.ViewInfo;
import team7.inplace.admin.crawling.client.YoutubeClient;
import team7.inplace.video.persistence.VideoRepository;

import java.util.List;
Expand All @@ -16,7 +17,7 @@ public class VideoCrawlingService {
private final YoutubeClient youtubeClient;

@Transactional(readOnly = true)
public List<CrawlingInfo.ViewInfo> crawlingVideoView() {
public List<ViewInfo> crawlingVideoView() {
var videos = videoRepository.findAll();

var videoInfos = videos.stream().map(video -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package team7.inplace.crawling.application;
package team7.inplace.admin.crawling.application;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team7.inplace.crawling.application.dto.CrawlingInfo;
import team7.inplace.crawling.client.KakaoMapClient;
import team7.inplace.crawling.client.YoutubeClient;
import team7.inplace.crawling.persistence.YoutubeChannelRepository;
import team7.inplace.admin.crawling.client.KakaoMapClient;
import team7.inplace.admin.crawling.client.YoutubeClient;
import team7.inplace.admin.crawling.persistence.YoutubeChannelRepository;
import team7.inplace.admin.crawling.application.dto.CrawlingInfo;
import team7.inplace.global.exception.InplaceException;
import team7.inplace.global.exception.code.ChannelErrorCode;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package team7.inplace.crawling.application.dto;
package team7.inplace.admin.crawling.application.dto;

import com.fasterxml.jackson.databind.JsonNode;
import team7.inplace.crawling.client.dto.PlaceNode;
import team7.inplace.place.application.command.PlacesCommand;
import team7.inplace.video.application.command.VideoCommand;

import java.util.List;
import java.util.Objects;
import team7.inplace.admin.crawling.client.dto.PlaceNode;
import team7.inplace.place.application.command.PlacesCommand;
import team7.inplace.video.application.command.VideoCommand;

public class CrawlingInfo {
public record VideoPlaceInfo(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package team7.inplace.crawling.client;
package team7.inplace.admin.crawling.client;

import com.fasterxml.jackson.databind.JsonNode;
import lombok.RequiredArgsConstructor;
Expand All @@ -9,7 +9,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import team7.inplace.crawling.client.dto.PlaceNode;
import team7.inplace.admin.crawling.client.dto.PlaceNode;
import team7.inplace.global.exception.InplaceException;
import team7.inplace.global.exception.code.PlaceErrorCode;
import team7.inplace.global.kakao.config.KakaoApiProperties;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package team7.inplace.crawling.client;
package team7.inplace.admin.crawling.client;

import com.fasterxml.jackson.databind.JsonNode;
import lombok.extern.slf4j.Slf4j;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package team7.inplace.crawling.client.dto;
package team7.inplace.admin.crawling.client.dto;

import com.fasterxml.jackson.databind.JsonNode;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package team7.inplace.crawling.domain;
package team7.inplace.admin.crawling.domain;

public enum ChannelType {
FOOD("FD6");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package team7.inplace.crawling.domain;
package team7.inplace.admin.crawling.domain;

import jakarta.persistence.*;
import lombok.AccessLevel;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package team7.inplace.crawling.persistence;
package team7.inplace.admin.crawling.persistence;

import org.springframework.data.jpa.repository.JpaRepository;
import team7.inplace.crawling.domain.YoutubeChannel;
import team7.inplace.admin.crawling.domain.YoutubeChannel;

import java.util.Optional;

Expand Down
Loading