diff --git a/Spring-Boot/rest-example/pom.xml b/Spring-Boot/rest-example/pom.xml index 318f4cc..901d134 100644 --- a/Spring-Boot/rest-example/pom.xml +++ b/Spring-Boot/rest-example/pom.xml @@ -43,6 +43,10 @@ spring-boot-starter-test test + + org.springframework.boot + spring-boot-starter-data-jpa + diff --git a/Spring-Boot/rest-example/src/main/java/com/javadevjournal/restexample/controller/RegController.java b/Spring-Boot/rest-example/src/main/java/com/javadevjournal/restexample/controller/RegController.java new file mode 100644 index 0000000..6017d16 --- /dev/null +++ b/Spring-Boot/rest-example/src/main/java/com/javadevjournal/restexample/controller/RegController.java @@ -0,0 +1,30 @@ +package com.javadevjournal.restexample.controller; + +import com.javadevjournal.restexample.data.Customer; +import com.javadevjournal.restexample.data.Registration; +import com.javadevjournal.restexample.service.CustomerService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +public class RegController { + + @Autowired + private CustomerService customerService; + + @PostMapping("/new-registration") + public @ResponseBody Customer register(@RequestBody Registration registration){ + return customerService.saveCustomer(mapCustomerData(registration)); + } + + protected Customer mapCustomerData(Registration registration){ + Customer customer = new Customer(registration.getFirstName(),registration.getLastName(),registration.getEmail()); + customer.setAge(registration.getAge()); + return customer; + } +} diff --git a/Spring-Boot/rest-example/src/main/java/com/javadevjournal/restexample/controller/RegistrationController.java b/Spring-Boot/rest-example/src/main/java/com/javadevjournal/restexample/controller/RegistrationController.java new file mode 100644 index 0000000..ba03513 --- /dev/null +++ b/Spring-Boot/rest-example/src/main/java/com/javadevjournal/restexample/controller/RegistrationController.java @@ -0,0 +1,30 @@ +package com.javadevjournal.restexample.controller; + +import com.javadevjournal.restexample.data.Customer; +import com.javadevjournal.restexample.data.Registration; +import com.javadevjournal.restexample.service.CustomerService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class RegistrationController { + + @Autowired + private CustomerService customerService; + + @PostMapping("/registration") + public ResponseEntity register(@RequestBody Registration registration){ + Customer customer= customerService.saveCustomer(mapCustomerData(registration)); + return new ResponseEntity(customer,HttpStatus.OK); + } + + protected Customer mapCustomerData(Registration registration){ + Customer customer = new Customer(registration.getFirstName(),registration.getLastName(),registration.getEmail()); + customer.setAge(registration.getAge()); + return customer; + } +} diff --git a/Spring-Boot/rest-example/src/main/java/com/javadevjournal/restexample/data/Customer.java b/Spring-Boot/rest-example/src/main/java/com/javadevjournal/restexample/data/Customer.java index 442ffc0..3fc5725 100644 --- a/Spring-Boot/rest-example/src/main/java/com/javadevjournal/restexample/data/Customer.java +++ b/Spring-Boot/rest-example/src/main/java/com/javadevjournal/restexample/data/Customer.java @@ -12,6 +12,7 @@ public class Customer { private String firstName; private String lastName; private String email; + private int age; public Customer() { } @@ -53,4 +54,12 @@ public class Customer { public void setEmail(String email) { this.email = email; } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } } diff --git a/Spring-Boot/rest-example/src/main/java/com/javadevjournal/restexample/data/Registration.java b/Spring-Boot/rest-example/src/main/java/com/javadevjournal/restexample/data/Registration.java new file mode 100644 index 0000000..8e9140b --- /dev/null +++ b/Spring-Boot/rest-example/src/main/java/com/javadevjournal/restexample/data/Registration.java @@ -0,0 +1,50 @@ +package com.javadevjournal.restexample.data; + +public class Registration { + + private String firstName; + private String lastName; + private String email; + private int age; + private String password; + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } +} diff --git a/Spring-Boot/rest-example/src/main/java/com/javadevjournal/restexample/service/CustomerService.java b/Spring-Boot/rest-example/src/main/java/com/javadevjournal/restexample/service/CustomerService.java index 9c93845..1e3b6bd 100644 --- a/Spring-Boot/rest-example/src/main/java/com/javadevjournal/restexample/service/CustomerService.java +++ b/Spring-Boot/rest-example/src/main/java/com/javadevjournal/restexample/service/CustomerService.java @@ -6,4 +6,5 @@ public interface CustomerService { public Customer saveCustomer(Customer customer, String id); public Customer updateCustomer(String email, String id); + public Customer saveCustomer(Customer customer); } diff --git a/Spring-Boot/rest-example/src/main/java/com/javadevjournal/restexample/service/DefaultCustomerService.java b/Spring-Boot/rest-example/src/main/java/com/javadevjournal/restexample/service/DefaultCustomerService.java index 3192300..93a2087 100644 --- a/Spring-Boot/rest-example/src/main/java/com/javadevjournal/restexample/service/DefaultCustomerService.java +++ b/Spring-Boot/rest-example/src/main/java/com/javadevjournal/restexample/service/DefaultCustomerService.java @@ -27,4 +27,9 @@ public class DefaultCustomerService implements CustomerService { customer.setEmail(email); return customerRepository.save(customer); } + + @Override + public Customer saveCustomer(Customer customer) { + return customerRepository.save(customer); + } }