Skip to content

Commit

Permalink
Add ConvertService and CovertController in backend
Browse files Browse the repository at this point in the history
  • Loading branch information
kohei-s committed Oct 6, 2023
1 parent b4b87cc commit 073d599
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 24 deletions.
29 changes: 5 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,10 @@ Children between 5-15 years old learning Japanese as (one of) their home languag
(** upcoming features)
## 6. Tech Stack
### Backend
- Java
- Spring Boot
- Maven
- Lombok
- REST
- DeepL API
Java, Spring Boot, Maven, REST, MongoDB, DeepL API, goo API
### Frontend
- JavaScript
- TypeScript
- CSS
- HTML
- React
- Vite
- Axios
- MUI
### Data Bank
- MongoDB
JavaScript, TypeScript, CSS, HTML, React, Vite, MUI
### TDD
- JUnit
- Mockito
- SonarCloud
- DeepL API mock server
### CI/CD
- GitHub Actions
- Docker
- AWS
JUnit, Mockito, SonarLint, SonarCloud
### DevOps, CI/CD
Docker, GitHub Actions, AWS
9 changes: 9 additions & 0 deletions backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,20 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>com.deepl.api</groupId>
<artifactId>deepl-java</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>73.2</version>
</dependency>
</dependencies>
<build>
<finalName>kotokoapp</finalName>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package de.neuefische.koheis.backend.converter;

public record Conversion(
String kanji,
String kana,
String alphabet
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package de.neuefische.koheis.backend.converter;

import de.neuefische.koheis.backend.translation.Translation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("api/converter")
public class ConvertController {

private final ConvertService convertService;

public ConvertController(ConvertService convertService) {
this.convertService = convertService;
}

@PostMapping
public Conversion convertTranslationToConversion(@RequestBody Translation translation) {
return convertService.convertTranslationToConversion(translation);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package de.neuefische.koheis.backend.converter;

import de.neuefische.koheis.backend.translation.Translation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import java.util.Objects;

@Service
public class ConvertService {

private final GooConfig gooConfig;
private final WebClient webClient;
private final KanaConverter kanaConverter;

public ConvertService(GooConfig gooConfig, @Value("${goo.api.url}") String url, KanaConverter kanaConverter) {
this.gooConfig = gooConfig;
this.webClient = WebClient.create(url);
this.kanaConverter = kanaConverter;
}

public GooRequest getGooRequest(String original, String kanaType) {
String appId = gooConfig.getId();
return new GooRequest(appId, original, kanaType);
}

public String convertKanjiToKana(GooRequest gooRequest) {
return Objects.requireNonNull(webClient.post()
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(gooRequest)
.retrieve()
.toEntity(GooResponse.class)
.block()).getBody().converted();
}

public String convertKanaToAlphabet(String kana) {
if (kana.matches("^[\\u3040-\\u309F]+$")) {
return kanaConverter.convertHiraganaToAlphabet(kana);
} else if (kana.matches("^[\\u30A0-\\u30FF]+$")) {
return kanaConverter.convertKatakanaToAlphabet(kana);
} else {
return "Invalid input. Input must be hiragana or katakana!";
}
}

public Conversion convertTranslationToConversion(Translation translation) {
String translated = translation.getJapanese();
if (translated.matches("^[\\u4E00-\\u9FFF]+$")) {
String kana = convertKanjiToKana(getGooRequest(translated, "hiragana"));
String alphabet = convertKanaToAlphabet(kana);
return new Conversion(translated, kana, alphabet);
} else if ((translated.matches("^[\\u3040-\\u309F]+$")) || (translation.getJapanese().matches("^[\\u30A0-\\u30FF]+$"))) {
String alphabet = convertKanaToAlphabet(translated);
return new Conversion("", translated, alphabet);
} else {
return new Conversion("", "", "");
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package de.neuefische.koheis.backend.converter;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class GooConfig {

@Value("${goo.api.id}")
private String apiId;

public String getId() {
return apiId;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package de.neuefische.koheis.backend.converter;

public record GooRequest(
String app_id,
String sentence,
String output_type
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package de.neuefische.koheis.backend.converter;

public record GooResponse(
String converted
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package de.neuefische.koheis.backend.converter;

import com.ibm.icu.text.Transliterator;
import org.springframework.stereotype.Component;

@Component
public class KanaConverter {

public String convertHiraganaToAlphabet(String hiragana) {
Transliterator trans = Transliterator.getInstance("Hiragana-Latin");
return trans.transliterate(hiragana);

}

public String convertKatakanaToAlphabet(String katakana) {
Transliterator trans = Transliterator.getInstance("Katakana-Latin");
return trans.transliterate(katakana);
}

}
2 changes: 2 additions & 0 deletions backend/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
spring.data.mongodb.uri=${MONGO_DB_URI}
server.error.include-message=always
deepl.api.key=${DEEPL_API_KEY}
goo.api.id=${GOO_API_ID}
goo.api.url=${GOO_API_URL}

0 comments on commit 073d599

Please sign in to comment.