-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…go-api [Feat] 배너를 업로드하고 반환하는 기능을 추가했어요.
- Loading branch information
Showing
20 changed files
with
626 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/java/team7/inplace/admin/application/BannerService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package team7.inplace.admin.application; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import team7.inplace.admin.application.command.BannerCommand; | ||
import team7.inplace.admin.application.dto.BannerInfo; | ||
import team7.inplace.admin.application.dto.BannerInfo.Detail; | ||
import team7.inplace.admin.persistence.BannerRepository; | ||
import team7.inplace.admin.persistence.BannerS3Repository; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class BannerService { | ||
private final BannerS3Repository bannerS3Repository; | ||
private final BannerRepository bannerRepository; | ||
|
||
public void uploadBanner(BannerCommand.Create command) { | ||
var imgPath = bannerS3Repository.uploadBanner(command.imgName(), 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(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/team7/inplace/admin/application/command/BannerCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package team7.inplace.admin.application.command; | ||
|
||
import java.time.LocalDateTime; | ||
import org.springframework.web.multipart.MultipartFile; | ||
import team7.inplace.admin.domain.Banner; | ||
|
||
public class BannerCommand { | ||
public record Create( | ||
String imgName, | ||
MultipartFile imageFile, | ||
LocalDateTime startDate, | ||
LocalDateTime endDate, | ||
Boolean isFixed | ||
) { | ||
public Banner toEntity(String imgPath) { | ||
return Banner.of(imgName, imgPath, startDate, endDate, isFixed); | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/team7/inplace/admin/application/dto/BannerInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package team7.inplace.admin.application.dto; | ||
|
||
import team7.inplace.admin.domain.Banner; | ||
|
||
public class BannerInfo { | ||
public record Detail( | ||
Long id, | ||
String imageUrl | ||
) { | ||
public static Detail from(Banner banner) { | ||
return new Detail(banner.getId(), banner.getImgPath()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package team7.inplace.admin.domain; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import java.time.LocalDateTime; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Banner { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
private String imgName; | ||
private String imgPath; | ||
private LocalDateTime startDate; | ||
private LocalDateTime endDate; | ||
private Boolean isFixed; | ||
|
||
private Banner(String imgName, String imgPath, LocalDateTime startDate, LocalDateTime endDate, Boolean isFixed) { | ||
this.imgName = imgName; | ||
this.imgPath = imgPath; | ||
this.startDate = startDate; | ||
this.endDate = endDate; | ||
this.isFixed = isFixed; | ||
} | ||
|
||
public static Banner of(String imgName, String imgPath, LocalDateTime startDate, LocalDateTime endDate, | ||
Boolean isFixed) { | ||
return new Banner(imgName, imgPath, startDate, endDate, isFixed); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/team7/inplace/admin/persistence/BannerRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package team7.inplace.admin.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; | ||
|
||
@Repository | ||
public interface BannerRepository extends JpaRepository<Banner, Long> { | ||
@Query("SELECT l FROM Banner l WHERE l.startDate <= :now AND l.endDate >= :now or l.isFixed = true") | ||
List<Banner> findActiveBanner(@Param("now") LocalDateTime now); | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/team7/inplace/admin/persistence/BannerS3Repository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package team7.inplace.admin.persistence; | ||
|
||
import com.amazonaws.services.s3.AmazonS3Client; | ||
import com.amazonaws.services.s3.model.ObjectMetadata; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Repository; | ||
import org.springframework.web.multipart.MultipartFile; | ||
import team7.inplace.infra.s3.AwsProperties; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class BannerS3Repository { | ||
private final AmazonS3Client amazonS3Client; | ||
private final AwsProperties awsProperties; | ||
|
||
public String uploadBanner(String bannerName, MultipartFile banner) { | ||
var bucketName = awsProperties.bucketName(); | ||
var key = "banner/" + bannerName; | ||
|
||
ObjectMetadata metadata = new ObjectMetadata(); | ||
metadata.setContentLength(banner.getSize()); | ||
metadata.setContentType(banner.getContentType()); | ||
|
||
try { | ||
amazonS3Client.putObject(bucketName, key, banner.getInputStream(), metadata); | ||
return amazonS3Client.getUrl(bucketName, key).toString(); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Failed to upload banner", e); | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/team7/inplace/admin/presentation/BannerController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package team7.inplace.admin.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.GetMapping; | ||
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; | ||
|
||
@RestController | ||
@Slf4j | ||
@RequestMapping("/banner") | ||
@RequiredArgsConstructor | ||
public class BannerController { | ||
private final BannerService bannerService; | ||
|
||
@PostMapping() | ||
public void saveBanner(BannerRequest.Create request) { | ||
|
||
bannerService.uploadBanner(request.toCommand()); | ||
} | ||
|
||
@GetMapping() | ||
public ResponseEntity<List<BannerResponse.Info>> getBanners() { | ||
var banners = bannerService.getBanners(); | ||
|
||
var response = banners.stream() | ||
.map(BannerResponse.Info::from) | ||
.toList(); | ||
return new ResponseEntity<>(response, HttpStatus.OK); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/team7/inplace/admin/presentation/BannerRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package team7.inplace.admin.presentation; | ||
|
||
import java.time.LocalDate; | ||
import org.springframework.web.multipart.MultipartFile; | ||
import team7.inplace.admin.application.command.BannerCommand; | ||
|
||
public class BannerRequest { | ||
public record Create( | ||
String imageName, | ||
MultipartFile imageFile, | ||
LocalDate startDate, | ||
LocalDate endDate, | ||
Boolean isFixed | ||
) { | ||
public BannerCommand.Create toCommand() { | ||
return new BannerCommand.Create( | ||
imageName, | ||
imageFile, | ||
startDate.atStartOfDay(), | ||
endDate.atStartOfDay(), | ||
isFixed | ||
); | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/team7/inplace/admin/presentation/BannerResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package team7.inplace.admin.presentation; | ||
|
||
import team7.inplace.admin.application.dto.BannerInfo; | ||
|
||
public class BannerResponse { | ||
public record Info( | ||
Long id, | ||
String imageUrl | ||
) { | ||
public static Info from(BannerInfo.Detail banner) { | ||
return new Info(banner.id(), banner.imageUrl().replace("https://", "http://")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package team7.inplace.infra.s3; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
@ConfigurationProperties(prefix = "aws") | ||
public record AwsProperties( | ||
String accessKey, | ||
String secretKey, | ||
String region, | ||
String bucketName | ||
) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package team7.inplace.infra.s3; | ||
|
||
import com.amazonaws.auth.AWSStaticCredentialsProvider; | ||
import com.amazonaws.auth.BasicAWSCredentials; | ||
import com.amazonaws.services.s3.AmazonS3Client; | ||
import com.amazonaws.services.s3.AmazonS3ClientBuilder; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class S3Config { | ||
private final AwsProperties awsProperties; | ||
|
||
@Bean | ||
public AmazonS3Client amazonS3Client() { | ||
BasicAWSCredentials awsCredentials = new BasicAWSCredentials(awsProperties.accessKey(), | ||
awsProperties.secretKey()); | ||
|
||
return (AmazonS3Client) AmazonS3ClientBuilder.standard() | ||
.withRegion(awsProperties.region()) | ||
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials)) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
aws: | ||
accessKey: ${AWS_ACCESS_KEY} | ||
secretKey: ${AWS_SECRET_KEY} | ||
region: ${AWS_REGION} | ||
bucket_name: ${AWS_BUCKET_NAME} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.