Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

feat: updated spring annotations #85

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public TodoListController() {

@RequestMapping("/home")
public Map<String, Object> home() {
final Map<String, Object> model = new HashMap<String, Object>();
final Map<String, Object> model = new HashMap<>();
model.put("id", UUID.randomUUID().toString());
model.put("content", "home");
return model;
Expand All @@ -35,20 +35,19 @@ public Map<String, Object> home() {
/**
* HTTP GET
*/
@RequestMapping(value = "/api/todolist/{index}",
method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE})
@GetMapping(value = "/api/todolist/{index}", produces = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity<?> getTodoItem(@PathVariable("index") String index) {
try {
return new ResponseEntity<TodoItem>(todoItemRepository.findById(index).get(), HttpStatus.OK);
return new ResponseEntity<>(todoItemRepository.findById(index).get(), HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<String>(index + " not found", HttpStatus.NOT_FOUND);
return new ResponseEntity<>(index + " not found", HttpStatus.NOT_FOUND);
}
}

/**
* HTTP GET ALL
*/
@RequestMapping(value = "/api/todolist", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE})
@GetMapping(value = "/api/todolist", produces = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity<?> getAllTodoItems() {
try {
return new ResponseEntity<>(todoItemRepository.findAll(), HttpStatus.OK);
Expand All @@ -60,41 +59,41 @@ public ResponseEntity<?> getAllTodoItems() {
/**
* HTTP POST NEW ONE
*/
@RequestMapping(value = "/api/todolist", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
@PostMapping(value = "/api/todolist", consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> addNewTodoItem(@RequestBody TodoItem item) {
try {
item.setID(UUID.randomUUID().toString());
todoItemRepository.save(item);
return new ResponseEntity<String>("Entity created", HttpStatus.CREATED);
return new ResponseEntity<>("Entity created", HttpStatus.CREATED);
} catch (Exception e) {
return new ResponseEntity<String>("Entity creation failed", HttpStatus.CONFLICT);
return new ResponseEntity<>("Entity creation failed", HttpStatus.CONFLICT);
}
}

/**
* HTTP PUT UPDATE
*/
@RequestMapping(value = "/api/todolist", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
@PutMapping(value = "/api/todolist", consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> updateTodoItem(@RequestBody TodoItem item) {
try {
todoItemRepository.deleteById(item.getID());
todoItemRepository.save(item);
return new ResponseEntity<String>("Entity updated", HttpStatus.OK);
return new ResponseEntity<>("Entity updated", HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<String>("Entity updating failed", HttpStatus.NOT_FOUND);
return new ResponseEntity<>("Entity updating failed", HttpStatus.NOT_FOUND);
}
}

/**
* HTTP DELETE
*/
@RequestMapping(value = "/api/todolist/{id}", method = RequestMethod.DELETE)
@DeleteMapping(value = "/api/todolist/{id}")
public ResponseEntity<String> deleteTodoItem(@PathVariable("id") String id) {
try {
todoItemRepository.deleteById(id);
return new ResponseEntity<String>("Entity deleted", HttpStatus.OK);
return new ResponseEntity<>("Entity deleted", HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<String>("Entity deletion failed", HttpStatus.NOT_FOUND);
return new ResponseEntity<>("Entity deletion failed", HttpStatus.NOT_FOUND);
}

}
Expand Down