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

[3주차 과제] 차현민 #48

Open
wants to merge 12 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
**/src/main/resources/*.properties
**/src/main/resources/*.yml

### STS ###
.apt_generated
Expand Down
Binary file added .gitignore.swp
Binary file not shown.
23 changes: 16 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
plugins {
id 'java'
id 'org.springframework.boot' version '3.0.1'
id 'io.spring.dependency-management' version '1.1.0'
id 'org.springframework.boot' version '2.7.8'
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
}

group = 'com.dku'
group = 'com.cha'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
sourceCompatibility = '11'

configurations {
compileOnly {
Expand All @@ -15,15 +15,24 @@ configurations {
}

repositories {
mavenCentral()
mavenCentral();
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.mysql:mysql-connector-j'
implementation 'org.springframework.boot:spring-boot-starter-websocket'
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5'
implementation 'io.springfox:springfox-swagger2:2.9.2'
implementation 'io.springfox:springfox-swagger-ui:2.9.2'
implementation 'io.jsonwebtoken:jjwt:0.9.1'
testImplementation 'junit:junit:4.13.1'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1 @@
rootProject.name = 'springstudy'
rootProject.name = 'carrotApi'
14 changes: 14 additions & 0 deletions src/main/java/com/cha/carrotApi/CarrotApiApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.cha.carrotApi;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@EnableJpaAuditing
@SpringBootApplication
public class CarrotApiApplication {
public static void main(String[] args) {
SpringApplication.run(CarrotApiApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.cha.carrotApi.DTO.category;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;

@Data
@ApiModel(value = "카테고리 생성 요청 처리")
@AllArgsConstructor
@NoArgsConstructor
public class CategoryCreateRequest {
@ApiModelProperty(value = "카테고리 명", notes = "카테고리 명을 입력하세요.", required = true, example = "category 1")
@NotBlank(message = "카테고리 명을 입력하세요.")
@Size(min = 2, max = 15, message = "길이 제한은 2~15자 이내입니다.")
private String name;

@ApiModelProperty(value = "부모 카테고리 id", notes = "부모 카테고리의 id를 입력하세요.")
private int parentId;
}
28 changes: 28 additions & 0 deletions src/main/java/com/cha/carrotApi/DTO/category/CategoryDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.cha.carrotApi.DTO.category;

import com.cha.carrotApi.domain.Category.Category;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

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

@AllArgsConstructor
@NoArgsConstructor
@Data
public class CategoryDto {
private int id;
private String name;
private List<CategoryDto> children;

public static List<CategoryDto> toDtoList(List<Category> categories) {
CategoryHelper helper = CategoryHelper.newInstance(
categories,
c -> new CategoryDto(c.getId(), c.getName(), new ArrayList<>()),
c -> c.getParent(),
c -> c.getId(),
d -> d.getChildren());
return helper.convert();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.cha.carrotApi.DTO.category;

import com.cha.carrotApi.exception.CannotConvertHelperException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

public class CategoryHelper<K, E, D> {
private List<E> entities;
private Function<E, D> toDto;
private Function<E, E> getParent;
private Function<E, K> getKey;
private Function<D, List<D>> getChildren;

public static <K, E, D> CategoryHelper newInstance(List<E> entities, Function<E, D> toDto, Function<E, E> getParent,
Function<E, K> getKey, Function<D, List<D>> getChildren) {
return new CategoryHelper<K, E, D>(entities, toDto, getParent, getKey, getChildren);
}

private CategoryHelper(List<E> entities, Function<E, D> toDto, Function<E, E> getParent, Function<E, K> getKey,
Function<D, List<D>> getChildren) {
this.entities = entities;
this.toDto = toDto;
this.getParent = getParent;
this.getKey = getKey;
this.getChildren = getChildren;
}

public List<D> convert() {
try {
return convertInternal();
} catch (NullPointerException e) {
throw new CannotConvertHelperException(e.getMessage());
}
}

private List<D> convertInternal() {
Map<K, D> map = new HashMap<>();
List<D> roots = new ArrayList<>();

for (E e : entities) {
D dto = toDto(e);
map.put(getKey(e), dto);
if (hasParent(e)) {
E parent = getParent(e);
K parentKey = getKey(parent);
D parentDto = map.get(parentKey);
getChildren(parentDto).add(dto);
} else {
roots.add(dto);
}
}
return roots;
}

private boolean hasParent(E e) {
return getParent(e) != null;
}

private E getParent(E e) {
return getParent.apply(e);
}

private D toDto(E e) {
return toDto.apply(e);
}

private K getKey(E e) {
return getKey.apply(e);
}

private List<D> getChildren(D d) {
return getChildren.apply(d);
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/cha/carrotApi/DTO/exception/BaseResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.cha.carrotApi.DTO.exception;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class BaseResponse {
private boolean isSuccess;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.cha.carrotApi.DTO.exception;

import com.cha.carrotApi.exception.ErrorCode;
import lombok.Builder;
import lombok.Getter;
import org.springframework.http.ResponseEntity;

import java.time.LocalDateTime;

@Getter
@Builder
public class ErrorResponse {
private final LocalDateTime timestamp = LocalDateTime.now();
private final int status;
private final String error;
private final String code;
private final String message;

public static ResponseEntity<ErrorResponse> toResponseEntity(ErrorCode errorCode) {
return ResponseEntity
.status(errorCode.getHttpStatus())
.body(ErrorResponse.builder()
.status(errorCode.getHttpStatus().value())
.error(errorCode.getHttpStatus().name())
.message(errorCode.getMessage())
.build()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.cha.carrotApi.DTO.exception;

import lombok.Getter;
import org.springframework.lang.Nullable;

@Getter
public class SuccessResponse<T> extends BaseResponse{

private T data;

public SuccessResponse(@Nullable T data) {
super(true);
this.data = data;
}
}
17 changes: 17 additions & 0 deletions src/main/java/com/cha/carrotApi/DTO/post/ImageDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.cha.carrotApi.DTO.post;

import com.cha.carrotApi.domain.Post.Image;
import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class ImageDto{
private int id;
private String originName;
private String uniqueName;

public static ImageDto toDto(Image image) {
return new ImageDto(image.getId(), image.getOriginName(), image.getUniqueName());
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/cha/carrotApi/DTO/post/PageInfoDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.cha.carrotApi.DTO.post;

import com.cha.carrotApi.domain.Post.Post;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.domain.Page;

@AllArgsConstructor
@NoArgsConstructor
@Data
public class PageInfoDto {
private int totalPage;
private int nowPage;
private int numberOfElements;
private boolean isNext;

public PageInfoDto(Page<Post> result) {
this.totalPage = result.getTotalPages();
this.nowPage = result.getNumber();
this.numberOfElements = result.getNumberOfElements();
this.isNext = result.hasNext();
}
}
29 changes: 29 additions & 0 deletions src/main/java/com/cha/carrotApi/DTO/post/PostCreateRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.cha.carrotApi.DTO.post;

import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiOperation(value = "게시글 생성 요청")
public class PostCreateRequest {
@ApiModelProperty(value = "게시글 제목", notes = "게시글 제목을 입력하세요.", required = true, example = "게시글 제목")
@NotBlank(message = "게시글 제목을 입력하세요.")
private String title;

@ApiModelProperty(value = "게시글 내용", notes = "게시글 내용을 입력하세요.", required = true, example = "게시글 내용")
@NotBlank(message = "게시글 내용을 입력하세요.")
private String content;

@ApiModelProperty(value = "이미지", notes = "이미지를 첨부해주세요.")
private List<MultipartFile> images = new ArrayList<>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.cha.carrotApi.DTO.post;

import lombok.AllArgsConstructor;
import lombok.Data;

import javax.validation.constraints.NotBlank;

@Data
@AllArgsConstructor
@NotBlank
public class PostCreateResponse {
private Long id;
private String title;
private String content;
}
19 changes: 19 additions & 0 deletions src/main/java/com/cha/carrotApi/DTO/post/PostFindAll.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.cha.carrotApi.DTO.post;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@AllArgsConstructor
@NoArgsConstructor
@Data
public class PostFindAll {
private List<PostSimpleDto> posts;
private PageInfoDto pageInfoDto;

public static PostFindAll toDto(List<PostSimpleDto> posts, PageInfoDto pageInfoDto){
return new PostFindAll(posts, pageInfoDto);
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/cha/carrotApi/DTO/post/PostListDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.cha.carrotApi.DTO.post;

import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.data.domain.Page;

import java.util.List;

@Data
@AllArgsConstructor
public class PostListDto {
private Integer totalElement;
private Integer totalPages;
private boolean hasNext;
private List<PostSimpleDto> postList;

public static PostListDto toDto(Page<PostSimpleDto> page) {
return new PostListDto(page.getTotalPages(), (int)page.getTotalElements(), page.hasNext(), page.getContent());
}
}
Loading