diff --git a/src/main/java/com/example/team2_be/album/AlbumService.java b/src/main/java/com/example/team2_be/album/AlbumService.java index f5999a66..9c58a680 100644 --- a/src/main/java/com/example/team2_be/album/AlbumService.java +++ b/src/main/java/com/example/team2_be/album/AlbumService.java @@ -3,7 +3,7 @@ import com.example.team2_be.album.dto.AlbumCreateRequestDTO; import com.example.team2_be.album.dto.AlbumFindAllResponseDTO; import com.example.team2_be.album.dto.AlbumUpdaterequestDTO; -import com.example.team2_be.core.error.exception.Exception404; +import com.example.team2_be.core.error.exception.NotFoundException; import com.example.team2_be.user.User; import com.example.team2_be.user.UserJPARepository; import lombok.RequiredArgsConstructor; @@ -38,7 +38,7 @@ public Album createAlbum(AlbumCreateRequestDTO requestDTO){ @Transactional public Album updateAlbum(AlbumUpdaterequestDTO requestDTO, Long AlbumId) { Album album = albumJPARepository.findById(AlbumId) - .orElseThrow(() -> new Exception404("해당 id값을 가진 앨범을 찾을 수 없습니다. : " + AlbumId)); + .orElseThrow(() -> new NotFoundException("해당 id값을 가진 앨범을 찾을 수 없습니다. : " + AlbumId)); String updatedAlbumName = requestDTO.getAlbumName() != null ? requestDTO.getAlbumName() : album.getAlbumName(); String updatedDescription = requestDTO.getDescription() != null ? requestDTO.getDescription() : album.getDescription(); @@ -52,7 +52,7 @@ public Album updateAlbum(AlbumUpdaterequestDTO requestDTO, Long AlbumId) { // 앨범 조회 기능 public AlbumFindAllResponseDTO findAllAlbum (Long userId){ User findUser = userJPARepository.findById(userId) - .orElseThrow(() -> new Exception404("해당 유저를 찾을 수 없습니다.")); + .orElseThrow(() -> new NotFoundException("해당 유저를 찾을 수 없습니다.")); List albums = albumJPARepository.findAllByUserId(userId); diff --git a/src/main/java/com/example/team2_be/album/page/AlbumPage.java b/src/main/java/com/example/team2_be/album/page/AlbumPage.java new file mode 100644 index 00000000..48c2c47a --- /dev/null +++ b/src/main/java/com/example/team2_be/album/page/AlbumPage.java @@ -0,0 +1,22 @@ +package com.example.team2_be.album.page; + +import com.example.team2_be.BaseEntity; +import com.example.team2_be.album.Album; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.ToString; + +import javax.persistence.Entity; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; + +@Entity +@Getter +@ToString +@NoArgsConstructor +public class AlbumPage extends BaseEntity { + @ManyToOne + @JoinColumn(name = "album_id") + private Album album; +} diff --git a/src/main/java/com/example/team2_be/auth/AuthService.java b/src/main/java/com/example/team2_be/auth/AuthService.java index 683282ac..7b11150b 100644 --- a/src/main/java/com/example/team2_be/auth/AuthService.java +++ b/src/main/java/com/example/team2_be/auth/AuthService.java @@ -69,19 +69,19 @@ private KakaoTokenDTO getKakaoAccessToken(String code){ } catch (HttpStatusCodeException e){ switch (e.getStatusCode().value()){ case 400: - throw new Exception400("잘못된 요청입니다"); + throw new BadRequestException("잘못된 요청입니다"); case 401: - throw new Exception401("인증되지 않은 사용자입니다"); + throw new UnauthorizedException("인증되지 않은 사용자입니다"); case 403: - throw new Exception403("접근이 허용되지 않습니다"); + throw new ForbiddenException("접근이 허용되지 않습니다"); case 404: - throw new Exception404("해당 사용자를 찾을 수 없습니다"); + throw new NotFoundException("해당 사용자를 찾을 수 없습니다"); default: - throw new Exception500("토큰 발급 오류입니다"); + throw new InternalSeverErrorException("토큰 발급 오류입니다"); } } catch (Exception e) { - throw new Exception500("토큰 발급 오류입니다"); + throw new InternalSeverErrorException("토큰 발급 오류입니다"); } } @@ -94,19 +94,19 @@ public String kakaoLogin(String code){ } catch (HttpStatusCodeException e){ switch (e.getStatusCode().value()){ case 400: - throw new Exception400("잘못된 요청입니다"); + throw new BadRequestException("잘못된 요청입니다"); case 401: - throw new Exception401("인증되지 않은 사용자입니다"); + throw new UnauthorizedException("인증되지 않은 사용자입니다"); case 403: - throw new Exception403("접근이 허용되지 않습니다"); + throw new ForbiddenException("접근이 허용되지 않습니다"); case 404: - throw new Exception404("해당 사용자를 찾을 수 없습니다"); + throw new NotFoundException("해당 사용자를 찾을 수 없습니다"); default: - throw new Exception500("유저 정보 확인 오류입니다"); + throw new InternalSeverErrorException("유저 정보 확인 오류입니다"); } } catch (Exception e) { - throw new Exception500("유저 정보 확인 오류입니다"); + throw new InternalSeverErrorException("유저 정보 확인 오류입니다"); } User user = userService.getUser(userAccount); @@ -124,19 +124,19 @@ public String googleLogin(String code){ } catch (HttpStatusCodeException e){ switch (e.getStatusCode().value()){ case 400: - throw new Exception400("잘못된 요청입니다"); + throw new BadRequestException("잘못된 요청입니다"); case 401: - throw new Exception401("인증되지 않은 사용자입니다"); + throw new UnauthorizedException("인증되지 않은 사용자입니다"); case 403: - throw new Exception403("접근이 허용되지 않습니다"); + throw new ForbiddenException("접근이 허용되지 않습니다"); case 404: - throw new Exception404("해당 사용자를 찾을 수 없습니다"); + throw new NotFoundException("해당 사용자를 찾을 수 없습니다"); default: - throw new Exception500("유저 정보 확인 오류입니다"); + throw new InternalSeverErrorException("유저 정보 확인 오류입니다"); } } catch (Exception e) { - throw new Exception500("유저 정보 확인 오류입니다"); + throw new InternalSeverErrorException("유저 정보 확인 오류입니다"); } User user = userService.getUser(userAccount); @@ -156,19 +156,19 @@ private GoogleTokenDTO getGoogleAccessToken(String code){ } catch (HttpStatusCodeException e){ switch (e.getStatusCode().value()){ case 400: - throw new Exception400("잘못된 요청입니다"); + throw new BadRequestException("잘못된 요청입니다"); case 401: - throw new Exception401("인증되지 않은 사용자입니다"); + throw new UnauthorizedException("인증되지 않은 사용자입니다"); case 403: - throw new Exception403("접근이 허용되지 않습니다"); + throw new ForbiddenException("접근이 허용되지 않습니다"); case 404: - throw new Exception404("해당 사용자를 찾을 수 없습니다"); + throw new NotFoundException("해당 사용자를 찾을 수 없습니다"); default: - throw new Exception500("토큰 발급 오류입니다"); + throw new InternalSeverErrorException("토큰 발급 오류입니다"); } } catch (Exception e) { - throw new Exception500("토큰 발급 오류입니다"); + throw new InternalSeverErrorException("토큰 발급 오류입니다"); } } @@ -177,7 +177,7 @@ private String decoding(String code){ try { decodedCode = java.net.URLDecoder.decode(code, StandardCharsets.UTF_8.name()); } catch (UnsupportedEncodingException e) { - throw new Exception400("잘못된 요청입니다"); + throw new BadRequestException("잘못된 요청입니다"); } return decodedCode; } diff --git a/src/main/java/com/example/team2_be/trash/Trash.java b/src/main/java/com/example/team2_be/trash/Trash.java new file mode 100644 index 00000000..42e67fc3 --- /dev/null +++ b/src/main/java/com/example/team2_be/trash/Trash.java @@ -0,0 +1,39 @@ +package com.example.team2_be.trash; + +import com.example.team2_be.BaseEntity; +import com.example.team2_be.album.page.AlbumPage; +import com.example.team2_be.user.User; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.ToString; + +import javax.persistence.*; +import java.time.LocalDateTime; + +@Entity +@Getter +@ToString +@NoArgsConstructor +public class Trash extends BaseEntity { + @ManyToOne + @JoinColumn(name = "user_id",nullable = false) + private User user; + + @ManyToOne + @JoinColumn (name ="albumPage_id",nullable = false) + private AlbumPage albumPage; + + + @Column (nullable = false) + private LocalDateTime deleteAt; + + @Builder + public Trash(Long id, User user, AlbumPage albumPage) { + super(id); + this.user = user; + this.albumPage =albumPage; + // 앨범 페이지 삭제 시간 - 여기서 할지 serviced에서 할지 테스트 필요 + this.deleteAt = this.getCreateAt().plusDays(7); + } +} diff --git a/src/main/java/com/example/team2_be/trash/TrashController.java b/src/main/java/com/example/team2_be/trash/TrashController.java new file mode 100644 index 00000000..85a2ae37 --- /dev/null +++ b/src/main/java/com/example/team2_be/trash/TrashController.java @@ -0,0 +1,30 @@ +package com.example.team2_be.trash; + +import com.example.team2_be.core.utils.ApiUtils; +import com.example.team2_be.trash.dto.TrashesFindResponseDTO; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/albums/{albumId}/trashes") +public class TrashController { + + private final TrashService trashService; + + //휴지통 조회 GET + @GetMapping + public ResponseEntity> findTrashes (@PathVariable Long albumId){ + TrashesFindResponseDTO findDTO = trashService.findTrashes(albumId); + + return ResponseEntity.ok(ApiUtils.success(findDTO)); + } + + @PostMapping("/{trashId}") + public ResponseEntity> restoreTrash(@PathVariable String albumId, @PathVariable Long trashId){ + trashService.restoreTrash(trashId); + + return ResponseEntity.ok(ApiUtils.success(null)); + } +} diff --git a/src/main/java/com/example/team2_be/trash/TrashJPARepository.java b/src/main/java/com/example/team2_be/trash/TrashJPARepository.java new file mode 100644 index 00000000..6d5425a5 --- /dev/null +++ b/src/main/java/com/example/team2_be/trash/TrashJPARepository.java @@ -0,0 +1,12 @@ +package com.example.team2_be.trash; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +import java.util.List; + +public interface TrashJPARepository extends JpaRepository { + @Query("select t from Trash t where t.albumPage.album.id = :albumId") + List findAllByAlbumId(@Param("albumId") Long albumId); +} diff --git a/src/main/java/com/example/team2_be/trash/TrashService.java b/src/main/java/com/example/team2_be/trash/TrashService.java new file mode 100644 index 00000000..c3d1e96a --- /dev/null +++ b/src/main/java/com/example/team2_be/trash/TrashService.java @@ -0,0 +1,31 @@ +package com.example.team2_be.trash; + +import com.example.team2_be.core.error.exception.NotFoundException; +import com.example.team2_be.trash.dto.TrashesFindResponseDTO; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +@Service +@RequiredArgsConstructor +@Transactional(readOnly = true) +public class TrashService { + + private final TrashJPARepository trashJPARepository; + + public TrashesFindResponseDTO findTrashes(Long albumId){ + List trashes = trashJPARepository.findAllByAlbumId(albumId); + + return new TrashesFindResponseDTO(albumId, trashes); + } + + @Transactional + public void restoreTrash(Long trashId){ + Trash trash = trashJPARepository.findById(trashId) + .orElseThrow(() -> new NotFoundException("해당 페이지가 휴지통 내에 존재하지 않습니다.")); + + trashJPARepository.delete(trash); + } +} diff --git a/src/main/java/com/example/team2_be/trash/dto/TrashesFindResponseDTO.java b/src/main/java/com/example/team2_be/trash/dto/TrashesFindResponseDTO.java new file mode 100644 index 00000000..32d46515 --- /dev/null +++ b/src/main/java/com/example/team2_be/trash/dto/TrashesFindResponseDTO.java @@ -0,0 +1,40 @@ +package com.example.team2_be.trash.dto; + +import com.example.team2_be.trash.Trash; +import lombok.Getter; + +import java.time.LocalDateTime; +import java.util.List; +import java.util.stream.Collectors; + +@Getter +public class TrashesFindResponseDTO { + private Long albumId; + private List trashes; + + + public TrashesFindResponseDTO(Long albumId, List trashes){ + this.albumId = albumId; + this.trashes = trashes.stream() + .map(TrashDTO::new) + .collect((Collectors.toList())); + } + + @Getter + public static class TrashDTO{ + private Long trashId; + private String image; + private String deleter; + private LocalDateTime createAt; + private LocalDateTime deleteAt; + + public TrashDTO(Trash trash) { + this.trashId = trash.getId(); + // 휴지통 페이지 미리보기 이미지 추가 + this.deleter = trash.getUser().getNickname(); + this.createAt = trash.getCreateAt(); + this.deleteAt = trash.getDeleteAt(); + } + } +} +