Skip to content

Commit

Permalink
feat: share 기능 + error 핸들링
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeongh00 committed Nov 6, 2024
1 parent 4215da5 commit 29ae1fa
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 20 deletions.
15 changes: 0 additions & 15 deletions core/src/main/resources/application-s3.yml
Original file line number Diff line number Diff line change
@@ -1,15 +0,0 @@
# S3
cloud:
aws:
s3:
bucket: imagedata-ec2-pocket4cut
url: https://imagedata-ec2-pocket4cut.s3.ap-northeast-2.amazonaws.com/
expTime: 900000 # 15분
credentials:
access-key: AKIA5FTY6F7TUYFDUPFZ
secret-key: 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
region:
static: ap-northeast-2
auto: false
stack:
auto: false
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.pocket.domain.dto.album.*;
import com.pocket.domain.dto.user.UserInfoDTO;
import com.pocket.domain.usecase.album.*;
import jakarta.persistence.criteria.CriteriaBuilder;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
Expand All @@ -14,7 +13,7 @@
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/album")
public class AlbumController implements AlbumContollerDocs{
public class AlbumController implements AlbumContollerDocs {

private final AlbumRegisterUseCase albumRegisterUseCase;
private final AlbumLikeUseCase albumLikeUseCase;
Expand All @@ -24,6 +23,7 @@ public class AlbumController implements AlbumContollerDocs{
private final AlbumDeleteUseCase albumDeleteUseCase;
private final AlbumHashtagUseCase albumHashtagUseCase;
private final AlbumFavoriteUseCase albumFavoriteUseCase;
private final AlbumShareUseCase albumShareUseCase;

@PostMapping
public ApplicationResponse<AlbumRegisterResponseDto> postPhoto(
Expand Down Expand Up @@ -108,4 +108,26 @@ public ApplicationResponse<List<AlbumResponseDto>> getFavoriteAlbums(
return ApplicationResponse.ok(response);
}

// 공유링크 생성
@PostMapping("/share")
public ApplicationResponse<String> shareLink(
@AuthenticationPrincipal UserInfoDTO user,
@RequestParam Long albumId
) {

Long token = albumShareUseCase.saveShareTable(user.email(), albumId);
return ApplicationResponse.ok("https://pocket4cut.link/page/share?token" + token);
}

// 공유자 데이터 정보 저장
@PostMapping("/{token}/saveShare")
public ApplicationResponse<String> saveShareInfos(
@AuthenticationPrincipal UserInfoDTO user,
@PathVariable Long token
) {

albumShareUseCase.saveNewData(user.email(), token);
return ApplicationResponse.ok("Saved");
}

}
Original file line number Diff line number Diff line change
@@ -1,36 +1,47 @@
package com.pocket.outbound.adapter.album.adapter;

import com.pocket.core.aop.annotation.AdapterService;
import com.pocket.core.exception.album.AlbumCustomException;
import com.pocket.core.exception.album.AlbumErrorCode;
import com.pocket.core.exception.hashtag.HashTagCustomException;
import com.pocket.core.exception.hashtag.HashTagErrorCode;
import com.pocket.core.exception.photobooth.PhotoBoothCustomException;
import com.pocket.core.exception.photobooth.PhotoBoothErrorCode;
import com.pocket.core.exception.user.UserCustomException;
import com.pocket.core.exception.user.UserErrorCode;
import com.pocket.domain.dto.album.AlbumRegisterRequestDto;
import com.pocket.domain.dto.album.AlbumRegisterResponseDto;
import com.pocket.domain.port.album.AlbumRegisterPort;
import com.pocket.domain.port.album.AlbumSharePort;
import com.pocket.outbound.adapter.album.mapper.AlbumOutBoundMapper;
import com.pocket.outbound.entity.*;
import com.pocket.outbound.entity.JpaUser;
import com.pocket.outbound.entity.album.JpaAlbum;
import com.pocket.outbound.entity.album.JpaAlbumHashTag;
import com.pocket.outbound.entity.album.JpaAlbumShare;
import com.pocket.outbound.entity.album.JpaHashTag;
import com.pocket.outbound.entity.photobooth.JpaPhotoBooth;
import com.pocket.outbound.repository.*;
import com.pocket.outbound.repository.UserRepository;
import com.pocket.outbound.repository.album.AlbumHashTagRepository;
import com.pocket.outbound.repository.album.AlbumRepository;
import com.pocket.outbound.repository.album.AlbumShareRepository;
import com.pocket.outbound.repository.album.HashTagRepository;
import com.pocket.outbound.repository.photobooth.PhotoBoothRepository;
import lombok.RequiredArgsConstructor;

import java.util.List;

@AdapterService
@RequiredArgsConstructor
public class AlbumRegisterAdapter implements AlbumRegisterPort {
public class AlbumRegisterAdapter implements AlbumRegisterPort, AlbumSharePort {

private final AlbumRepository albumRepository;
private final HashTagRepository hashtagRepository;
private final AlbumHashTagRepository photoHashtagRepository;
private final PhotoBoothRepository photoBoothRepository;
private final UserRepository userRepository;
private final AlbumOutBoundMapper albumOutBoundMapper;
private final AlbumShareRepository albumShareRepository;
private final AlbumHashTagRepository albumHashTagRepository;

@Override
public AlbumRegisterResponseDto registerPhoto(AlbumRegisterRequestDto dto, String name) {
Expand Down Expand Up @@ -72,4 +83,53 @@ public AlbumRegisterResponseDto registerPhoto(AlbumRegisterRequestDto dto, Strin
dto.filePath()
);
}

@Override
public Long saveShareTable(String email, Long albumId) {

JpaAlbum album = albumRepository.findById(albumId).orElseThrow(() -> new AlbumCustomException(AlbumErrorCode.ALBUM_NOT_FOUND));
JpaUser jpaUser = userRepository.findByUserEmail(email)
.orElseThrow(() -> new UserCustomException(UserErrorCode.NO_USER_INFO));

JpaAlbumShare share = JpaAlbumShare.builder()
.album(album)
.user(jpaUser)
.build();

final JpaAlbumShare jpaAlbumShare = albumShareRepository.save(share);
return jpaAlbumShare.getId();
}

@Override
public void saveNewData(String email, Long token) {

JpaAlbumShare albumShare = albumShareRepository.findById(token).orElseThrow(() -> new AlbumCustomException(AlbumErrorCode.ALBUM_SHARE_NOT_FOUND));
JpaUser jpaUser = userRepository.findByUserEmail(email)
.orElseThrow(() -> new UserCustomException(UserErrorCode.NO_USER_INFO));

// 앨범 저장
JpaAlbum album = albumRepository.findById(albumShare.getAlbum().getId()).orElseThrow(() -> new AlbumCustomException(AlbumErrorCode.ALBUM_NOT_FOUND));
JpaAlbum newAlbum = JpaAlbum.builder()
.image(album.getImage())
.jpaUser(jpaUser)
.photoBooth(album.getPhotoBooth())
.isLiked(album.isLiked())
.memo(album.getMemo())
.build();
albumRepository.save(newAlbum);

// 해시태그들 저장
List<JpaAlbumHashTag> albumHashTagList = albumHashTagRepository.findByJpaAlbum_Id(album.getId());

for (JpaAlbumHashTag a : albumHashTagList) {
JpaHashTag hashTag = hashtagRepository.findById(a.getJpaHashtag().getId()).orElseThrow(() -> new HashTagCustomException(HashTagErrorCode.HASHTAG_NOT_FOUND));

JpaHashTag newTag = JpaHashTag.builder()
.hashTag(hashTag.getHashTag())
.jpaUser(jpaUser)
.build();

hashtagRepository.save(newTag);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.pocket.outbound.entity.album;

import com.pocket.outbound.entity.JpaUser;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@Table(name = "SHARE")
@Entity
@Builder
@AllArgsConstructor
public class JpaAlbumShare {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "album_share_id")
private Long id;

@ManyToOne
@JoinColumn(name = "album_id")
private JpaAlbum album;

@ManyToOne
@JoinColumn(name = "user_id")
private JpaUser user;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.pocket.outbound.repository.album;

import com.pocket.outbound.entity.album.JpaAlbumShare;
import org.springframework.data.jpa.repository.JpaRepository;

public interface AlbumShareRepository extends JpaRepository<JpaAlbumShare, Long> {
}

0 comments on commit 29ae1fa

Please sign in to comment.