Skip to content

Commit

Permalink
Merge pull request #53 from Step3-kakao-tech-campus/feat/trashs
Browse files Browse the repository at this point in the history
feat: 휴지통 관련 기능 구현
  • Loading branch information
Choi-Jungbin authored Oct 23, 2023
2 parents 2e89bdc + 5b66aea commit 52bb647
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/main/java/com/example/team2_be/album/page/AlbumPage.java
Original file line number Diff line number Diff line change
@@ -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;
}
39 changes: 39 additions & 0 deletions src/main/java/com/example/team2_be/trash/Trash.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
30 changes: 30 additions & 0 deletions src/main/java/com/example/team2_be/trash/TrashController.java
Original file line number Diff line number Diff line change
@@ -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<ApiUtils.ApiResult<TrashesFindResponseDTO>> findTrashes (@PathVariable Long albumId){
TrashesFindResponseDTO findDTO = trashService.findTrashes(albumId);

return ResponseEntity.ok(ApiUtils.success(findDTO));
}

@PostMapping("/{trashId}")
public ResponseEntity<ApiUtils.ApiResult<Void>> restoreTrash(@PathVariable String albumId, @PathVariable Long trashId){
trashService.restoreTrash(trashId);

return ResponseEntity.ok(ApiUtils.success(null));
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/example/team2_be/trash/TrashJPARepository.java
Original file line number Diff line number Diff line change
@@ -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<Trash, Long> {
@Query("select t from Trash t where t.albumPage.album.id = :albumId")
List<Trash> findAllByAlbumId(@Param("albumId") Long albumId);
}
31 changes: 31 additions & 0 deletions src/main/java/com/example/team2_be/trash/TrashService.java
Original file line number Diff line number Diff line change
@@ -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<Trash> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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<TrashDTO> trashes;


public TrashesFindResponseDTO(Long albumId, List<Trash> 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();
}
}
}

0 comments on commit 52bb647

Please sign in to comment.