Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/h-jjang/bauction into fe…
Browse files Browse the repository at this point in the history
…ature/#34-매물-검색-기능-구현

� Conflicts:
�	backend/src/main/java/com/hjjang/backend/global/config/SwaggerConfig.java
  • Loading branch information
Yunkeun committed May 5, 2022
2 parents 483aa1d + 50a784d commit dd0cb6d
Show file tree
Hide file tree
Showing 64 changed files with 984 additions and 696 deletions.
5 changes: 5 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,8 @@ out/
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

/src/main/resources/application-aws.yml
/src/main/resources/application-credentials.yml
/src/main/resources/application-auth.yml
/src/main/resources/application-db.yml
/src/main/resources/application-email.yml
12 changes: 11 additions & 1 deletion backend/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'org.springframework.boot' version '2.6.5'
id 'org.springframework.boot' version '2.6.6'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'org.asciidoctor.convert' version '1.5.8'
id 'java'
Expand Down Expand Up @@ -37,6 +37,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-websocket'
implementation 'org.flywaydb:flyway-core'
implementation 'org.springframework.kafka:spring-kafka'
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'io.micrometer:micrometer-registry-prometheus'
Expand All @@ -53,9 +54,18 @@ dependencies {
implementation 'io.jsonwebtoken:jjwt-api:0.11.2'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.2'
runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.11.2'
implementation 'org.springframework.boot:spring-boot-starter-log4j2'


}
configurations {
all*.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
all*.exclude group: 'org.springframework.boot', module: 'logback-classic'
}

tasks.named('compileJava') {
inputs.files(tasks.named('processResources'))
}

