0
\$\begingroup\$

I'd like to have code review for backend of todo app.

It has 2 main functionalities:

  1. Authentication and authorization using Spring Security and JWT token.

  2. CRUD for tasks

In particular I'd like to focus on code quality and database design.

https://github.com/redshift-7/todo-app

REST controller for managing tasks:

@Slf4j
@RequiredArgsConstructor
@CrossOrigin
@RestController
@RequestMapping("/api")
public class TaskController {

    private final TaskService taskService;

    @GetMapping("/tasks")
    public List<Task> all() {
        log.info("Request to get all tasks for current user");

        return taskService.findAll();
    }

    @GetMapping("/task/{id}")
    public ResponseEntity<Task> one(@PathVariable Long id) {
        log.info("Request to get tasks with id: {}", id);

        return taskService.getById(id).map(response -> ResponseEntity.ok().body(response))
                .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    @PostMapping("/tasks")
    public ResponseEntity<Task> newTask(@Valid @RequestBody Task task) throws URISyntaxException {
        log.info("Request to save new task item: {}", task);
        Task result = taskService.save(task);
        log.info("New task saved with id: {}", result.getId());

        return ResponseEntity.created(new URI("/api/task/" + result.getId())).body(result);
    }

    @PutMapping("/tasks/{id}")
    public ResponseEntity<Task> updateTask(@Valid @RequestBody Task newTask, @PathVariable Long id) {
        log.info("Request to update task with id: {}", id);
        Optional<Task> result = taskService.update(newTask);

        return result.map(task -> ResponseEntity.ok().body(task))
                .orElseGet(() -> ResponseEntity.notFound().build());
    }

    @DeleteMapping("/tasks/{id}")
    public ResponseEntity<HttpStatus> deleteTask(@PathVariable Long id) {
        log.info("Request to delete task with id: {}", id);
        taskService.delete(id);

        return ResponseEntity.noContent().build();
    }
}
\$\endgroup\$
4
  • \$\begingroup\$ Is the /task/{id} mapping (with singular instead of plural) intentional or a typo? \$\endgroup\$
    – Marvin
    Nov 30 at 13:29
  • \$\begingroup\$ @Marvin it seems intentional as also the newTask method contains the generation of the Location link that is created as part of the 201 Created response that points to /task/{id}. \$\endgroup\$ Nov 30 at 15:36
  • \$\begingroup\$ @Marvin, it's intentional \$\endgroup\$
    – J.Olufsen
    yesterday
  • \$\begingroup\$ Interesting, as that doesn't match with your logging ("Request to get task_s_ with id") and, more importantly, your PUT or DELETE mappings. \$\endgroup\$
    – Marvin
    yesterday

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.