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

HF-109, HF-110: 게시글 dto 정보 수정, 자신이 작성한 게시글 불러오기, 이벤트 지역 추가 #22

Merged
merged 8 commits into from
Jul 29, 2024
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package gible.domain.donation.controller;

import gible.domain.donation.dto.DonationPostInfoRes;
import gible.domain.donation.dto.DonationReq;
import gible.domain.donation.dto.DonationSenderInfoRes;
import gible.domain.donation.service.DonationService;
import gible.global.common.response.SuccessRes;
import gible.domain.security.common.SecurityUserDetails;
import gible.global.common.response.SuccessRes;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@RequiredArgsConstructor
Expand All @@ -33,24 +30,24 @@ public ResponseEntity<?> donate(@Valid @RequestBody DonationReq donationReq,

/* 게시글에 대한 기부자 목록 불러오기 */
@GetMapping("/post/{postId}/donators")
public List<DonationSenderInfoRes> getDonorsForPost(@PathVariable UUID postId) {
public ResponseEntity<?> getDonorsForPost(@PathVariable UUID postId) {

return donationService.getDonorsForPost(postId);
return ResponseEntity.ok().body(donationService.getDonorsForPost(postId));
}

/* 기부한 게시글에 대한 정보 불러오기 */
@GetMapping("/donation/my-donation")
public List<DonationPostInfoRes> getPostDonationDetails(
public ResponseEntity<?> getPostDonationDetails(
@AuthenticationPrincipal SecurityUserDetails userDetails) {

return donationService.getPostDonationDetails(userDetails.getId());
return ResponseEntity.ok().body(donationService.getPostDonationDetails(userDetails.getId()));
}

/* 기부해준 사람들의 목록 불러오기 */
@GetMapping("/donation/received-donation")
public List<DonationSenderInfoRes> getDonorsList(
public ResponseEntity<?> getDonorsList(
@AuthenticationPrincipal SecurityUserDetails userDetails) {

return donationService.getDonorsList(userDetails.getId());
return ResponseEntity.ok().body(donationService.getDonorsList(userDetails.getId()));
}
}
17 changes: 9 additions & 8 deletions src/main/java/gible/domain/event/controller/EventController.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package gible.domain.event.controller;

import gible.domain.event.dto.EventDetailRes;
import gible.domain.event.dto.EventReq;
import gible.domain.event.dto.EventSummaryRes;
import gible.domain.event.entity.Region;
import gible.domain.event.service.EventService;
import gible.global.common.response.SuccessRes;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
Expand All @@ -34,17 +32,20 @@ public ResponseEntity<?> saveEvent(@Valid @RequestBody EventReq eventReq) {

/* 이벤트 목록 조회 */
@GetMapping
public Page<EventSummaryRes> getAllEvents(
@PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable) {
public ResponseEntity<?> getAllEvents(
@PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable,
@RequestParam(name = "region", required = false) Region region) {

return eventService.getAllEvents(pageable);
if (region == null)
return ResponseEntity.ok().body(eventService.getAllEvents(pageable));
return ResponseEntity.ok().body(eventService.getAllEventsByRegion(region, pageable));
}

/* 특정 이벤트 조회 */
@GetMapping("/{eventId}")
public EventDetailRes getEvent(@PathVariable UUID eventId) {
public ResponseEntity<?> getEvent(@PathVariable UUID eventId) {

return eventService.getEvent(eventId);
return ResponseEntity.ok().body(eventService.getEvent(eventId));
}

/* 이벤트 수정 */
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/gible/domain/event/entity/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public class Event {
@NotNull
private String content;

@Enumerated(EnumType.STRING)
private Region region;

@Column(name = "image_url")
private String imageUrl;

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/gible/domain/event/entity/Region.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package gible.domain.event.entity;

import lombok.Getter;

@Getter
public enum Region {
SEOUL, BUSAN, DAEGU_GYEONGBUK, INCHEON, GWANGJU_JEONNAM, ULSAN,
GYEONGGI, GANGWON, CHUNGBUK, CHUNGNAM, JEONBUK, GYEONGNAM, JEJU
}

Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package gible.domain.event.repository;

import gible.domain.event.entity.Event;
import gible.domain.event.entity.Region;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.UUID;

public interface EventRepository extends JpaRepository<Event, UUID> {

Page<Event> findByRegion(Region region);
}
9 changes: 9 additions & 0 deletions src/main/java/gible/domain/event/service/EventService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import gible.domain.event.dto.EventReq;
import gible.domain.event.dto.EventSummaryRes;
import gible.domain.event.entity.Event;
import gible.domain.event.entity.Region;
import gible.domain.event.repository.EventRepository;
import gible.exception.CustomException;
import gible.exception.error.ErrorType;
Expand Down Expand Up @@ -36,6 +37,14 @@ public Page<EventSummaryRes> getAllEvents(Pageable pageable) {
return events.map(EventSummaryRes::fromEntity);
}

/* 이벤트 목록 조회 (지역별) */
@Transactional(readOnly = true)
public Page<EventSummaryRes> getAllEventsByRegion(Region region, Pageable pageable) {

Page<Event> events = eventRepository.findByRegion(region);
return events.map(EventSummaryRes::fromEntity);
}

/* 특정 이벤트 조회 */
@Transactional(readOnly = true)
public EventDetailRes getEvent(UUID eventId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package gible.domain.participate.controller;

import gible.domain.participate.dto.ParticipationEventRes;
import gible.domain.participate.service.ParticipateService;
import gible.domain.security.common.SecurityUserDetails;
import gible.global.common.response.SuccessRes;
Expand All @@ -12,7 +11,6 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

@RequiredArgsConstructor
Expand All @@ -32,10 +30,10 @@ public ResponseEntity<?> participationEvent(@AuthenticationPrincipal SecurityUse

/* 사용자가 참여한 이벤트 목록 조회하기 */
@GetMapping("/user/participation-event")
public List<ParticipationEventRes> getAllParticipationEvents(
public ResponseEntity<?> getAllParticipationEvents(
@AuthenticationPrincipal SecurityUserDetails userDetails) {

return participateService.getAllParticipationEvents(userDetails.getId());
return ResponseEntity.ok().body(participateService.getAllParticipationEvents(userDetails.getId()));
}
}

13 changes: 5 additions & 8 deletions src/main/java/gible/domain/post/controller/PostController.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package gible.domain.post.controller;

import gible.domain.post.dto.PostDetailRes;
import gible.domain.post.dto.PostReq;
import gible.domain.post.dto.PostSummaryRes;
import gible.domain.post.service.PostService;
import gible.domain.security.common.SecurityUserDetails;
import gible.global.common.response.SuccessRes;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
Expand Down Expand Up @@ -36,21 +33,21 @@ public ResponseEntity<?> savePost(@Valid @RequestBody PostReq postReq,

/* 게시글 목록 조회 + 검색 조회 */
@GetMapping
public Page<PostSummaryRes> getAllPosts(
public ResponseEntity<?> getAllPosts(
@PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable,
@RequestParam(name = "search", required = false) String search) {

if (search == null)
return postService.getAllPosts(pageable);
return ResponseEntity.ok().body(postService.getAllPosts(pageable));

return postService.getPostsByKeyword(search, pageable);
return ResponseEntity.ok().body(postService.getPostsByKeyword(search, pageable));
}

/* 특정 게시글 조회 */
@GetMapping("/{postId}")
public PostDetailRes getPost(@PathVariable UUID postId) {
public ResponseEntity<?> getPost(@PathVariable UUID postId) {

return postService.getPost(postId);
return ResponseEntity.ok().body(postService.getPost(postId));
}

/* 게시글 수정 */
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/gible/domain/post/dto/PostDetailRes.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public record PostDetailRes(
public static PostDetailRes fromEntity(Post post) {
return new PostDetailRes(
post.getId(), post.getTitle(), post.getContent(), post.getAddress(),
post.getName(), post.getWantedCard(), post.getDonatedCare(),
post.getWriter().getPhoneNumber(), post.getWriter().getName());
post.getWriter().getName(), post.getWantedCard(), post.getDonatedCare(),
post.getWriter().getPhoneNumber(), post.getWriter().getNickname());
}
}
3 changes: 0 additions & 3 deletions src/main/java/gible/domain/post/dto/PostReq.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ public record PostReq(
String content,
@NotBlank(message = "주소는 필수 작성 항목입니다.")
String address,
@NotBlank(message = "이름은 필수 작성 항목입니다.")
String name,
@NotNull(message = "필요한 개수는 필수 작성 항목입니다.")
int wantedCard
) {
Expand All @@ -21,7 +19,6 @@ public Post toEntity(PostReq postReq) {
.title(postReq.title())
.content(postReq.content())
.address(postReq.address())
.name(postReq.name())
.wantedCard(postReq.wantedCard())
.build();
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/gible/domain/post/dto/PostTitleRes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package gible.domain.post.dto;

import gible.domain.post.entity.Post;

import java.util.UUID;

public record PostTitleRes(
UUID postId,
String title
) {
public static PostTitleRes fromEntity(Post post) {
return new PostTitleRes(post.getId(), post.getTitle());
}
}
5 changes: 0 additions & 5 deletions src/main/java/gible/domain/post/entity/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ public class Post {
@NotNull
private String address;

@NotNull
private String name;

@NotNull
@Column(name = "wanted_card")
private int wantedCard;
Expand All @@ -62,7 +59,6 @@ public Post(String title, String content, String address, String name, int wante
this.title = title;
this.content = content;
this.address = address;
this.name = name;
this.wantedCard = wantedCard;
this.writer = writer;
}
Expand All @@ -72,7 +68,6 @@ public void updatePost(PostReq postReq) {
this.title = postReq.title();
this.content = postReq.content();
this.address = postReq.address();
this.name = postReq.name();
this.wantedCard = postReq.wantedCard();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.UUID;

public interface PostRepository extends JpaRepository<Post, UUID> {

Page<Post> findByTitleContaining(String search, Pageable pageable);
List<Post> findByWriter_Id(UUID userId);

@Query("SELECT p.writer.id FROM Post p WHERE p.id = :postId")
UUID findWriterIdByPostId(@Param("postId") UUID postId);
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/gible/domain/post/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import gible.domain.post.dto.PostDetailRes;
import gible.domain.post.dto.PostReq;
import gible.domain.post.dto.PostSummaryRes;
import gible.domain.post.dto.PostTitleRes;
import gible.domain.post.entity.Post;
import gible.domain.post.repository.PostRepository;
import gible.domain.user.entity.User;
Expand All @@ -19,6 +20,7 @@

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
Expand Down Expand Up @@ -69,6 +71,14 @@ public Page<PostSummaryRes> getPostsByKeyword(String search, Pageable pageable)
return searchPosts.map(PostSummaryRes::fromEntity);
}

/* 작성자의 게시글 불러오기 */
@Transactional(readOnly = true)
public List<PostTitleRes> getPostByUserId(UUID userId) {

List<Post> posts = postRepository.findByWriter_Id(userId);
return posts.stream().map(PostTitleRes::fromEntity).collect(Collectors.toList());
}

/* 게시글 수정 */
@AuthenticatedUser
@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import gible.domain.review.dto.ReviewReq;
import gible.domain.review.service.ReviewService;
import gible.domain.security.common.SecurityUserDetails;
import gible.global.util.api.SuccessRes;
import gible.global.common.response.SuccessRes;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/gible/domain/review/entity/Review.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;
import java.util.UUID;

@Getter
@Entity
@EntityListeners(AuditingEntityListener.class)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Review {
@Id
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gible.domain.user.controller;

import gible.domain.post.service.PostService;
import gible.domain.security.common.SecurityUserDetails;
import gible.domain.user.dto.MyPageRes;
import gible.domain.user.dto.SignUpReq;
Expand All @@ -16,6 +17,7 @@
@RequestMapping("/user")
public class UserController {
private final UserService userService;
private final PostService postService;

@PostMapping("/signUp")
public ResponseEntity<?> SignUp(@Valid @RequestBody SignUpReq signUpReq){
Expand All @@ -29,6 +31,12 @@ public ResponseEntity<MyPageRes> getMyPage(
return ResponseEntity.ok().body(userService.getMyPage(userDetails.getId()));
}

@GetMapping("/posts")
public ResponseEntity<?> getPostByUserId(@AuthenticationPrincipal SecurityUserDetails userDetails) {

return ResponseEntity.ok().body(postService.getPostByUserId(userDetails.getId()));
}

// @GetMapping("/participation-event")
// public ResponseEntity<List<EventSummaryRes>> getParticipationEvent(
// @AuthenticationPrincipal SecurityUserDetails userDetails
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/gible/domain/user/dto/SignUpReq.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static User toEntity(SignUpReq signUpDto) {
.phoneNumber(signUpDto.phoneNumber())
.nickname(signUpDto.nickname())
.emailAgree(signUpDto.emailAgree())
.role(Role.valueOf(signUpDto.role()))
.role(Role.USER)
.build();
}
}
Loading