Skip to content

Commit

Permalink
Merge pull request #9 from AI-SIP/develop
Browse files Browse the repository at this point in the history
get processImage from fastApi Server
  • Loading branch information
KiSeungMin authored Jul 25, 2024
2 parents 7348ab9 + 58f3766 commit c44665c
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.aisip.OnO.backend.Dto.Problem;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.*;

import java.time.LocalDateTime;

@Builder
@Getter
@Setter
@NoArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.aisip.OnO.backend.Dto.User;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.*;

@Builder
@Getter
@Setter
@NoArgsConstructor
Expand All @@ -15,10 +13,6 @@ public class UserResponseDto {

private Long userId;

private String socialId;

private String socialLoginType;

private String userName;

private String userEmail;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.aisip.OnO.backend.controller;

import com.aisip.OnO.backend.Dto.User.UserResponseDto;
import com.aisip.OnO.backend.service.AuthService;
import com.aisip.OnO.backend.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -19,9 +20,9 @@ public class UserController {
@GetMapping("/info")
public ResponseEntity<?> getUserInfo(Authentication authentication) {
Long userId = (Long) authentication.getPrincipal();
User user = authService.getUserById(userId);
if (user != null) {
return ResponseEntity.ok(user);
UserResponseDto userResponseDto = authService.getUserById(userId);
if (userResponseDto != null) {
return ResponseEntity.ok(userResponseDto);
} else {
return ResponseEntity.status(404).body(new ErrorResponse("User not found"));
}
Expand Down
24 changes: 13 additions & 11 deletions src/main/java/com/aisip/OnO/backend/converter/ProblemConverter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.aisip.OnO.backend.converter;

import com.aisip.OnO.backend.Dto.Problem.ProblemRegisterDto;
import com.aisip.OnO.backend.Dto.Problem.ProblemResponseDto;
import com.aisip.OnO.backend.entity.Image.ImageData;
import com.aisip.OnO.backend.entity.Problem;
Expand All @@ -13,30 +14,31 @@ public static ProblemResponseDto convertToResponseDto(Problem problem, List<Imag
return null;
}

ProblemResponseDto dto = new ProblemResponseDto();
dto.setProblemId(problem.getId());
dto.setMemo(problem.getMemo());
dto.setReference(problem.getReference());
dto.setSolvedAt(problem.getSolvedAt());

ProblemResponseDto problemResponseDto = ProblemResponseDto.builder()
.problemId(problem.getId())
.memo(problem.getMemo())
.reference(problem.getReference())
.solvedAt(problem.getSolvedAt())
.build();

// 이미지 데이터를 DTO에 설정
for (ImageData image : images) {
switch (image.getImageType()) {
case PROBLEM_IMAGE:
dto.setProblemImageUrl(image.getImageUrl());
problemResponseDto.setProblemImageUrl(image.getImageUrl());
break;
case PROCESS_IMAGE:
dto.setProcessImageUrl(image.getImageUrl());
problemResponseDto.setProcessImageUrl(image.getImageUrl());
break;
case ANSWER_IMAGE:
dto.setAnswerImageUrl(image.getImageUrl());
problemResponseDto.setAnswerImageUrl(image.getImageUrl());
break;
case SOLVE_IMAGE:
dto.setSolveImageUrl(image.getImageUrl());
problemResponseDto.setSolveImageUrl(image.getImageUrl());
break;
}
}

return dto;
return problemResponseDto;
}
}
22 changes: 22 additions & 0 deletions src/main/java/com/aisip/OnO/backend/converter/UserConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.aisip.OnO.backend.converter;

import com.aisip.OnO.backend.Dto.User.UserResponseDto;
import com.aisip.OnO.backend.entity.User;

public class UserConverter {

public static UserResponseDto convertToResponseDto(User user){

if (user == null) {
return null;
}

UserResponseDto userResponseDto = UserResponseDto.builder()
.userId(user.getUserId())
.userName(user.getName())
.userEmail(user.getEmail())
.build();

return userResponseDto;
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/aisip/OnO/backend/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long userId;

private String email;

private String name;
}

10 changes: 8 additions & 2 deletions src/main/java/com/aisip/OnO/backend/service/AuthService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.aisip.OnO.backend.service;

import com.aisip.OnO.backend.Dto.User.UserResponseDto;
import com.aisip.OnO.backend.converter.UserConverter;
import com.aisip.OnO.backend.repository.UserRepository;
import com.aisip.OnO.backend.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -19,10 +21,14 @@ public User registerOrLoginUser(String email, String name) {
user.setName(name);
userRepository.save(user);
}

return user;
}

public User getUserById(Long userId) {
return userRepository.findById(userId).orElse(null);
public UserResponseDto getUserById(Long userId) {

User user = userRepository.findById(userId).orElse(null);

return UserConverter.convertToResponseDto(user);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.DeleteObjectRequest;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -60,7 +62,7 @@ public ImageData saveImageData(String imageUrl, Problem problem, ImageType image
@Override
public String getProcessImageUrlFromProblemImageUrl(String problemImageUrl) {
RestTemplate restTemplate = new RestTemplate();
String url = fastApiUrl + "/show-url";
String url = fastApiUrl + "/process-color";

UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl(url)
.queryParam("full_url", problemImageUrl);
Expand All @@ -70,7 +72,20 @@ public String getProcessImageUrlFromProblemImageUrl(String problemImageUrl) {

// 응답 로그 출력
System.out.println("Response from server: " + response);
return response;

// JSON 응답 파싱
try {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readTree(response);
JsonNode pathNode = rootNode.path("path");
String inputPath = pathNode.path("output_path").asText();

return "https://" + bucket + ".s3.ap-northeast-2.amazonaws.com/" + inputPath;

} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Failed to parse response", e);
}
}

@Transactional(readOnly = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public boolean saveProblem(Long userId, ProblemRegisterDto problemRegisterDto) {

if (problemImageUrl != null && !problemImageUrl.isEmpty()) {
String processImageUrl = fileUploadService.getProcessImageUrlFromProblemImageUrl(problemImageUrl);
fileUploadService.saveImageData("processImageUrl", savedProblem, ImageType.PROCESS_IMAGE);
fileUploadService.saveImageData(processImageUrl, savedProblem, ImageType.PROCESS_IMAGE);
}

return true;
Expand Down

0 comments on commit c44665c

Please sign in to comment.