-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathTaskController.java
More file actions
54 lines (46 loc) · 1.79 KB
/
Copy pathTaskController.java
File metadata and controls
54 lines (46 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package br.ce.wcaquino.taskbackend.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import br.ce.wcaquino.taskbackend.model.Task;
import br.ce.wcaquino.taskbackend.repo.TaskRepo;
import br.ce.wcaquino.taskbackend.utils.DateUtils;
import br.ce.wcaquino.taskbackend.utils.ValidationException;
@RestController
@RequestMapping(value ="/todo")
public class TaskController {
@Autowired
private TaskRepo todoRepo;
@GetMapping
public List<Task> findAll() {
return todoRepo.findAll();
}
@PostMapping
public ResponseEntity<Task> save(@RequestBody Task todo) throws ValidationException {
if(todo.getTask() == null || todo.getTask() == "") {
throw new ValidationException("Fill the task description");
}
if(todo.getDueDate() == null) {
throw new ValidationException("Fill the due date");
}
if(!DateUtils.isEqualOrFutureDate(todo.getDueDate())) {
throw new ValidationException("Due date must not be in past");
}
Task saved = todoRepo.save(todo);
return new ResponseEntity<Task>(saved, HttpStatus.CREATED);
}
@DeleteMapping(value = "/{id}")
@ResponseStatus(code = HttpStatus.NO_CONTENT)
public void delete(@PathVariable Long id) {
todoRepo.deleteById(id);
}
}