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

9주차미션_그린 #23

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
44 changes: 44 additions & 0 deletions 그린_안승연/week7/umc_board/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
buildscript {
ext {
queryDslVersion = "5.0.0"
}
}

plugins {
id 'java'
id 'org.springframework.boot' version '2.7.11'
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
id "com.ewerk.gradle.plugins.querydsl" version "1.0.10" //querydsl
}

group = 'com.example'
Expand All @@ -26,6 +33,9 @@ dependencies {
implementation "io.swagger:swagger-models:1.5.21"
implementation group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'

implementation "com.querydsl:querydsl-jpa:${queryDslVersion}"
implementation "com.querydsl:querydsl-apt:${queryDslVersion}"

implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
Expand All @@ -39,3 +49,37 @@ dependencies {
tasks.named('test') {
useJUnitPlatform()
}

//querydsl 사용 경로
def querydslDir = "$buildDir/generated/querydsl"

//querydsl 사용 설정
querydsl {
jpa = true
querydslSourcesDir = querydslDir
}

// build 시 사용할 sourceSet
sourceSets {
main.java.srcDir querydslDir
}

// compileClasspath와 annotationProcessor 상속
configurations {
compileOnly {
extendsFrom annotationProcessor
}
querydsl.extendsFrom compileClasspath
}

//feature/improve-all
// querydsl 컴파일 시 사용할 옵션.
compileQuerydsl {
options.annotationProcessorPath = configurations.querydsl
}

//feature/improve-all
// QType 정리
clean {
delete file(querydslDir)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.umc_board.Config;

import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Configuration
public class QuerydslConfiguration {

@PersistenceContext
private EntityManager entityManager;

@Bean
public JPAQueryFactory jpaQueryFactory(){
return new JPAQueryFactory(entityManager);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.List;

@RestController
@RequiredArgsConstructor
Expand All @@ -48,6 +49,12 @@ public ResponseEntity<Page<Board>> list(@PageableDefault Pageable pageable, Mode
return ResponseEntity.ok(boardService.findBoardList(pageable));
}

@GetMapping("/search")
@ApiOperation(value = "게시물 검색 API")
public ResponseEntity<List<Board>> getSearchBoards(@RequestParam(value = "search") String search){
return ResponseEntity.ok(boardService.findBoardByTitle(search));
}

@GetMapping("/{id}")
@ApiOperation(value = "게시물 조회 API")
@ApiImplicitParam(name = "id", value = "게시물 id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public ResponseEntity<UserResponseDto> join(@RequestBody UserRequestDto requestD
@PostMapping("/login")
@ApiOperation(value = "로그인 API")
public ResponseEntity<UserResponseDto> login(@RequestBody LoginRequestDto requestDto){
return ResponseEntity.ok(UserResponseDto.of(userService.findUser(requestDto.getEmail(), requestDto.getPassword())));
return ResponseEntity.ok(UserResponseDto.of(userService.findByEmail(requestDto.getEmail(), requestDto.getPassword())));
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
package com.example.umc_board.Dto;

import com.example.umc_board.Entity.Board;
import com.example.umc_board.Entity.Category;
import com.example.umc_board.Entity.KakaoPlace;
import com.example.umc_board.Entity.User;
import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

@Getter
Expand All @@ -24,46 +17,22 @@ public class BoardRequestDto {
@ApiModelProperty(example = "test content")
private String content;

@ApiModelProperty(example = "커뮤니티")
@ApiModelProperty(example = "community")
private String category;

@ApiModelProperty(example = "tester")
private User user;
@ApiModelProperty(example = "[email protected]")
private String email;

@ApiModelProperty(example = "[[\"20522613\", \"거북당\", \"http://place.map.kakao.com/20522613\"]]")
private List<List<String>> placeList;
@ApiModelProperty(example = "[20522613]")
private List<Long> placeList;

@Builder
public BoardRequestDto(String title, String content, String category, User user,
List<List<String>> places){
public BoardRequestDto(String title, String content, String category, String email,
List<Long> places) {
this.title = title;
this.content = content;
this.category = category;
this.user = user;
this.email = email;
this.placeList = places;
}

public Board toEntity() throws MalformedURLException {
List<KakaoPlace> places = new ArrayList<>();

for(List<String> place : placeList){
Long id = Long.valueOf(place.get(0));
String title = place.get(1);
URL url = new URL(place.get(2));
KakaoPlace temp = KakaoPlace.builder()
.id(id)
.title(title)
.url(url)
.build();
places.add(temp);
}

return Board.builder()
.title(title)
.content(content)
.category(Category.valueOf(category))
.user(user)
.list(places)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.example.umc_board.Dto;

import com.example.umc_board.Entity.Board;
import com.example.umc_board.Entity.Category;
import com.example.umc_board.Entity.KakaoPlace;
import com.example.umc_board.Entity.User;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Getter
Expand All @@ -24,32 +25,21 @@ public class BoardResponseDto {
private String content;

@ApiModelProperty(example = "커뮤니티")
private String category;
private Category category;

@ApiModelProperty(example = "tester")
private String user;
private User user;

@ApiModelProperty(example = "[[\"20522613\", \"거북당\", \"http://place.map.kakao.com/20522613\"]]")
private List<List<String>> placeList;
private List<KakaoPlace> placeList;

public static BoardResponseDto of(Board board){
List<KakaoPlace> places = board.getPlaceList();
List<List<String>> list = new ArrayList<>();

for(KakaoPlace place : places){
List<String> tempList = new ArrayList<>();
tempList.add(place.getId().toString());
tempList.add(place.getTitle());
tempList.add(place.getUrl().toString());
list.add(tempList);
}

return BoardResponseDto.builder()
.title(board.getTitle())
.content(board.getContent())
.category(board.getCategory().getValue())
.user(board.getUser().getName())
.placeList(list)
.category(board.getCategory())
.user(board.getUser())
.placeList(board.getPlaceList())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.umc_board.Entity;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -8,14 +9,14 @@
import java.util.ArrayList;
import java.util.List;

@NoArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@Entity
@Table
public class Board extends BaseTimeEntity{

@Id
@Column
@Column(name = "board_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

Expand All @@ -29,6 +30,7 @@ public class Board extends BaseTimeEntity{
private Category category;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;

@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
Expand All @@ -44,6 +46,10 @@ public Board(String title, String content,
this.placeList = list;
}

public void setKakaoPlaces(KakaoPlace place){
this.placeList.add(place);
}

public void update(String title, String content, Category category){
this.title = title;
this.content = content;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package com.example.umc_board.Entity;

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

import javax.persistence.*;
import java.net.URL;

@NoArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@Entity
@Table
public class KakaoPlace {

@Id
@Column
@Column(name = "kakaoplace_id")
private Long id;

@Column
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface BoardRepository extends JpaRepository<Board, Long> {
Board findByUser(User user);
public interface BoardRepository extends JpaRepository<Board, Long>, BoardRepositoryCustom {
List<Board> findByUser(User user);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.umc_board.Repository;

import com.example.umc_board.Entity.Board;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface BoardRepositoryCustom {

List<Board> findByTitle(String title);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.umc_board.Repository;

import com.example.umc_board.Entity.Board;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;

import java.util.List;

import static com.example.umc_board.Entity.QBoard.board;

@RequiredArgsConstructor
public class BoardRepositoryImpl implements BoardRepositoryCustom{

private final JPAQueryFactory jpaQueryFactory;

public List<Board> findByTitle(String title){
return jpaQueryFactory.selectFrom(board)
.where(board.title.eq(title))
.fetch();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.umc_board.Repository;

import com.example.umc_board.Entity.KakaoPlace;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface KakaoPlaceRepository extends JpaRepository<KakaoPlace, Long> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
User findByEmail(String email);

boolean existsByEmail(String email);
}
Loading