Skip to content

Commit

Permalink
Merge pull request #3 from KAKAO-TOUR-API-CONTEST/develop
Browse files Browse the repository at this point in the history
FIX : PathVariable -> requestParams
  • Loading branch information
femmefatalehaein authored Jul 22, 2024
2 parents 97e4cee + 592757f commit 4da8797
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 5 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
//testImplementation 'org.springframework.security:spring-security-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
implementation 'io.awspring.cloud:spring-cloud-aws-starter-s3:3.1.1'


}

tasks.named('test') {
Expand Down
82 changes: 82 additions & 0 deletions src/main/java/com/example/ai_jeju/controller/MyPageController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.example.ai_jeju.controller;

import com.example.ai_jeju.domain.User;
import com.example.ai_jeju.service.MyPageService;
import com.example.ai_jeju.service.S3Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Optional;

import static org.springframework.data.jpa.domain.AbstractPersistable_.id;

@RestController
@RequestMapping("/api")
public class MyPageController {

@Autowired
private MyPageService myPageService;
@Autowired
private S3Service s3Service;

//마이페이지 회원 1명씩 조회
@GetMapping("/mypage")
public ResponseEntity<User> getUserById(@RequestParam Long userId) {
Optional<User> user = myPageService.getUserById(userId);
if (user.isPresent()) {
return ResponseEntity.ok(user.get());
} else {
return ResponseEntity.notFound().build();
}
}


@PutMapping("/mypage/nickname")
public ResponseEntity<String> updateNickname(@RequestParam Long userId, @RequestBody Map<String, String> request) {
String nickname = request.get("nickname");
myPageService.updateNickname(userId, nickname);
return ResponseEntity.ok("Nickname changed");
}

//sns프로필 사용하기로 했었나? 사용하면 user쪽에 entity변수 하나 추가해서 해야함
/*
@PutMapping("/mypage/snsprofile")
public ResponseEntity<String> updateSnsProfile(@RequestParam Long userId, @RequestBody Map<String, String> request) {
String snsprofile = request.get("snsprofile");
myPageService.updateSnsProfile(id, snsprofile);
return ResponseEntity.ok("snsprofile change");
}*/

//프로필 이미지 변경
@PutMapping("/mypage/profileimg")
public ResponseEntity<String> updateProfileImage(@RequestParam Long userId, @RequestBody Map<String, String> request) {
String profileimg = request.get("profileimg");
myPageService.updateProfile(userId, profileimg);
return ResponseEntity.ok("success");
}

/*
@DeleteMapping("/mypage/snsprofile")
public ResponseEntity<String> deleteSnsProfile(@RequestParam Long id) {
myPageService.deleteSnsProfile(id);
return ResponseEntity.ok("deleted");
}*/

@DeleteMapping("/mypage/profileimg")
public ResponseEntity<String> deleteProfileImage(@RequestParam Long userId) {
myPageService.deleteProfileImage(userId);
return ResponseEntity.ok("deleted");
}

@PostMapping("/mypage/presign")
public ResponseEntity<String> createPresignedUrl(@RequestBody Map<String, String> request) {
String filePath = request.get("filePath");
String presignedUrl = s3Service.createPresignedUrl(filePath);
return ResponseEntity.ok(presignedUrl);
}



}
10 changes: 7 additions & 3 deletions src/main/java/com/example/ai_jeju/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
@Table(name="users")
@NoArgsConstructor(access= AccessLevel.PROTECTED) //기본생성자
@Getter
@Setter
@Entity
@AllArgsConstructor // 모든 필드를 초기화하는 생성자
@Builder // 빌더 패턴
Expand All @@ -32,15 +33,18 @@ public class User {
@Column(name = "nickname")
private String nickname;

@Column(name = "profile")
private String profile;

@Column(name = "provider")
private String provider;

@Column(name="snsprofile")
private String snsprofile;

@Column(name="profileimg")
private String profileimg;

/*

/*
// Builder pattern
private User(Builder builder) {
this.email = builder.email;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public String successHadler(SignUpRequest signUpRequest) throws IOException {
.nickname(nick)
.provider(signUpRequest.getProvider())
.email(signUpRequest.getEmail())
.profile(signUpRequest.getProfile())
.snsprofile(signUpRequest.getProfile())
.provider(signUpRequest.getProvider())
.build();

Expand Down
75 changes: 75 additions & 0 deletions src/main/java/com/example/ai_jeju/service/MyPageService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.example.ai_jeju.service;

import com.example.ai_jeju.domain.User;
import com.example.ai_jeju.repository.UserRepository;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.NoSuchElementException;
import java.util.Optional;

@Service
public class MyPageService {

@Autowired
private UserRepository userRepository;
@Autowired
private S3Service s3Service;

//마이페이지 회원 한명 id로 찾기
public Optional<User> getUserById(Long id) {
return userRepository.findById(id);
}

public void updateNickname(Long id, String nickname) {
User user = userRepository.findById(id)
.orElseThrow(() -> new RuntimeException("User not found"));
user.setNickname(nickname);
userRepository.save(user);
}

public void updateSnsProfile(Long id, String snsprofile) {
User user = userRepository.findById(id)
.orElseThrow(() -> new RuntimeException("User not found"));
user.setSnsprofile(snsprofile);
userRepository.save(user);
}

@Transactional
public void updateProfile(Long id, String profileimg) {
User user = userRepository.findById(id)
.orElseThrow(() -> new RuntimeException("User not found"));
user.setProfileimg(profileimg); // S3에 업로드된 이미지의 URL을 프로필 이미지로 설정
userRepository.save(user);
}

//프로필 이미지 반환
public String getProfileUrl(Long id) {
User user = userRepository.findById(id)
.orElseThrow(() -> new RuntimeException("Profile not found"));
return user.getProfileimg(); // 프로필 이미지 URL 반환
}

public void deleteSnsProfile(Long id) {
Optional<User> userOptional = userRepository.findById(id);
if (userOptional.isPresent()) {
User user = userOptional.get();
user.setSnsprofile(null);
userRepository.save(user);
} else {
throw new NoSuchElementException("not found");
}
}

public void deleteProfileImage(Long id) {
Optional<User> userOptional = userRepository.findById(id);
if (userOptional.isPresent()) {
User user = userOptional.get();
user.setProfileimg(null);
userRepository.save(user);
} else {
throw new NoSuchElementException("not found");
}
}
}
32 changes: 32 additions & 0 deletions src/main/java/com/example/ai_jeju/service/S3Service.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.example.ai_jeju.service;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.presigner.S3Presigner;
import software.amazon.awssdk.services.s3.presigner.model.PutObjectPresignRequest;

import java.time.Duration;

@Service
@RequiredArgsConstructor
public class S3Service {

@Value("${spring.cloud.aws.s3.bucket}")
private String bucket;
private final S3Presigner s3Presigner;

public String createPresignedUrl(String path) {
var putObjectRequest = PutObjectRequest.builder()
.bucket(bucket)
.key(path)
.build();
var preSignRequest = PutObjectPresignRequest.builder()
.signatureDuration(Duration.ofMinutes(10))
.putObjectRequest(putObjectRequest)
.build();
return s3Presigner.presignPutObject(preSignRequest).url().toString();
}

}
2 changes: 1 addition & 1 deletion src/main/java/com/example/ai_jeju/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public String signUp(SignUpRequest signUpRequest) throws IOException {
.nickname(nick)
.provider(signUpRequest.getProvider())
.email(signUpRequest.getEmail())
.profile(signUpRequest.getProfile())
//.profile(signUpRequest.getProfile())
.provider(signUpRequest.getProvider())
.build();
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@ [email protected]
jwt.secret_key= aijejubackend
react.app.url=http://localhost:3000


spring.cloud.aws.credentials.accessKey=${AWS_ACCESS_KEY_ID}
spring.cloud.aws.credentials.secretKey=${AWS_SECRET_KEY}
spring.cloud.aws.s3.bucket=ai-jeju
spring.cloud.aws.region.static=ap-northeast-2

1 comment on commit 4da8797

@femmefatalehaein
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

확인했습니다.

Please sign in to comment.