Skip to content

Commit

Permalink
🥹Feat #25: [가계부 공유] Img 파일 업로드
Browse files Browse the repository at this point in the history
  • Loading branch information
DDonghyeo committed Jul 24, 2023
1 parent c279691 commit 8fe1689
Show file tree
Hide file tree
Showing 24 changed files with 82 additions and 55 deletions.
Binary file added .DS_Store
Binary file not shown.
Binary file modified .gradle/8.1.1/executionHistory/executionHistory.lock
Binary file not shown.
Binary file modified .gradle/8.1.1/fileHashes/fileHashes.lock
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
1 change: 0 additions & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions out/production/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ logging:
com.example.carrotmarket: debug
org.hibernate.SQL: debug

spring:
jpa:
properties:
hibernate:
format_sql: true
show_sql: true
---
# Settings for local
spring:
Expand All @@ -18,4 +12,11 @@ spring:
password: qwe335577!
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
hibernate.ddl-auto: update
hibernate.ddl-auto: update
properties:
hibernate:
format_sql: true
show_sql: true
jwt:
secret-key: 6B64DCA4EA2F53EDIKU9AAB215FE7

Binary file added src/.DS_Store
Binary file not shown.
Binary file added src/main/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

Expand All @@ -22,18 +23,16 @@ public class accountBookSharingController {
/*
* [가계부 공유] 게시글 검색
* @param keyword
* @param pageable
*
* @param pageable
* size : 페이지 사이즈
* page : 페이지
* sortBy : 정렬순
* - like
* - likes
* - createdAt
*/

@GetMapping("/search")

public ResponseEntity<?> searchAll(@RequestParam("keyword") String keyword, @RequestParam("category") int category, Pageable pageable) {
log.info("searching : " + keyword + category);
List<SharingDto.ListResponse> res = accountBookSharingService.searchByKeyword(keyword, category, pageable);
Expand All @@ -43,14 +42,14 @@ public ResponseEntity<?> searchAll(@RequestParam("keyword") String keyword, @Req

/*
* [가계부 공유] 게시글 등록
*
* @param RequestDto
*/

@PostMapping("")
public ResponseEntity<?> createPost(@RequestBody SharingDto.Request req) {
accountBookSharingService.createPost(req);
public ResponseEntity<?> createPost(@RequestPart SharingDto.Request req,
@RequestPart(value = "file", required = false) List<MultipartFile> files) {
log.info("req open");
accountBookSharingService.createPost(req, files);
return new ResponseEntity<>(HttpStatus.OK);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public static class Request {

private String content;

private List<String> images;

public SharingBoard toEntity(User user) {
return SharingBoard.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface SharingBoardRepository extends JpaRepository<SharingBoard,Long>
// }


@Query(value = "SELECT sharing_board.*, COUNT(sharing_sympathy.sharing_board_id) AS cnt FROM sharing_board\n" +
@Query(value = "SELECT sharing_board.*, COUNT(sharing_sympathy.sharing_board_id) AS likes FROM sharing_board\n" +
"LEFT JOIN sharing_sympathy ON sharing_board.sharing_board_id = sharing_sympathy.sharing_board_id\n" +
"WHERE (sharing_board.title LIKE %:keyword% OR sharing_board.content LIKE %:keyword%)\n" +
"AND sharing_board.category = :category GROUP BY sharing_board.sharing_board_id ", nativeQuery = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import com.umc.DongnaeFriend.domain.account.sharing.dto.SharingDto;
import org.springframework.data.domain.Pageable;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

public interface AccountBookSharingService {

List<SharingDto.ListResponse> searchByKeyword(String keyword, int category, Pageable pageable);

void createPost(SharingDto.Request req);
void createPost(SharingDto.Request req, List<MultipartFile> files);

/*
* [가계부 공유] 게시글 상세 조회
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,23 @@
import com.umc.DongnaeFriend.domain.account.sharing.repository.SharingImgRepository;
import com.umc.DongnaeFriend.domain.account.sharing.repository.SharingSympathyRepository;
import com.umc.DongnaeFriend.domain.dongnae.entity.Dongnae;
import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeImg;
import com.umc.DongnaeFriend.domain.type.Age;
import com.umc.DongnaeFriend.domain.type.Gender;
import com.umc.DongnaeFriend.domain.type.SharingCategory;
import com.umc.DongnaeFriend.domain.type.YesNo;
import com.umc.DongnaeFriend.domain.user.entity.User;
import com.umc.DongnaeFriend.global.exception.CustomException;
import com.umc.DongnaeFriend.global.exception.ErrorCode;
import com.umc.DongnaeFriend.global.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -59,17 +64,37 @@ public List<SharingDto.ListResponse> searchByKeyword(String keyword, int categor
if (sharingBoards.isEmpty()) {
throw new CustomException(ErrorCode.NO_CONTENT_FOUND);
}
log.info("board found" + sharingBoards.get(0).getId());
return getListResponses(sharingBoards);
}


/*
* [가계부 공유] 게시글 등록
*/
@Override
public void createPost(SharingDto.Request req) {
sharingBoardRepository.save(req.toEntity(user));
//TODO : Img 파일 업로드
public void createPost(SharingDto.Request req, List<MultipartFile> files) {
SharingBoard board = sharingBoardRepository.save(req.toEntity(user));
log.info("파일 검사");

if (files == null) {
return;
}
log.info("파일 인식");

for (int i = 0; i < files.size(); i++) {
String fileName = "SharingBoard_" + board.getId().toString() + "_" + i + ".png";
try {
FileUtil.fileUpload(files.get(i),fileName );
} catch (IOException e) {
throw new RuntimeException(e);
}
sharingImgRepository.save(
SharingImg.builder()
.sharingBoard(board)
.imageUrl(fileName)
.build()
);
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.umc.DongnaeFriend.domain.dongnae.dto.DongnaeBoardDto;
import com.umc.DongnaeFriend.domain.dongnae.dto.UserLocationDto;
import org.springframework.web.multipart.MultipartFile;

import javax.naming.AuthenticationException;
import java.util.List;
Expand All @@ -12,12 +13,16 @@ public interface DongnaeBoardService {

List<DongnaeBoardDto.ListResponse> searchAll(int sort);

void createBoard(DongnaeBoardDto.Request req);

List<DongnaeBoardDto.ListResponse> home(int category);

UserLocationDto getUserLocation();


/*
* [동네정보] 게시글 등록
*/
void createBoard(DongnaeBoardDto.Request req);

DongnaeBoardDto.Response getBoard(long board_id);

void updateBoard(long board_id, DongnaeBoardDto.Request request) throws AuthenticationException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ public List<DongnaeBoardDto.ListResponse> searchAll(int sort) {
*/
@Override
public void createBoard(DongnaeBoardDto.Request req) {
//TODO : User Mapping UserRepository 필요.
dongnaeBoardRepository.save(req.toEntity(user, dongnae));
DongnaeBoard board= dongnaeBoardRepository.save(req.toEntity(user, dongnae));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public class GlobalExceptionHandler {
@ExceptionHandler({CustomException.class})
protected ResponseEntity<?> handlecustomException(CustomException e) {
return ResponseEntity
.status(e.getErrorCode().getHttpStatus())
.body(new ErrorResponse(e));
.status(e.getErrorCode().getHttpStatus())
.body(new ErrorResponse(e));
}

//@valid Exception
Expand All @@ -25,16 +25,17 @@ protected ResponseEntity<?> handleMethodArgumentNotValidException(MethodArgument
CustomException validException = new CustomException(INVALID_VALUE, message);

return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(new ErrorResponse(validException));
.status(HttpStatus.BAD_REQUEST)
.body(new ErrorResponse(validException));
}

}
//일반 예외처리
@ExceptionHandler({Exception.class})
protected ResponseEntity<?> handleServerException(Exception ex) {
CustomException exception = new CustomException(SERVER_ERROR);
return ResponseEntity
.status(SERVER_ERROR.getHttpStatus())
.body(new ErrorResponse(exception));
}
}
// @ExceptionHandler({Exception.class})
// protected ResponseEntity<?> handleServerException(Exception ex) {
// CustomException exception = new CustomException(SERVER_ERROR);
// return ResponseEntity
// .status(SERVER_ERROR.getHttpStatus())
// .body(new ErrorResponse(exception));
// }
//}
17 changes: 7 additions & 10 deletions src/main/java/com/umc/DongnaeFriend/global/util/FileUtil.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
package com.umc.DongnaeFriend.global.util;

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

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class FileUtil {

public static String fileUpload(List<MultipartFile> files) throws IOException {
String filepath = "/resources/static/img";
public static void fileUpload(MultipartFile file, String fileName) throws IOException {
String filepath = "/Users/kimdonghyun/Desktop/Dong_Hyun/projects/동네친구/Server/src/main/resources/static/img/"; //절대경로 지정

List<String> list = new ArrayList<>();
for (MultipartFile file : files) {
String originalfileName = file.getOriginalFilename();
File dest = new File(filepath + originalfileName);
file.transferTo(dest);
list.add(dest.getPath());
}
return String.join(",", list);
File dest = new File(filepath + fileName);
file.transferTo(dest);
}


}
Binary file added src/main/resources/.DS_Store
Binary file not shown.
15 changes: 8 additions & 7 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ logging:
com.example.carrotmarket: debug
org.hibernate.SQL: debug

spring:
jpa:
properties:
hibernate:
format_sql: true
show_sql: true
---
# Settings for local
spring:
Expand All @@ -18,4 +12,11 @@ spring:
password: qwe335577!
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
hibernate.ddl-auto: update
hibernate.ddl-auto: update
properties:
hibernate:
format_sql: true
show_sql: true
jwt:
secret-key: 6B64DCA4EA2F53EDIKU9AAB215FE7

Binary file added src/main/resources/static/.DS_Store
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/static/img/abc.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8fe1689

Please sign in to comment.