-
Notifications
You must be signed in to change notification settings - Fork 0
/
TodoController.java
48 lines (38 loc) · 1.33 KB
/
TodoController.java
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
package com.example.todo.controller;
import com.example.todo.model.Todo;
import com.example.todo.sevice.TodoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/api/todos")
public class TodoController {
@Autowired
private TodoService todoService;
@GetMapping
public List<Todo> getAllTodos() {
return todoService.findAll();
}
@GetMapping("/{id}")
public Todo getTodoById(@PathVariable String id) {
return todoService.findById(id).orElse(null);
}
@PostMapping
@PreAuthorize("hasAuthority('SCOPE_Todo.User')")
public Todo createTodo(@RequestBody Todo todo, Authentication authentication) {
return todoService.save(todo);
}
@PutMapping("/{id}")
@PreAuthorize("hasAuthority('SCOPE_Todo.User')")
public Todo updateTodo(@PathVariable String id, @RequestBody Todo todo) {
return todoService.save(todo);
}
@DeleteMapping("/{id}")
@PreAuthorize("hasAuthority('SCOPE_Todo.Admin')")
public void deleteTodo(@PathVariable String id) {
todoService.deleteById(id);
}
}