Skip to content

Commit

Permalink
Merge pull request #4 from SunjooAI/#2/feature
Browse files Browse the repository at this point in the history
[FEATURE] 감정분석에 관련된 api 설계
  • Loading branch information
SunYerim authored Jun 30, 2024
2 parents a9d165d + 0ed3b0c commit 6289c23
Show file tree
Hide file tree
Showing 26 changed files with 590 additions and 23 deletions.
1 change: 0 additions & 1 deletion .idea/modules/sentimentAnalysis.main.iml

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

2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
testImplementation 'com.h2database:h2'
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
implementation group: 'org.json', name: 'json', version: '20231013'
}

dependencyManagement {
Expand Down
Binary file modified build/libs/sentimentAnalysis-0.0.1-SNAPSHOT-plain.jar
Binary file not shown.
Binary file modified build/libs/sentimentAnalysis-0.0.1-SNAPSHOT.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients(basePackages = "com.sunjoo.sentimentAnalysis.client")
public class SentimentAnalysisApplication {

public static void main(String[] args) {
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/sunjoo/sentimentAnalysis/client/DrinkClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.sunjoo.sentimentAnalysis.client;

import com.sunjoo.sentimentAnalysis.dto.DrinkResponse;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(name = "drinktionary-service", url = "http://sunjoo-server-drinktionary-drinktionary-1:8091")
public interface DrinkClient {

@GetMapping("/drinks/{drinkId}")
DrinkResponse getDrinkById(@PathVariable("drinkId") Long drinkId, @RequestHeader("Authorization") String token);

@GetMapping("/drinks/recommend")
DrinkResponse getRecommendedDrink(@RequestParam("sentiment") String sentiment, @RequestHeader("Authorization") String token);
}
13 changes: 13 additions & 0 deletions src/main/java/com/sunjoo/sentimentAnalysis/client/UserClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.sunjoo.sentimentAnalysis.client;

import com.sunjoo.sentimentAnalysis.dto.UserInfoResponse;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;

@FeignClient(name = "auth-service", url = "http://sunjoo-server-auth-spring-1:8090")
public interface UserClient {

@GetMapping("/auth/userinfo")
UserInfoResponse getUserInfo(@RequestHeader("Authorization") String token);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.sunjoo.sentimentAnalysis.controller;

import com.sunjoo.sentimentAnalysis.client.UserClient;
import com.sunjoo.sentimentAnalysis.dto.*;
import com.sunjoo.sentimentAnalysis.service.AnalysisService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequiredArgsConstructor
@Slf4j
@RestController
public class AnalysisController {

private final AnalysisService analysisService;
private final UserClient userClient;

@GetMapping("/{analysis_id}")
public ResponseEntity<AnalysisResponse> findAnalysis(@PathVariable(value = "analysis_id") Long analysisId, @RequestHeader("Authorization") String token) {
final AnalysisResponse analysis = analysisService.findAnalysisById(analysisId, token);

return ResponseEntity.ok(analysis);
}

@GetMapping
public ResponseEntity<List<AnalysisHistory>> findHistoriesByUserId(@RequestHeader("Authorization") String token) {
// userId 조회
UserInfoResponse userInfoResponse = userClient.getUserInfo("Bearer" + token);
UserDTO user = userInfoResponse.getResult();

final List<AnalysisHistory> histories = analysisService.findHistoriesByUserId(user.getId());

return ResponseEntity.ok(histories);
}

@PostMapping("/sources")
public ResponseEntity<AnalysisCreatedResponse> createAnalysisSource(@RequestHeader("Authorization") String token,
@RequestBody AnalysisRequest sourceRequest) {

// userId 조회
UserInfoResponse userInfoResponse = userClient.getUserInfo("Bearer" + token);
UserDTO user = userInfoResponse.getResult();

final AnalysisResponse analysisResponse = analysisService.postAnalysis(user.getUserNo(), sourceRequest, token);

return ResponseEntity.ok(new AnalysisCreatedResponse(analysisResponse.getId()));
}


}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.sunjoo.sentimentAnalysis.dto;


import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public final class AnalysisCreatedResponse {
private final Long id;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.sunjoo.sentimentAnalysis.dto;

import com.sunjoo.sentimentAnalysis.entity.Analysis;
import com.sunjoo.sentimentAnalysis.entity.Sentiment;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.time.LocalDateTime;

@Getter
@RequiredArgsConstructor
public class AnalysisHistory {
private final Long resultId;
private final LocalDateTime date;
private final Sentiment sentiment;

public static AnalysisHistory from(final AnalysisResponse analysis) {
return new AnalysisHistory(analysis.getId(), analysis.getDate(), analysis.getSentiment());
}

public static AnalysisHistory from(final Analysis analysis) {
return new AnalysisHistory(analysis.getResultId(), analysis.getDate(), analysis.getSentiment());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.sunjoo.sentimentAnalysis.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Getter
@Setter
@NoArgsConstructor
public class AnalysisRequest {

private String textExpression;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm")
private LocalDateTime date = LocalDateTime.now();

public boolean isNotExistExpressions() {
return textExpression == null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.sunjoo.sentimentAnalysis.dto;

import com.sunjoo.sentimentAnalysis.client.DrinkClient;
import com.sunjoo.sentimentAnalysis.entity.Analysis;
import com.sunjoo.sentimentAnalysis.entity.Sentiment;
import lombok.Builder;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.time.LocalDateTime;

@Getter
@RequiredArgsConstructor
@Builder
public class AnalysisResponse {

private final Long id;
private final String textExpression;
private final LocalDateTime date;
private final Sentiment sentiment;
private final Long drinkId;
private final String name;
private final double dosu;
private final int price;
private final int volume;
private final int sweetness;
private final String drinkImageUrl;
private final String type;

public static AnalysisResponse from(final Analysis analysis, final DrinkResponse drinkResponse) {

return AnalysisResponse.builder()
.id(analysis.getResultId())
.textExpression(analysis.getTextExpression())
.date(analysis.getDate())
.sentiment(analysis.getSentiment())
.drinkId(analysis.getDrinkId())
.name(drinkResponse.getName())
.dosu(drinkResponse.getDosu())
.price(drinkResponse.getPrice())
.volume(drinkResponse.getVolume())
.sweetness(drinkResponse.getSweetness())
.drinkImageUrl(drinkResponse.getDrinkImageUrl())
.type(drinkResponse.getType())
.build();
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.sunjoo.sentimentAnalysis.dto;

import com.sunjoo.sentimentAnalysis.entity.Analysis;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.List;

@Getter
@RequiredArgsConstructor
public class AnalysisResponses {
private final List<AnalysisResponse> list;

public static AnalysisResponses of(final List<AnalysisResponse> responses) {
return new AnalysisResponses(responses);
}
}
48 changes: 48 additions & 0 deletions src/main/java/com/sunjoo/sentimentAnalysis/dto/AnalysisResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.sunjoo.sentimentAnalysis.dto;

import com.sunjoo.sentimentAnalysis.entity.Sentiment;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class AnalysisResult {
private double positive;

public static AnalysisResult of(final String emotionString, final double confidence) {
final AnalysisResult analysisResult = new AnalysisResult();
if (emotionString.equals("smile")) {
analysisResult.setPositive(confidence);
} else if (emotionString.equals("neutral")) {
analysisResult.setPositive(confidence / 2);
} else if (emotionString.equals("laugh")) {
analysisResult.setPositive(confidence);
} else if (emotionString.equals("taliking")) {
analysisResult.setPositive(confidence / 2);
}

return analysisResult;
}

public static AnalysisResult of(final double positive, final double neutral) {
return new AnalysisResult(positive + neutral / 2);
}

public Sentiment getSentiment() {
if (positive < 20) {
return Sentiment.SAD2;
} else if (positive < 40) {
return Sentiment.SAD1;
} else if (positive < 60) {
return Sentiment.MEDIAN;
} else if (positive < 80) {
return Sentiment.Happy1;
} else {
return Sentiment.HAPPY2;
}
}
}
17 changes: 17 additions & 0 deletions src/main/java/com/sunjoo/sentimentAnalysis/dto/DrinkResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.sunjoo.sentimentAnalysis.dto;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public class DrinkResponse {
private Long id;
private String name;
private double dosu;
private int price;
private int volume;
private int sweetness;
private String drinkImageUrl;
private String type;
}
13 changes: 13 additions & 0 deletions src/main/java/com/sunjoo/sentimentAnalysis/dto/UserDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.sunjoo.sentimentAnalysis.dto;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@Getter
public class UserDTO {
private Long userNo;
private String id;
private String name;
private String type;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.sunjoo.sentimentAnalysis.dto;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@Getter
public class UserInfoResponse {
private String resultCode;
private UserDTO result;

}
Loading

0 comments on commit 6289c23

Please sign in to comment.