|
1 | 1 | package lk.ijse.dep.note.api; |
2 | 2 |
|
3 | | -import org.springframework.web.bind.annotation.RequestMapping; |
4 | | -import org.springframework.web.bind.annotation.RestController; |
5 | | - |
| 3 | +import lk.ijse.dep.note.dto.NoteDTO; |
| 4 | +import lk.ijse.dep.note.service.Noteservice; |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; |
| 6 | +import org.springframework.http.HttpStatus; |
| 7 | +import org.springframework.validation.Errors; |
| 8 | +import org.springframework.validation.annotation.Validated; |
| 9 | +import org.springframework.web.bind.annotation.*; |
| 10 | +import org.springframework.web.server.ResponseStatusException; |
| 11 | +import java.util.List; |
6 | 12 | @RestController |
7 | | -@RequestMapping("api/v1/users/{userId:[A-F0-9\\~]{36}/notes") |
| 13 | +@RequestMapping("api/v1/users/{userId:[A-F0-9\\-]{36}/notes") |
8 | 14 | public class NoteController { |
| 15 | + |
| 16 | + @Autowired |
| 17 | + private Noteservice noteservice; |
| 18 | + @ResponseStatus(HttpStatus.CREATED) |
| 19 | + @PostMapping(consumes = "application/json",produces = "application/json") |
| 20 | + public NoteDTO addNote(@PathVariable String userId, @RequestBody @Validated NoteDTO noteDTO, Errors errors){ |
| 21 | + if(errors.hasFieldErrors()){ |
| 22 | + throw new ResponseStatusException(HttpStatus.BAD_REQUEST,errors.getFieldErrors().get(0).getDefaultMessage()); |
| 23 | + } |
| 24 | + if(!userId.equals(noteDTO.getUserId())) throw new ResponseStatusException(HttpStatus.CONFLICT,"User Id mismatch"); |
| 25 | + |
| 26 | + //Todo: save this node |
| 27 | + return noteservice.saveNote(noteDTO); |
| 28 | + } |
| 29 | + |
| 30 | + @GetMapping(produces = "application/json") |
| 31 | + public List<NoteDTO> getAllNotes(@PathVariable String userId){ |
| 32 | + |
| 33 | + return noteservice.getAllNotes(userId); |
| 34 | + } |
| 35 | + @ResponseStatus(HttpStatus.NO_CONTENT) |
| 36 | + @DeleteMapping(path = "/{noteId:\\d+}") |
| 37 | + public void deleteNote(@PathVariable String userId,@PathVariable int noteId) { |
| 38 | + noteservice.deleteNote(userId,noteId); |
| 39 | + } |
9 | 40 | } |
0 commit comments