Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…k-Server into develop
  • Loading branch information
yangchef1 committed May 14, 2024
2 parents a7f9851 + 0e39a5c commit 8eef5e7
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.sanbosillok.sanbosillokserver.api.image.dto;

import lombok.Data;

@Data
public class ImagePathResponse {
private String imagePath;

public ImagePathResponse(String imagePath) {
this.imagePath = imagePath;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.sanbosillok.sanbosillokserver.api.image.service;

import com.sanbosillok.sanbosillokserver.api.image.dto.ImagePathResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Service
@RequiredArgsConstructor
public class ImageService {

public ImagePathResponse upload(String path, MultipartFile file) {
String uuid = UUID.randomUUID().toString();
String fileName = file.getOriginalFilename();
assert fileName != null;
String fileExtension = fileName.substring(fileName.lastIndexOf('.'));

File saveFile = new File(path + uuid + fileExtension);

try {
file.transferTo(saveFile);
return new ImagePathResponse(path + uuid + fileExtension);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.sanbosillok.sanbosillokserver.api.member.dto.CheckUserNameResponse;
import com.sanbosillok.sanbosillokserver.api.member.dto.JoinRequest;
import com.sanbosillok.sanbosillokserver.api.member.service.JoinService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

Expand All @@ -14,7 +13,7 @@ public class JoinController {
private final JoinService joinService;

@PostMapping("/signup")
public void join(@RequestBody @Valid JoinRequest joinRequest) {
public void join(@ModelAttribute JoinRequest joinRequest) {
joinService.join(joinRequest);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ public class Member extends BaseEntity {

private MemberRole role;

private String studentIdImagePath;

@Builder
public Member(String username, String password, MemberRole role) {
public Member(String username, String password, MemberRole role, String studentIdImagePath) {
this.username = username;
this.password = password;
this.role = role;
this.studentIdImagePath = studentIdImagePath;
}

public void updateRole(MemberRole role) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.sanbosillok.sanbosillokserver.api.member.dto;

import lombok.Data;
import org.springframework.web.multipart.MultipartFile;

@Data
public class JoinRequest {

private String username;

private String password;

private MultipartFile studentIdImage;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
public class MemberResponse {
private String username;
private LocalDateTime createdAt;
private String studentIdImagePath;

public MemberResponse(Member member) {
username = member.getUsername();
createdAt = member.getCreatedAt();
studentIdImagePath = member.getStudentIdImagePath();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.sanbosillok.sanbosillokserver.api.member.service;

import com.sanbosillok.sanbosillokserver.api.image.dto.ImagePathResponse;
import com.sanbosillok.sanbosillokserver.api.image.service.ImageService;
import com.sanbosillok.sanbosillokserver.api.member.domain.Member;
import com.sanbosillok.sanbosillokserver.api.member.domain.MemberRole;
import com.sanbosillok.sanbosillokserver.api.member.dto.CheckUserNameResponse;
Expand All @@ -13,15 +15,20 @@
@RequiredArgsConstructor
public class JoinService {
private final MemberRepository memberRepository;
private final ImageService imageService;
private final BCryptPasswordEncoder bCryptPasswordEncoder;
private static final String JOIN_IMAGE_PATH = "/home/ubuntu/join/";

public void join(JoinRequest joinRequest) {
if (!memberRepository.existsByUsername(joinRequest.getUsername())) {

String studentIdImagePath = imageService.upload(JOIN_IMAGE_PATH, joinRequest.getStudentIdImage()).getImagePath();

Member member = Member.builder()
.username(joinRequest.getUsername())
.password(bCryptPasswordEncoder.encode(joinRequest.getPassword()))
.role(MemberRole.INACTIVE)
.studentIdImagePath(studentIdImagePath)
.build();

memberRepository.save(member);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.sanbosillok.sanbosillokserver.api.post.controller;

import com.sanbosillok.sanbosillokserver.api.post.dto.PostRequest;
import com.sanbosillok.sanbosillokserver.api.post.dto.PostResponse;
import com.sanbosillok.sanbosillokserver.api.post.dto.PostTitleResponse;
import com.sanbosillok.sanbosillokserver.api.post.dto.PostUpdateRequest;
import com.sanbosillok.sanbosillokserver.api.image.dto.ImagePathResponse;
import com.sanbosillok.sanbosillokserver.api.image.service.ImageService;
import com.sanbosillok.sanbosillokserver.api.post.dto.*;
import com.sanbosillok.sanbosillokserver.api.post.service.PostService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

Expand All @@ -18,6 +18,8 @@
public class PostController {

private final PostService postService;
private final ImageService imageService;
private static final String POST_IMAGE_PATH = "/home/ubuntu/post/";

@PostMapping("")
public void createPost(@RequestBody @Valid PostRequest postRequest) {
Expand Down Expand Up @@ -51,4 +53,9 @@ public void updatePost(@PathVariable String title,
public void deletePost(@PathVariable String title) {
postService.delete(title);
}

@PostMapping("/upload")
public ImagePathResponse uploadPostImage(@RequestParam MultipartFile file) {
return imageService.upload(POST_IMAGE_PATH, file);
}
}

0 comments on commit 8eef5e7

Please sign in to comment.