Skip to content

Commit e6a2f67

Browse files
author
Rajeev Kumar Singh
committed
Spring Boot version updated to 2.0.0
1 parent 73022f7 commit e6a2f67

File tree

3 files changed

+48
-21
lines changed

3 files changed

+48
-21
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<parent>
1515
<groupId>org.springframework.boot</groupId>
1616
<artifactId>spring-boot-starter-parent</artifactId>
17-
<version>1.5.8.RELEASE</version>
17+
<version>2.0.0.RELEASE</version>
1818
<relativePath/> <!-- lookup parent from repository -->
1919
</parent>
2020

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.example.easynotes.controller;
22

3+
import com.example.easynotes.exception.ResourceNotFoundException;
34
import com.example.easynotes.model.Note;
45
import com.example.easynotes.repository.NoteRepository;
56
import org.springframework.beans.factory.annotation.Autowired;
@@ -24,42 +25,38 @@ public List<Note> getAllNotes() {
2425
return noteRepository.findAll();
2526
}
2627

27-
@GetMapping("/notes/{id}")
28-
public ResponseEntity<Note> getNoteById(@PathVariable(value = "id") Long noteId) {
29-
Note note = noteRepository.findOne(noteId);
30-
if(note == null) {
31-
return ResponseEntity.notFound().build();
32-
}
33-
return ResponseEntity.ok().body(note);
34-
}
35-
3628
@PostMapping("/notes")
3729
public Note createNote(@Valid @RequestBody Note note) {
3830
return noteRepository.save(note);
3931
}
4032

33+
@GetMapping("/notes/{id}")
34+
public Note getNoteById(@PathVariable(value = "id") Long noteId) {
35+
return noteRepository.findById(noteId)
36+
.orElseThrow(() -> new ResourceNotFoundException("Note", "id", noteId));
37+
}
38+
4139
@PutMapping("/notes/{id}")
42-
public ResponseEntity<Note> updateNote(@PathVariable(value = "id") Long noteId,
40+
public Note updateNote(@PathVariable(value = "id") Long noteId,
4341
@Valid @RequestBody Note noteDetails) {
44-
Note note = noteRepository.findOne(noteId);
45-
if(note == null) {
46-
return ResponseEntity.notFound().build();
47-
}
42+
43+
Note note = noteRepository.findById(noteId)
44+
.orElseThrow(() -> new ResourceNotFoundException("Note", "id", noteId));
45+
4846
note.setTitle(noteDetails.getTitle());
4947
note.setContent(noteDetails.getContent());
5048

5149
Note updatedNote = noteRepository.save(note);
52-
return ResponseEntity.ok(updatedNote);
50+
return updatedNote;
5351
}
5452

5553
@DeleteMapping("/notes/{id}")
56-
public ResponseEntity<Note> deleteNote(@PathVariable(value = "id") Long noteId) {
57-
Note note = noteRepository.findOne(noteId);
58-
if(note == null) {
59-
return ResponseEntity.notFound().build();
60-
}
54+
public ResponseEntity<?> deleteNote(@PathVariable(value = "id") Long noteId) {
55+
Note note = noteRepository.findById(noteId)
56+
.orElseThrow(() -> new ResourceNotFoundException("Note", "id", noteId));
6157

6258
noteRepository.delete(note);
59+
6360
return ResponseEntity.ok().build();
6461
}
6562
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.example.easynotes.exception;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.web.bind.annotation.ResponseStatus;
5+
6+
@ResponseStatus(value = HttpStatus.NOT_FOUND)
7+
public class ResourceNotFoundException extends RuntimeException {
8+
private String resourceName;
9+
private String fieldName;
10+
private Object fieldValue;
11+
12+
public ResourceNotFoundException( String resourceName, String fieldName, Object fieldValue) {
13+
super(String.format("%s not found with %s : '%s'", resourceName, fieldName, fieldValue));
14+
this.resourceName = resourceName;
15+
this.fieldName = fieldName;
16+
this.fieldValue = fieldValue;
17+
}
18+
19+
public String getResourceName() {
20+
return resourceName;
21+
}
22+
23+
public String getFieldName() {
24+
return fieldName;
25+
}
26+
27+
public Object getFieldValue() {
28+
return fieldValue;
29+
}
30+
}

0 commit comments

Comments
 (0)