package server; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/dictionary") public class DictionaryController { private final Dictionary dictionary; @Autowired//tells Spring to automatically resolve and inject the required bean (object) into the class where it's used public DictionaryController(Dictionary dictionary) { this.dictionary = dictionary; } @PostMapping(value = "/add", consumes = {"application/json", "application/xml"}) public ResponseEntity addWord(@RequestBody Word word) { int result = dictionary.addWord(word.getWord().toLowerCase(), word.getDefinition()); if (result == -1) { return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).build(); } else { return ResponseEntity.ok().build(); } } @PutMapping(value = "/modify", consumes = {"application/json", "application/xml"}) public ResponseEntity changeDefinition(@RequestBody Word word) { int result = dictionary.changeWordDefinition(word.getWord().toLowerCase(), word.getDefinition()); if (result == -1) { return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); } else { return ResponseEntity.ok().build(); } } @GetMapping(value = "/get/{word}", produces = "text/plain") public ResponseEntity getDefinition(@PathVariable("word") String word) { String definition = dictionary.viewDefinition(word.toLowerCase()); if (definition == null) { return ResponseEntity.ok("Word not in dictionary"); } return ResponseEntity.ok("Definition of " + word + ": " + definition); } @DeleteMapping("/delete/{word}") public ResponseEntity deleteWord(@PathVariable("word") String word) { dictionary.deleteWord(word.toLowerCase()); return ResponseEntity.ok().build(); } }