Skip to content

Commit

Permalink
refactor: serviceImpl 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
YooJHyun committed Oct 8, 2024
2 parents 88093d6 + 3d68e6d commit caf53c6
Show file tree
Hide file tree
Showing 87 changed files with 2,196 additions and 1,221 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
HELP.md
.DS_Store
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
application-dev.properties

### STS ###
.apt_generated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
import com.prgrms2.java.bitta.apply.entity.Apply;
import com.prgrms2.java.bitta.jobpost.entity.JobPost;
import com.prgrms2.java.bitta.member.entity.Member;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Repository;

import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package com.prgrms2.java.bitta.apply.service;

import com.prgrms2.java.bitta.apply.dto.ApplyDTO;
import com.prgrms2.java.bitta.apply.entity.Apply;
import com.prgrms2.java.bitta.jobpost.entity.JobPost;
import com.prgrms2.java.bitta.member.entity.Member;
import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity;

import java.util.List;
import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
import com.prgrms2.java.bitta.apply.exception.ApplyException;
import com.prgrms2.java.bitta.apply.repository.ApplyRepository;
import com.prgrms2.java.bitta.jobpost.entity.JobPost;
import com.prgrms2.java.bitta.jobpost.exception.JobPostException;
import com.prgrms2.java.bitta.jobpost.repository.JobPostRepository;
import com.prgrms2.java.bitta.jobpost.util.JobPostProvider;
import com.prgrms2.java.bitta.member.dto.MemberProvider;
import com.prgrms2.java.bitta.member.entity.Member;
import com.prgrms2.java.bitta.member.service.MemberProvider;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.prgrms2.java.bitta.apply.util;

import com.prgrms2.java.bitta.apply.dto.ApplyDTO;
import com.prgrms2.java.bitta.apply.entity.Apply;
import com.prgrms2.java.bitta.apply.repository.ApplyRepository;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@

