1 package lv.id.jc.piglatin.controller;
2
3
4 import org.springframework.http.HttpStatus;
5 import org.springframework.http.ResponseEntity;
6 import org.springframework.web.bind.annotation.RestController;
7
8 import lv.id.jc.piglatin.api.PigLatinApi;
9 import lv.id.jc.piglatin.model.TranslationRequest;
10 import lv.id.jc.piglatin.model.TranslationResponse;
11 import lv.id.jc.piglatin.service.TranslationService;
12
13
14
15
16 @RestController
17 public class PigLatinController implements PigLatinApi {
18
19 private final TranslationService translationService;
20
21 public PigLatinController(TranslationService translationService) {
22 this.translationService = translationService;
23 }
24
25 @Override
26 public ResponseEntity<TranslationResponse> translate(TranslationRequest translationRequest) {
27 var textEnglish = translationRequest.getSentence();
28 var textPigLatin = translationService.translate(textEnglish);
29 if (textPigLatin.equalsIgnoreCase(textEnglish)) {
30 return new ResponseEntity<>(HttpStatus.I_AM_A_TEAPOT);
31 }
32 return new ResponseEntity<>(new TranslationResponse(textPigLatin), HttpStatus.OK);
33 }
34 }