Skip to content
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

Merged
merged 22 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
7b64066
feat: AlbumPage 엔티티 임시 생성
won0104 Oct 18, 2023
23f6ed2
feat: Trashs 엔티티 생성
won0104 Oct 18, 2023
c4cf4dd
refactor: 주석 추가
won0104 Oct 18, 2023
4b68fc3
feat: Trashs 컨트롤러 생성
won0104 Oct 18, 2023
d12643a
feat: 휴지통 조회 메서드 생성
won0104 Oct 18, 2023
7b2a9df
feat: TrashsFindResponseDTO 디티오 생성
won0104 Oct 18, 2023
ebffb32
refactor: trashs -> trash로 변경
won0104 Oct 18, 2023
a2bfebc
refactor: trashService 파일 생성
won0104 Oct 20, 2023
4a1acf2
refactor: 변수 명 수정
Choi-Jungbin Oct 20, 2023
0588c3e
feat: 휴지통 조회 응답 DTO 수정
Choi-Jungbin Oct 20, 2023
af22c89
feat: 휴지통 레포지토리 작성
Choi-Jungbin Oct 21, 2023
e7ee6bb
feat: #52 휴지통 조회 메서드 작성
Choi-Jungbin Oct 21, 2023
26771e6
feat: #52 휴지통 조회 컨트롤러 메서드 작성
Choi-Jungbin Oct 21, 2023
198a75d
feat: #52 앨범 페이지 복구 메서드 작성
Choi-Jungbin Oct 21, 2023
24f4af5
feat: #52 앨범 페이지 복구 컨트롤러 메서드 작성
Choi-Jungbin Oct 21, 2023
4ab858a
refactor: 휴지통 서비스 이름 수정
Choi-Jungbin Oct 21, 2023
778227d
refactor: 휴지통 조회 메서드 이름 수정
Choi-Jungbin Oct 21, 2023
6049e6c
refactor: #52 휴지통 이미지 설정 수정
Choi-Jungbin Oct 21, 2023
c724ac6
refactor: #52 테이블 네임 삭제
Choi-Jungbin Oct 22, 2023
b857b02
refactor: #52 엔트 포인트 수정
Choi-Jungbin Oct 23, 2023
69bc59d
refactor: #52 변수 명 통일
Choi-Jungbin Oct 23, 2023
5b66aea
refactor: #52 트랜잭션 처리
Choi-Jungbin Oct 23, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
Copy link
Contributor

@MinseokGo MinseokGo Oct 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

작성하신 대로 trash의 복수형이 trashs인데 trashs에서 명세를 바꾸는 것이 좋을듯 합니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

잘 이해가 안가서 그러는데 trashs로 명세를 다 바꾸는 것이 좋다는 의미인가요?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Contributor

@MinseokGo MinseokGo Oct 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분도 Spring Data JPA 라이브러리가 제공하는 쿼리 메소드를 활용하면 좋을듯 합니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jpa 기본 메서드가 findAllByAlbumPage_Album_Id라 findAllByAlbumId으로 직접 쿼리를 적는 것이 직관적이라 생각했습니다. 둘의 성능 차이는 없는데 기본 메서드로 변경하는 것이 좋을까요?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

휴지통이 가지는 앨범 페이지의 앨범 id를 참조하네요

쿼리(@query)로 작성하는 것이 더 가독성이 좋아보입니다.

좋은 고민인 거 같아요!

}
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){
Copy link
Contributor

Choose a reason for hiding this comment

The 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();
}
}
}