bootRun {
mainClassName = 'com.hjjang.backend.BackendApplication'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.hjjang.backend.domain.image.controller;

import com.hjjang.backend.domain.image.domain.entity.Image;
import com.hjjang.backend.domain.image.dto.ImagePathResponse;
import com.hjjang.backend.domain.image.service.ImageService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

import static com.hjjang.backend.global.util.HttpStatusResponseEntity.RESPONSE_OK;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/images")
public class ImageController {

private final ImageService imageService;

@GetMapping()
public ResponseEntity<List<ImagePathResponse>> findAllImage() {
return ResponseEntity.ok(imageService
.findAll()
.stream()
.map(ImagePathResponse::of)
.collect(Collectors.toList())
);
}

@PostMapping()
public ResponseEntity<ImagePathResponse> uploadImage(@RequestParam("images") MultipartFile multipartFile) throws IOException {
return ResponseEntity.ok(ImagePathResponse
.of(imageService
.uploadNewImage(multipartFile)
)
);
}

@GetMapping("/{imageId}")
public ResponseEntity<ImagePathResponse> findImage(@PathVariable Long imageId) {
return ResponseEntity.ok(ImagePathResponse
.of(imageService
.findImageById(imageId)
)
);
}

@DeleteMapping("/{imageId}")
public ResponseEntity<HttpStatus> deleteImage(@PathVariable Long imageId) {
Image image = imageService.findImageById(imageId);
imageService.removeImage(image);

return RESPONSE_OK;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.hjjang.backend.infra.image.domain.entity;
package com.hjjang.backend.domain.image.domain.entity;

import lombok.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.hjjang.backend.infra.image.domain.repository;
package com.hjjang.backend.domain.image.domain.repository;

import com.hjjang.backend.infra.image.domain.entity.Image;
import com.hjjang.backend.domain.image.domain.entity.Image;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ImageRepository extends JpaRepository<Image, Long> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.hjjang.backend.domain.image.dto;

import com.hjjang.backend.domain.image.domain.entity.Image;
import lombok.Builder;
import lombok.Getter;

@Builder
@Getter
public class ImagePathResponse {

private Long id;
private String path;

public static ImagePathResponse of(Image image) {
return ImagePathResponse.builder()
.id(image.getId())
.path(image.getPath())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.hjjang.backend.infra.image.exception;
package com.hjjang.backend.domain.image.exception;

public class ImageNotFoundException extends RuntimeException {

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

import com.hjjang.backend.domain.image.domain.repository.ImageRepository;
import com.hjjang.backend.domain.image.domain.entity.Image;
import com.hjjang.backend.domain.image.exception.ImageNotFoundException;
import com.hjjang.backend.infra.aws.service.S3ImageUploader;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.transaction.Transactional;
import java.io.IOException;
import java.util.List;

@Service
@RequiredArgsConstructor
@Transactional
public class ImageService {
private final ImageRepository imageRepository;

private final S3ImageUploader imageUploader;

public List<Image> findAll() {
return imageRepository.findAll();
}

public Image findImageById(Long imageId) {
return imageRepository.findById(imageId).orElseThrow(ImageNotFoundException::new);
}

public Image uploadNewImage(MultipartFile multipartFile) throws IOException {
return imageRepository.save(new Image(imageUploader.upload(multipartFile)));
}

public void removeImage(Image image) {
image.removeImage();
imageRepository.save(image);
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.hjjang.backend.infra.image.component;
package com.hjjang.backend.domain.image.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
Expand All @@ -13,35 +13,27 @@

@Slf4j
@RequiredArgsConstructor
@Component
public class LocalUploader implements ImageUploader {
@Service
public class ImageUploader {

@Override
public String upload(MultipartFile multipartFile) throws IOException {
File uploadFile =
this.localUpload(multipartFile) // 파일 변환할 수 없으면 에러
.orElseThrow(
() -> new IllegalArgumentException("error: MultipartFile -> File convert fail"));
return uploadFile.getName();
return null;
}

@Override
public String changeFileName(MultipartFile uploadFile) {
return UUID.randomUUID() + uploadFile.getOriginalFilename(); // 저장될 파일 이름 변환
return UUID.randomUUID() + ".png"; // 저장될 파일 이름 변환
}

// 로컬에 파일 업로드 하기
@Override
public Optional<File> localUpload(MultipartFile file) throws IOException {
File convertFile = new File(System.getProperty("user.dir") + "/backend/src/main/resources/images/" + changeFileName(file));
File convertFile = new File(Path.imageSavePath.getPath() + changeFileName(file));
if (convertFile.createNewFile()) { // 바로 위에서 지정한 경로에 File이 생성됨 (경로가 잘못되었다면 생성 불가능)
try (FileOutputStream fos =
new FileOutputStream(convertFile)) { // FileOutputStream 데이터를 파일에 바이트 스트림으로 저장하기 위함
fos.write(file.getBytes());
}
return Optional.of(convertFile);
}

return Optional.empty();
}

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

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Slf4j
@RequiredArgsConstructor
@Service
public class LocalImageUploader extends ImageUploader{

@Override
public String upload(MultipartFile multipartFile) throws IOException {
File uploadFile =
this.localUpload(multipartFile) // 파일 변환할 수 없으면 에러
.orElseThrow(
() -> new IllegalArgumentException("error: MultipartFile -> File convert fail"));
return uploadFile.getName();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.hjjang.backend.domain.image.service;

import lombok.Getter;

@Getter
public enum Path {
imageSavePath(System.getProperty("user.dir") + "/backend/src/main/resources/images/");

private final String path;
Path(String path) {
this.path = path;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class PostController {
private final PostServiceImpl postService;

@PostMapping
public ResponseEntity<PostResponse> createItem(@Validated @RequestParam PostRequest postRequest) {
public ResponseEntity<PostResponse> createItem(@Validated @RequestBody PostRequest postRequest) {
return ResponseEntity.ok(
PostResponse.of(
postService.save(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,50 @@
package com.hjjang.backend.domain.post.domain.entity;

import com.hjjang.backend.domain.user.entity.User;
import com.hjjang.backend.infra.image.domain.entity.Image;
import lombok.*;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.DynamicInsert;

import javax.persistence.*;

@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@DynamicInsert
@Table(schema = "post")
@Entity
public class Post {
@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

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

@ManyToOne
@JoinColumn(name = "image_id")
private Image image;
// @ManyToOne
// @JoinColumn(name = "image_id")
// private Image image;

private String title;

private String content;

private Integer item_price;

@ColumnDefault("0")
private Integer views = 0;

@ColumnDefault("0")
private Integer interest_number = 0;

@ColumnDefault("0")
private Integer chat_number = 0;

private String is_sale_completion;
@ColumnDefault("'false'")
private String is_sale_completion = "false";

@ColumnDefault("false")
private boolean removed = false;

public void removePost() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.hjjang.backend.domain.post.dto;

import com.hjjang.backend.domain.post.domain.entity.Post;
import lombok.Getter;
import lombok.*;

import javax.validation.constraints.NotEmpty;

@Getter
@Setter
@AllArgsConstructor
@RequiredArgsConstructor
public class PostRequest {

@NotEmpty
Expand All @@ -15,13 +18,15 @@ public class PostRequest {
private String content;

@NotEmpty
private int price;
private String price;

public Post toEntity() {
return Post.builder()
// .user(null)
// .image(null)
.title(this.title)
.content(this.content)
.item_price(this.price)
.item_price(Integer.valueOf(this.price))
.build();
}
}
Loading

0 comments on commit dd0cb6d

Please sign in to comment.