Skip to content

Commit 635873f

Browse files
committed
Finalize the api layer
1 parent 6d18b90 commit 635873f

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed
Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,40 @@
11
package lk.ijse.dep.note.api;
22

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;
612
@RestController
7-
@RequestMapping("api/v1/users/{userId:[A-F0-9\\~]{36}/notes")
13+
@RequestMapping("api/v1/users/{userId:[A-F0-9\\-]{36}/notes")
814
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+
}
940
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
package lk.ijse.dep.note.service;
22

3+
import lk.ijse.dep.note.dto.NoteDTO;
4+
import lk.ijse.dep.note.service.exception.NotFoundException;
5+
import java.util.List;
36
public interface Noteservice {
7+
8+
NoteDTO saveNote(NoteDTO note) throws NotFoundException;
9+
10+
void deleteNote(String userId,int noteId) throws NotFoundException;
11+
12+
List<NoteDTO> getAllNotes(String userId);
413
}

0 commit comments

Comments
 (0)