Skip to content

Commit

Permalink
Merge pull request #141 from Next-Room/feature/image-upload
Browse files Browse the repository at this point in the history
[FEAT] ๊ตฌ๋… ์ƒํƒœ ๊ฒ€์ฆ ๋กœ์ง ์ถ”๊ฐ€
  • Loading branch information
eunsol-an authored Oct 13, 2024
2 parents fcdb171 + f45291c commit 118ebcb
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 17 deletions.
19 changes: 19 additions & 0 deletions src/main/java/com/nextroom/nextRoomServer/domain/Shop.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.nextroom.nextRoomServer.enums.UserStatus;
import com.nextroom.nextRoomServer.exceptions.CustomException;
import com.nextroom.nextRoomServer.security.SecurityUtil;
import org.hibernate.annotations.Comment;

import com.nextroom.nextRoomServer.util.Timestamped;
Expand All @@ -23,6 +27,9 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import static com.nextroom.nextRoomServer.exceptions.StatusCode.NOT_PERMITTED;
import static com.nextroom.nextRoomServer.exceptions.StatusCode.SUBSCRIPTION_NOT_PERMITTED;

@Entity
@Builder
@Getter
Expand Down Expand Up @@ -75,4 +82,16 @@ public class Shop extends Timestamped {
public void updateLastLoginAt() {
this.lastLoginAt = LocalDateTime.now();
}

public void checkAuthorized() {
if (!Objects.equals(this.id, SecurityUtil.getCurrentShopId())) {
throw new CustomException(NOT_PERMITTED);
}
}

public void validateSubscription() {
if (this.subscription == null || this.subscription.getStatus() != UserStatus.SUBSCRIPTION) {
throw new CustomException(SUBSCRIPTION_NOT_PERMITTED);
}
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/nextroom/nextRoomServer/dto/HintDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ public static class AddHintRequest {
private final int progress;
private final List<String> hintImageList;
private final List<String> answerImageList;

public boolean hasImages() {
boolean hasHintImages = this.hintImageList != null && !this.hintImageList.isEmpty();
boolean hasAnswerImages = this.answerImageList != null && !this.answerImageList.isEmpty();

return hasHintImages || hasAnswerImages;
}
}

@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ public static class SubscriptionInfoResponse {
public SubscriptionInfoResponse(Subscription subscription) {
this.id = subscription.getId();
this.name = subscription.getShop().getName();
this.status = UserStatus.SUBSCRIPTION_EXPIRATION.equals(subscription.getStatus()) ?
UserStatus.FREE : subscription.getStatus();
this.status = subscription.getStatus();
this.startDate = subscription.getStartDate();
this.expiryDate = subscription.getExpiryDate();
this.createdAt = Timestamped.dateTimeFormatter(subscription.getCreatedAt());
Expand All @@ -72,8 +71,7 @@ public static class UserStatusResponse {

public UserStatusResponse(Subscription subscription) {
this.id = subscription.getId();
this.status = UserStatus.SUBSCRIPTION_EXPIRATION.equals(subscription.getStatus()) ?
UserStatus.FREE : subscription.getStatus();
this.status = subscription.getStatus();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public enum StatusCode {
* 403 FORBIDDEN
*/
NOT_PERMITTED(HttpStatus.FORBIDDEN, "์ ‘๊ทผ ๊ถŒํ•œ์ด ์—†์Šต๋‹ˆ๋‹ค."),
SUBSCRIPTION_NOT_PERMITTED(HttpStatus.FORBIDDEN, "๊ตฌ๋… ๊ถŒํ•œ์ด ์—†์Šต๋‹ˆ๋‹ค."),


/**
* 404 Not Found
Expand Down
30 changes: 17 additions & 13 deletions src/main/java/com/nextroom/nextRoomServer/service/HintService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import static com.nextroom.nextRoomServer.exceptions.StatusCode.*;

import java.util.List;
import java.util.Objects;

import com.nextroom.nextRoomServer.domain.Shop;
import com.nextroom.nextRoomServer.util.aws.S3Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -15,7 +15,6 @@
import com.nextroom.nextRoomServer.exceptions.CustomException;
import com.nextroom.nextRoomServer.repository.HintRepository;
import com.nextroom.nextRoomServer.repository.ThemeRepository;
import com.nextroom.nextRoomServer.security.SecurityUtil;

import lombok.RequiredArgsConstructor;

Expand All @@ -32,10 +31,13 @@ public class HintService {

@Transactional
public HintDto.UrlResponse getPresignedUrl(HintDto.UrlRequest request) {
Long themeId = request.getThemeId();
this.validateThemeAndShop(themeId);
Theme theme = this.validateThemeAndShop(request.getThemeId());
Shop shop = theme.getShop();

shop.validateSubscription();

Long shopId = SecurityUtil.getCurrentShopId();
Long themeId = theme.getId();
Long shopId = shop.getId();
List<String> hintImageUrlList = s3Component.generatePresignedUrlsForUpload(shopId, themeId, TYPE_HINT, request.getHintImageCount());
List<String> answerImageUrlList = s3Component.generatePresignedUrlsForUpload(shopId, themeId, TYPE_ANSWER, request.getAnswerImageCount());

Expand All @@ -45,6 +47,7 @@ public HintDto.UrlResponse getPresignedUrl(HintDto.UrlRequest request) {
@Transactional
public void addHint(HintDto.AddHintRequest request) {
Theme theme = this.validateThemeAndShop(request.getThemeId());
this.validateSubscriptionWithImageRequest(theme.getShop(), request);
this.validateHintCodeConflict(theme, request.getHintCode());

Hint hint = Hint.builder()
Expand All @@ -62,11 +65,11 @@ public void addHint(HintDto.AddHintRequest request) {

@Transactional(readOnly = true)
public List<HintDto.HintListResponse> getHintList(Long themeId) {
this.validateThemeAndShop(themeId);
Theme theme = this.validateThemeAndShop(themeId);

List<Hint> hints = hintRepository.findAllByThemeIdOrderByProgress(themeId);

Long shopId = SecurityUtil.getCurrentShopId();
Long shopId = theme.getShop().getId();
return hints.stream().map(hint ->
new HintDto.HintListResponse(
hint,
Expand Down Expand Up @@ -95,23 +98,24 @@ public void removeHint(HintDto.RemoveHintRequest request) {
}

private void deleteHintAndAnswerImages(Hint hint) {
Long shopId = SecurityUtil.getCurrentShopId();
Long shopId = hint.getTheme().getShop().getId();
Long themeId = hint.getTheme().getId();

s3Component.deleteObjects(shopId, themeId, TYPE_HINT, hint.getHintImageList());
s3Component.deleteObjects(shopId, themeId, TYPE_ANSWER, hint.getAnswerImageList());
}

private void checkShopAuthorization(Long shopId) {
if (!Objects.equals(shopId, SecurityUtil.getCurrentShopId())) {
throw new CustomException(NOT_PERMITTED);
private void validateSubscriptionWithImageRequest(Shop shop, HintDto.AddHintRequest request) {
if (request.hasImages()) {
shop.validateSubscription();
}
}

private Theme validateThemeAndShop(Long themeId) {
Theme theme = themeRepository.findById(themeId)
.orElseThrow(() -> new CustomException(THEME_NOT_FOUND));

this.checkShopAuthorization(theme.getShop().getId());
theme.getShop().checkAuthorized();

return theme;
}
Expand All @@ -120,7 +124,7 @@ private Hint validateHintAndShop(Long hintId) {
Hint hint = hintRepository.findById(hintId)
.orElseThrow(() -> new CustomException(HINT_NOT_FOUND));

checkShopAuthorization(hint.getTheme().getShop().getId());
hint.getTheme().getShop().checkAuthorized();

return hint;
}
Expand Down

0 comments on commit 118ebcb

Please sign in to comment.