-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: 휴지통 관련 기능 구현 #53
Changes from all commits
7b64066
23f6ed2
c4cf4dd
4b68fc3
d12643a
7b2a9df
ebffb32
a2bfebc
4a1acf2
0588c3e
af22c89
e7ee6bb
26771e6
198a75d
24f4af5
4ab858a
778227d
6049e6c
c724ac6
b857b02
69bc59d
5b66aea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
} |
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); | ||
} | ||
} |
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)); | ||
} | ||
} |
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 부분도 Spring Data JPA 라이브러리가 제공하는 쿼리 메소드를 활용하면 좋을듯 합니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. jpa 기본 메서드가 findAllByAlbumPage_Album_Id라 findAllByAlbumId으로 직접 쿼리를 적는 것이 직관적이라 생각했습니다. 둘의 성능 차이는 없는데 기본 메서드로 변경하는 것이 좋을까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} |
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){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 트랜잭션 처리를 해야하지 않을까요! |
||
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(); | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
작성하신 대로 trash의 복수형이 trashs인데 trashs에서 명세를 바꾸는 것이 좋을듯 합니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
잘 이해가 안가서 그러는데 trashs로 명세를 다 바꾸는 것이 좋다는 의미인가요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
trashes 로 바뀌어야 하지 않을까 라는 의견입니다..ㅎㅎ