Tutorials
This commit is contained in:
@@ -43,6 +43,10 @@
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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<Customer> register(@RequestBody Registration registration){
|
||||
Customer customer= customerService.saveCustomer(mapCustomerData(registration));
|
||||
return new ResponseEntity<Customer>(customer,HttpStatus.OK);
|
||||
}
|
||||
|
||||
protected Customer mapCustomerData(Registration registration){
|
||||
Customer customer = new Customer(registration.getFirstName(),registration.getLastName(),registration.getEmail());
|
||||
customer.setAge(registration.getAge());
|
||||
return customer;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user