Skip to content

Commit 4a6eea1

Browse files
REFACTOR: minor improvements for code reability and ADD bash script instructions on readme
1 parent dcaaf0d commit 4a6eea1

File tree

6 files changed

+57
-21
lines changed

6 files changed

+57
-21
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* JDK or OpenJDK - 1.8.x
2828
* Maven - 3.x.x
2929
* MySQL - 5.x.x or MariaDB 10.x.x
30+
* Spring - 2.0.0.RELEASE (inclued in pom.xml)
3031

3132
* Tested on:
3233
* GNU/Linux Solus 3
@@ -82,6 +83,12 @@ java -jar target/person-1.0.0.jar
8283
mvn spring-boot:run
8384
```
8485

86+
+ Also, you can run the app without packaging it using:
87+
88+
```bash
89+
./run.sh
90+
```
91+
8592
<p align="center"><img width=95% src="https://github.com/gmartinezramirez/Spring-API-REST-Person-CRUD/blob/master/docs/mvn-spring-bootrun.gif"></p>
8693

8794

pom.xml

Lines changed: 1 addition & 1 deletion
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.4.RELEASE</version>
17+
<version>2.0.0.RELEASE</version>
1818
<relativePath/> <!-- lookup parent from repository -->
1919
</parent>
2020

src/main/java/com/people23/person/controller/PersonController.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import org.springframework.http.ResponseEntity;
77
import org.springframework.web.bind.annotation.*;
88

9+
import com.people23.person.exception.PersonNotFoundException;
10+
911
import javax.validation.Valid;
1012
import java.util.List;
1113

@@ -25,40 +27,35 @@ public List<Person> getAllPersons() {
2527
}
2628

2729
@GetMapping("/persons/{id}")
28-
public ResponseEntity<Person> getPersonById(@PathVariable(value = "id") Long id) {
29-
Person person = personRepository.findOne(id);
30-
if (person == null) {
31-
return ResponseEntity.notFound().build();
32-
}
33-
return ResponseEntity.ok().body(person);
30+
public Person getPersonById(@PathVariable(value = "id") Long id) {
31+
return personRepository.findById(id)
32+
.orElseThrow(() -> new PersonNotFoundException("Person", "id", id));
3433
}
35-
34+
3635
@PostMapping("/persons")
3736
public Person createPerson(@Valid @RequestBody Person person) {
3837
return personRepository.save(person);
3938
}
4039

4140
@PutMapping("/persons/{id}")
42-
public ResponseEntity<Person> updatePerson(@PathVariable(value = "id") Long id,
43-
@Valid @RequestBody Person personDetails) {
44-
Person person = personRepository.findOne(id);
45-
if (person == null) {
46-
return ResponseEntity.notFound().build();
47-
}
41+
public Person updatePerson(@PathVariable(value = "id") Long id, @Valid @RequestBody Person personDetails) {
42+
43+
Person person = personRepository.findById(id)
44+
.orElseThrow(() -> new PersonNotFoundException("Person", "id", id));
45+
4846
person.setFirstName(personDetails.getFirstName());
4947
person.setLastName(personDetails.getLastName());
5048

5149
Person updatedPerson = personRepository.save(person);
52-
return ResponseEntity.ok(updatedPerson);
50+
return updatedPerson;
5351
}
5452

5553
@DeleteMapping("/persons/{id}")
5654
public ResponseEntity<Person> deletePerson(@PathVariable(value = "id") Long id) {
57-
Person person = personRepository.findOne(id);
58-
if (person == null) {
59-
return ResponseEntity.notFound().build();
60-
}
61-
55+
56+
Person person = personRepository.findById(id)
57+
.orElseThrow(() -> new PersonNotFoundException("Person", "id", id));
58+
6259
personRepository.delete(person);
6360
return ResponseEntity.ok().build();
6461
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.people23.person.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 PersonNotFoundException extends RuntimeException {
8+
private String personName;
9+
private String fieldName;
10+
private Object fieldValue;
11+
12+
public PersonNotFoundException( String personName, String fieldName, Object fieldValue) {
13+
super(String.format("%s not found with %s : '%s'", personName, fieldName, fieldValue));
14+
this.personName = personName;
15+
this.fieldName = fieldName;
16+
this.fieldValue = fieldValue;
17+
}
18+
19+
public String getPersonName() {
20+
return personName;
21+
}
22+
23+
public String getFieldName() {
24+
return fieldName;
25+
}
26+
27+
public Object getFieldValue() {
28+
return fieldValue;
29+
}
30+
}

src/main/java/com/people23/person/model/Person.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package com.people23.person.model;
22

33
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4-
import org.hibernate.validator.constraints.NotBlank;
54
import org.springframework.data.annotation.CreatedDate;
65
import org.springframework.data.annotation.LastModifiedDate;
76
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
87

98
import javax.persistence.*;
109
import java.util.Date;
10+
import javax.validation.constraints.NotBlank;
1111

1212
/**
1313
* Created by gmartinezramirez on 01/03/18.

src/main/java/com/people23/person/repository/PersonRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
import com.people23.person.model.Person;
44
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.stereotype.Repository;
56

67
/**
78
* Created by gmartinezramirez on 01/03/18.
89
* This use JpaRepository that extends the PagingAndSortingRepository that extends CRUDRepository.
910
*/
11+
@Repository
1012
public interface PersonRepository extends JpaRepository<Person, Long> {
1113

1214
}

0 commit comments

Comments
 (0)