-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[FEATURE] 감정분석에 관련된 api 설계
- Loading branch information
Showing
26 changed files
with
590 additions
and
23 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/java/com/sunjoo/sentimentAnalysis/client/DrinkClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
src/main/java/com/sunjoo/sentimentAnalysis/client/UserClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/com/sunjoo/sentimentAnalysis/controller/AnalysisController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
|
||
|
||
} | ||
|
20 changes: 0 additions & 20 deletions
20
src/main/java/com/sunjoo/sentimentAnalysis/domain/controller/AnalysController.java
This file was deleted.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
src/main/java/com/sunjoo/sentimentAnalysis/dto/AnalysisCreatedResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/sunjoo/sentimentAnalysis/dto/AnalysisHistory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/sunjoo/sentimentAnalysis/dto/AnalysisRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/com/sunjoo/sentimentAnalysis/dto/AnalysisResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
|
||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/sunjoo/sentimentAnalysis/dto/AnalysisResponses.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
48
src/main/java/com/sunjoo/sentimentAnalysis/dto/AnalysisResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
src/main/java/com/sunjoo/sentimentAnalysis/dto/DrinkResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
src/main/java/com/sunjoo/sentimentAnalysis/dto/UserDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/sunjoo/sentimentAnalysis/dto/UserInfoResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
Oops, something went wrong.