import com.prgrms2.java.bitta.feed.dto.FeedDTO;
import com.prgrms2.java.bitta.feed.service.FeedService;
import com.prgrms2.java.bitta.global.exception.AuthenticationException;
import com.prgrms2.java.bitta.global.util.AuthenticationProvider;
import com.prgrms2.java.bitta.media.dto.MediaDto;
import com.prgrms2.java.bitta.member.entity.Role;
import com.prgrms2.java.bitta.member.service.MemberProvider;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
Expand All @@ -31,8 +39,8 @@ public class FeedController {
private final FeedService feedService;

@Operation(
summary = "전체 피드 조회",
description = "전체 피드를 조회합니다.",
summary = "피드 목록 조회",
description = "페이지와 사이즈를 조건으로 피드 목록을 조회합니다.",
responses = {
@ApiResponse(
responseCode = "200",
Expand All @@ -52,11 +60,32 @@ public class FeedController {
)
}
)
@Parameters({
@Parameter(
name = "page",
description = "피드 페이지 번호",
required = true,
example = "0",
schema = @Schema(type = "integer", defaultValue = "0", minimum = "0")
),
@Parameter(
name = "size",
description = "피드 페이지 크기",
required = true,
example = "10",
schema = @Schema(type = "integer", defaultValue = "10", minimum = "1")
)
})
@GetMapping
public ResponseEntity<?> getFeed() {
public ResponseEntity<?> getFeeds(@RequestParam(required = false, defaultValue = "0", value = "page") int page
, @RequestParam(required = false, defaultValue = "10", value = "size") int size
, @RequestParam(required = false, value = "username") String username
, @RequestParam(required = false, value = "title") String title) {
Pageable pageable = PageRequest.of(page, size);

return ResponseEntity.ok(
Map.of("message", "피드를 성공적으로 조회했습니다.", "result", feedService.readAll())
);
Map.of("message", "피드를 성공적으로 조회했습니다."
, "result", feedService.readAll(pageable, username, title)));
}

@Operation(
Expand Down Expand Up @@ -204,10 +233,14 @@ public ResponseEntity<?> createFeed(@RequestPart(value = "feed") @Valid FeedDTO
)
@PutMapping(value = "/{id}", consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE})
public ResponseEntity<?> modifyFeed(@PathVariable("id") @Min(1) Long id, @RequestPart("feed") @Valid FeedDTO feedDTO
, @RequestPart("filesToUpload") List<MultipartFile> filesToUpload, @RequestPart("filepathsToDelete") List<String> filepathsToDelete) {
, @RequestPart("filesToUpload") List<MultipartFile> filesToUpload, @RequestPart("filesToDelete") List<MediaDto> filesToDelete) {
if (!checkPermission(id)) {
throw AuthenticationException.CANNOT_ACCESS.get();
}

feedDTO.setId(id);

feedService.update(feedDTO, filesToUpload, filepathsToDelete);
feedService.update(feedDTO, filesToUpload, filesToDelete);

return ResponseEntity.ok().body(Map.of("message", "피드가 수정되었습니다."));
}
Expand Down Expand Up @@ -251,9 +284,21 @@ public ResponseEntity<?> modifyFeed(@PathVariable("id") @Min(1) Long id, @Reques
)
@DeleteMapping("/{id}")
public ResponseEntity<?> deleteFeed(@PathVariable("id") @Min(1) Long id) {
if (!checkPermission(id)) {
throw AuthenticationException.CANNOT_ACCESS.get();
}

feedService.delete(id);

return ResponseEntity.ok().body(Map.of("message", "피드가 삭제되었습니다."));
}

private boolean checkPermission(Long id) {
if (AuthenticationProvider.getRoles() == Role.ADMIN) {
return true;
}

return feedService.checkAuthority(id, AuthenticationProvider.getUsername());
}
}

17 changes: 7 additions & 10 deletions src/main/java/com/prgrms2/java/bitta/feed/dto/FeedDTO.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.prgrms2.java.bitta.feed.dto;

import com.prgrms2.java.bitta.media.dto.MediaDto;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.*;
import lombok.AllArgsConstructor;
Expand All @@ -17,11 +18,11 @@
@Schema(title = "피드 DTO", description = "피드의 요청 및 응답에 사용하는 DTO입니다.")
public class FeedDTO {
@Schema(title = "피드 ID (PK)", description = "피드의 고유 ID 입니다.", example = "1", minimum = "1")
@Min(value = 1, message = "ID는 음수가 될 수 없습니다.")
@Min(value = 1, message = "ID는 0 또는 음수가 될 수 없습니다.")
private Long id;

@Schema(title = "피드 제목", description = "피드 제목입니다.", example = "Feed Title", minimum = "1", maximum = "50")
@NotBlank(message = "제목은 비워둘 수 없습니다.")
@NotBlank(message = "제목은 비우거나, 공백이 될 수 없습니다.")
@Size(min = 1, max = 50, message = "제목은 1 ~ 50자 이하여야 합니다.")
private String title;

Expand All @@ -31,17 +32,13 @@ public class FeedDTO {
private String content = "";

@Schema(title = "회원 ID (FK)", description = "회원의 고유 ID 입니다.", example = "1", minimum = "1")
@Min(value = 1, message = "ID는 음수가 될 수 없습니다.")
@Min(value = 1, message = "ID는 0 또는 음수가 될 수 없습니다.")
@NotNull(message = "회원 ID는 누락될 수 없습니다.")
private Long memberId;

@Schema(title = "피드 생성일시", description = "피드가 생성된 날짜 및 시간입니다.", example = "2023-09-24T14:45:00")
@PastOrPresent(message = "생성일자는 현재 시점 혹은 이전이어야 합니다.")
private LocalDateTime createdAt;

@Schema(title = "사진 URL 목록", description = "피드에 포함된 사진 URL 목록입니다.", example = "[\"IMAGE_URL_1\", \"IMAGE_URL_2\"]")
private List<String> photoUrls;

@Schema(title = "영상 URL 목록", description = "피드에 포함된 영상 URL 목록입니다.", example = "[\"VIDEO_URL_1\", \"VIDEO_URL_2\"]")
private List<String> videoUrls;
}
@Schema(title = "미디어 파일 목록", description = "피드에 포함된 사진 및 영상 목록입니다.")
private List<MediaDto> medias;
}
24 changes: 4 additions & 20 deletions src/main/java/com/prgrms2/java/bitta/feed/entity/Feed.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.prgrms2.java.bitta.feed.entity;

import com.prgrms2.java.bitta.media.entity.Media;
import com.prgrms2.java.bitta.member.entity.Member;
import com.prgrms2.java.bitta.photo.entity.Photo;
import com.prgrms2.java.bitta.video.entity.Video;
import jakarta.persistence.*;
import lombok.*;
import org.springframework.data.annotation.CreatedDate;
Expand Down Expand Up @@ -41,24 +40,9 @@ public class Feed {

@Builder.Default
@OneToMany(mappedBy = "feed", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Photo> photos = new ArrayList<>();
private List<Media> medias = new ArrayList<>();

@Builder.Default
@OneToMany(mappedBy = "feed", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Video> videos = new ArrayList<>();

public void addPhoto(Photo photo) {
photos.add(photo);
photo.setFeed(this);
}

public void addVideo(Video video) {
videos.add(video);
video.setFeed(this);
}

public void clearFiles() {
photos.clear();
videos.clear();
public void clearMedias() {
medias.clear();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.prgrms2.java.bitta.feed.exception;


import lombok.AllArgsConstructor;
import lombok.Getter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.prgrms2.java.bitta.feed.entity.Feed;
import com.prgrms2.java.bitta.member.entity.Member;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -12,10 +14,24 @@

@Repository
public interface FeedRepository extends JpaRepository<Feed, Long> {
@Query("SELECT f FROM Feed f WHERE f.member = :member")
List<Feed> findAllByMember(@Param("member") Member member);
@Query("SELECT f FROM Feed f WHERE f.member.username LIKE %:username%")
Page<Feed> findAllLikeUsernameOrderByIdDesc(@Param("username") String username, Pageable pageable);

@Query("SELECT f FROM Feed f WHERE f.title LIKE %:title%")
Page<Feed> findAllLikeTitleOrderByIdDesc(@Param("title") String title, Pageable pageable);

@Query("SELECT f FROM Feed f WHERE f.member.username LIKE %:username% AND f.title LIKE %:title%")
Page<Feed> findAllLikeUsernameAndTitleOrderByIdDesc(@Param("username") String username, @Param("title") String title, Pageable pageable);

Page<Feed> findAllByOrderByIdDesc(Pageable pageable);

@Modifying
@Query("DELETE FROM Feed f WHERE f.id = :id")
Long deleteByIdAndReturnCount(@Param("id") Long id);
int deleteByIdAndReturnCount(@Param("id") Long id);

//////////////////////////
@Query(value = "SELECT * FROM feed ORDER BY RAND() LIMIT :limit", nativeQuery = true)
List<Feed> findRandomFeeds(@Param("limit") int limit);

boolean existsByIdAndMember_Username(Long feedId, String username);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.prgrms2.java.bitta.feed.service;

import com.prgrms2.java.bitta.feed.entity.Feed;
import com.prgrms2.java.bitta.feed.repository.FeedRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class FeedProvider {
private final FeedRepository feedRepository;

@Transactional(readOnly = true)
public Feed getById(Long id) {
return feedRepository.findById(id).orElse(null);
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
package com.prgrms2.java.bitta.feed.service;

import com.prgrms2.java.bitta.feed.dto.FeedDTO;
import com.prgrms2.java.bitta.member.entity.Member;
import com.prgrms2.java.bitta.media.dto.MediaDto;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

public interface FeedService {
FeedDTO read(Long id);

List<FeedDTO> readAll();

List<FeedDTO> readAll(Member member);
Page<FeedDTO> readAll(Pageable pageable, String username, String title);

void insert(FeedDTO feedDto, List<MultipartFile> files);

void update(FeedDTO feedDto, List<MultipartFile> filesToUpload, List<String> filepathsToDelete);
void update(FeedDTO feedDto, List<MultipartFile> filesToUpload, List<MediaDto> filesToDelete);

void delete(Long id);

List<FeedDTO> readRandomFeeds(int limit);

boolean checkAuthority(Long feedId, String memberId);
}
Loading

0 comments on commit caf53c6

Please sign in to comment.