Skip to content

Commit

Permalink
Merge pull request #67 from h-jjang/fix/#63-post-entity
Browse files Browse the repository at this point in the history
Fix/#63 Post, User 엔티티 수정
  • Loading branch information
Gnu-Kenny authored May 31, 2022
2 parents 1d97af0 + a85623d commit 0562ddf
Show file tree
Hide file tree
Showing 16 changed files with 377 additions and 73 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
package com.hjjang.backend.domain.post.controller;

import static org.springframework.http.HttpStatus.*;
import static org.springframework.http.ResponseEntity.*;

import java.util.stream.Collectors;

import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hjjang.backend.domain.post.domain.entity.Post;
import com.hjjang.backend.domain.post.dto.PostMapper;
import com.hjjang.backend.domain.post.dto.PostRequestDto;
import com.hjjang.backend.domain.post.dto.PostResponseDto;
import com.hjjang.backend.domain.post.service.PostServiceImpl;
import com.hjjang.backend.domain.user.entity.User;
import com.hjjang.backend.global.dto.ApiResponse;
import com.hjjang.backend.global.response.code.SuccessCode;
import com.hjjang.backend.global.response.response.SuccessResponse;
import com.hjjang.backend.global.util.UserUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.stream.Collectors;

import static org.springframework.http.HttpStatus.*;
import static org.springframework.http.ResponseEntity.*;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@RequestMapping("/api/posts")
Expand All @@ -26,19 +39,11 @@ public class PostController {
private final UserUtil userUtil;

@PostMapping
public ResponseEntity<ApiResponse> createItem(@Validated @RequestBody PostRequestDto postRequestDto) {
return status(CREATED)
.body(ApiResponse.success(
"createItem",
postMapper.fromEntity(
postService.save(
postMapper.toEntity(
postRequestDto, userUtil.getLoginUserByToken()
)
)
)
)
);
public ResponseEntity<SuccessResponse> createItem(@Validated @RequestBody PostRequestDto postRequestDto) {
User user = userUtil.getLoginUserByToken();
Post post = postService.save(postMapper.toEntity(postRequestDto, user.getUniversity()));
PostResponseDto postResponseDto = postMapper.fromEntity(post);
return ResponseEntity.ok(SuccessResponse.of(SuccessCode.POST_CREATE_SUCCESS, postResponseDto));
}

@GetMapping
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
package com.hjjang.backend.domain.post.domain.entity;

import com.hjjang.backend.domain.post.dto.PostRequestDto;
import com.hjjang.backend.domain.user.entity.User;
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;
import static com.hjjang.backend.domain.post.domain.entity.PostDefaultValue.*;

import javax.persistence.*;
import java.time.LocalDateTime;

import static com.hjjang.backend.domain.post.domain.entity.PostDefaultValue.*;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.CreationTimestamp;

import com.hjjang.backend.domain.post.dto.PostRequestDto;
import com.hjjang.backend.domain.university.entity.University;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
//@Builder
Expand All @@ -22,9 +39,13 @@ public class Post {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne
@JoinColumn(name = "user_id")
private User user;
// @ManyToOne
// @JoinColumn(name = "user_id")
// private User user;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "university_id", nullable = false)
private University university = DEFAULT_UNIVERSITY;

@Column(name = "title", nullable = false)
private String title;
Expand Down Expand Up @@ -59,8 +80,8 @@ public class Post {
private LocalDateTime time;

@Builder
public Post(User user, String title, String content, int itemPrice) {
this.user = user;
public Post(University university, String title, String content, int itemPrice) {
this.university = university;
this.title = title;
this.content = content;
this.itemPrice = itemPrice;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.hjjang.backend.domain.post.domain.entity;

import com.hjjang.backend.domain.university.entity.University;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class PostDefaultValue {
public static final University DEFAULT_UNIVERSITY = new University(1L, "empty", "empty");
public static final int DEFAULT_VIEWS = 0;
public static final int DEFAULT_INTEREST_NUMBER = 0;
public static final int DEFAULT_CHAT_NUMBER = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.hjjang.backend.domain.post.dto;

import com.hjjang.backend.domain.post.domain.entity.Post;
import com.hjjang.backend.domain.user.entity.User;
import org.springframework.stereotype.Service;

import com.hjjang.backend.domain.post.domain.entity.Post;
import com.hjjang.backend.domain.university.entity.University;

@Service
public class PostMapper {

public Post toEntity(PostRequestDto postRequestDto, User user) {
public Post toEntity(PostRequestDto postRequestDto, University university) {
return Post.builder()
.user(user)
.university(university)
.title(postRequestDto.getTitle())
.content(postRequestDto.getContent())
.itemPrice(postRequestDto.getPrice())
Expand All @@ -19,7 +20,7 @@ public Post toEntity(PostRequestDto postRequestDto, User user) {
public PostResponseDto fromEntity(Post post) {
return PostResponseDto.builder()
.id(post.getId())
.user_id(post.getUser().getId())
.university_id(post.getUniversity().getId())
.title(post.getTitle())
.content(post.getContent())
.item_price(post.getItemPrice())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.hjjang.backend.domain.post.dto;

import com.hjjang.backend.domain.post.domain.entity.Post;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -12,7 +11,7 @@ public class PostResponseDto {

private Long id;

private Long user_id;
private Long university_id;

private String title;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.hjjang.backend.domain.post.service;

import java.util.List;

import org.springframework.stereotype.Service;

import com.hjjang.backend.domain.post.domain.entity.Post;
import com.hjjang.backend.domain.post.domain.repository.PostRepository;
import com.hjjang.backend.domain.post.dto.PostRequestDto;
import com.hjjang.backend.domain.post.exception.PostNotFoundException;
import com.hjjang.backend.domain.user.entity.User;
import com.hjjang.backend.domain.user.exception.UserNotMatchException;
import com.hjjang.backend.global.response.code.ErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@Service
Expand All @@ -32,8 +32,9 @@ public Post findOneById(Long id) {

public Post updateOneById(Long id, PostRequestDto postRequestDto, User user) {
Post foundPost = findOneById(id);
if (user == foundPost.getUser()) return save(foundPost.update(postRequestDto));
throw new UserNotMatchException("사용자 정보가 일치하지 않습니다.", ErrorCode.NO_AUTHORITY);
return save(foundPost.update(postRequestDto));
// if (user == foundPost.getUser()) return
// throw new UserNotMatchException("사용자 정보가 일치하지 않습니다.", ErrorCode.NO_AUTHORITY);
}

public void deleteOneById(Long id) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.hjjang.backend.domain.search.controller;

import com.hjjang.backend.domain.post.dto.PostMapper;
import com.hjjang.backend.domain.search.service.SearchServiceImpl;
import com.hjjang.backend.global.dto.ApiResponse;
import com.hjjang.backend.global.util.UserUtil;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/searches")
public class SearchController {

private final SearchServiceImpl searchService;
private final PostMapper postMapper;
private final UserUtil userUtil;

@GetMapping("/all")
public ResponseEntity<ApiResponse> searchPosts(
@RequestParam(required = false) String filter,
@PageableDefault(sort = "time", direction = Direction.DESC) Pageable pageable) {
return ResponseEntity.ok(
ApiResponse.success(
"searchPosts",
searchService.findAll(filter, pageable, userUtil.getLoginUserByToken())
.stream()
.map(postMapper::fromEntity)
.collect(Collectors.toList()))
);
}

@GetMapping
public ResponseEntity<ApiResponse> searchPostsByKeyword(
@RequestParam String keyword,
@RequestParam(required = false) String filter,
@PageableDefault(sort = "time", direction = Direction.DESC) Pageable pageable) {
return ResponseEntity.ok(
ApiResponse.success(
"searchPostsByKeyword",
searchService.findByKeyword(keyword, filter, pageable, userUtil.getLoginUserByToken())
.stream()
.map(postMapper::fromEntity)
.collect(Collectors.toList())
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.hjjang.backend.domain.search.repository;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

import com.hjjang.backend.domain.post.domain.entity.Post;
import com.hjjang.backend.domain.university.entity.University;

public interface SearchRepository extends JpaRepository<Post, Long> {

List<Post> findByIsSaleCompletion(String IsSaleCompletion, Pageable pageable);

Page<Post> findAllByIsSaleCompletion(String IsSaleCompletion, Pageable pageable);

List<Post> findByTitleContaining(String keyword, Pageable pageable);

Page<Post> findByUniversity(University university, Pageable pageable);

List<Post> findByIsSaleCompletionAndUniversity(String IsSaleCompletion, University university, Pageable pageable);
Page<Post> findAllByIsSaleCompletionAndUniversity(String IsSaleCompletion, University university, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.hjjang.backend.domain.search.service;

import com.hjjang.backend.domain.post.domain.entity.Post;
import com.hjjang.backend.domain.user.entity.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

public interface SearchService {

Page<Post> findAll(String filter, Pageable pageable, User user);

Page<Post> findByKeyword(String keyword, String filter, Pageable pageable, User user);
}
Loading

0 comments on commit 0562ddf

Please sign in to comment.