1414
1515import com .fd .jpa .converter .CycleAvoidingMappingContext ;
1616import com .fd .jpa .converter .PersonEntityMapper ;
17- import com .fd .jpa .entity .PersonEntityModel ;
17+ import com .fd .jpa .entity .PersonEntity ;
1818import com .fd .jpa .model .Person ;
19+ import com .fd .jpa .repository .person .PersonRepository ;
20+ import com .fd .jpa .repository .person .PersonSpecifications ;
1921import lombok .extern .slf4j .Slf4j ;
2022import org .springframework .beans .factory .annotation .Autowired ;
23+ import org .springframework .data .jpa .domain .Specification ;
2124import org .springframework .web .bind .annotation .GetMapping ;
25+ import org .springframework .web .bind .annotation .PathVariable ;
2226import org .springframework .web .bind .annotation .PostMapping ;
2327import org .springframework .web .bind .annotation .RequestBody ;
2428import org .springframework .web .bind .annotation .RequestMapping ;
29+ import org .springframework .web .bind .annotation .RequestParam ;
2530import org .springframework .web .bind .annotation .RestController ;
2631
2732import javax .servlet .http .HttpServletRequest ;
28- import java .time .LocalDate ;
33+ import java .util .List ;
34+ import java .util .Optional ;
2935
3036/**
3137 * @author furkan.danismaz
@@ -39,16 +45,32 @@ public class PersonController extends BaseController {
3945 @ Autowired
4046 private PersonEntityMapper personEntityMapper ;
4147
48+ @ Autowired
49+ private PersonRepository personRepository ;
50+
4251 @ GetMapping ("/{id}" )
43- public Person getPerson () {
44- return new Person (1 , "furkan" , "danismaz" , LocalDate .of (1985 , 12 , 7 ));
52+ public Person getPerson (@ PathVariable int id ) {
53+ Optional <PersonEntity > queryResult = this .personRepository .findById (id );
54+ if (queryResult .isPresent ()) {
55+ return this .personEntityMapper .toBusinessObject (queryResult .get (), new CycleAvoidingMappingContext (), this .getRequestTimeZone ());
56+ }
57+ return null ;
58+ }
59+
60+ @ GetMapping ("/search" )
61+ public List <Person > search (@ RequestParam String name , @ RequestParam String surname ) {
62+ List <PersonEntity > searchResult = this .personRepository .findAll (
63+ Specification .where (PersonSpecifications .withName (name ))
64+ .and (PersonSpecifications .withSurname (surname )));
65+ return this .personEntityMapper .toBusinessObjectList (searchResult , new CycleAvoidingMappingContext ());
4566 }
4667
4768 @ PostMapping
4869 public Person savePerson (@ RequestBody Person person , HttpServletRequest request ) {
49- PersonEntityModel personEntityModel = this .personEntityMapper .toEntityModel (person , new CycleAvoidingMappingContext (), this .getRequestTimeZone ());
50- log .debug (personEntityModel .toString ());
51- return null ;
70+ PersonEntity personEntity = this .personEntityMapper .toEntity (person , new CycleAvoidingMappingContext (), this .getRequestTimeZone ());
71+ this .personRepository .save (personEntity );
72+ person .setId (personEntity .getId ());
73+ return person ;
5274 }
5375
5476}
0 commit comments