diff --git a/onlinecabbookingapp/src/main/java/com/cabBooking/controller/CustomerController.java b/onlinecabbookingapp/src/main/java/com/cabBooking/controller/CustomerController.java new file mode 100644 index 0000000..3b9d08b --- /dev/null +++ b/onlinecabbookingapp/src/main/java/com/cabBooking/controller/CustomerController.java @@ -0,0 +1,82 @@ +package com.cabBooking.controller; + +import java.util.List; + +import javax.validation.Valid; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +import com.cabBooking.exceptions.CustomerNotFound; +import com.cabBooking.exceptions.InValidId; +import com.cabBooking.models.Customer; +import com.cabBooking.services.CustomerServices; + +@RestController + +public class CustomerController { + @Autowired + private CustomerServices customerService; + + @PostMapping("/save") + public ResponseEntity insertCustomersHandler(@Valid @RequestBody Customer customer) throws CustomerNotFound{ + Customer cus = customerService.insertCustomer(customer); + + return new ResponseEntity(cus, HttpStatus.OK); + + } + + @PutMapping("/update/customer") + public ResponseEntity updateCustomerHandler(@RequestBody Customer customer) throws CustomerNotFound{ + + Customer cus = customerService.updateCustomer(customer); + + return new ResponseEntity(cus,HttpStatus.ACCEPTED); + } + + + @DeleteMapping("/delete/{id}") + public ResponseEntity deleteCustomerHandler(@PathVariable("id") Integer id) throws CustomerNotFound, InValidId{ + + Customer cus = customerService.deleteCustomer(id); + + return new ResponseEntity(cus, HttpStatus.OK); + + } + @GetMapping("/getAllCustomers") + public ResponseEntity> getAllCustomersHandler() throws CustomerNotFound{ + + List cus = customerService.viewCustomers(); + + return new ResponseEntity>(cus,HttpStatus.OK); + } + + @GetMapping("/customer/{id}") + public ResponseEntity getCustomerByIdHandler(@PathVariable("id") Integer id) throws InValidId{ + + Customer cus = customerService.viewCustomerById(id); + + return new ResponseEntity (cus,HttpStatus.OK); + +} + @PostMapping("/validateCustomer/{username}/{password}") +// @PostMapping("/validateCustomer") + public ResponseEntity vaildCustomerByUserName( @PathVariable("username") String username, @PathVariable("password") String password) throws CustomerNotFound{ + Customer cus = customerService.validateCustomer(username, password); + + return new ResponseEntity<>(cus,HttpStatus.ACCEPTED); + + + } + + + +} diff --git a/onlinecabbookingapp/src/main/java/com/cabBooking/exceptions/GlobalExceptionHandler.java b/onlinecabbookingapp/src/main/java/com/cabBooking/exceptions/GlobalExceptionHandler.java index 096d327..3e9f98e 100644 --- a/onlinecabbookingapp/src/main/java/com/cabBooking/exceptions/GlobalExceptionHandler.java +++ b/onlinecabbookingapp/src/main/java/com/cabBooking/exceptions/GlobalExceptionHandler.java @@ -60,4 +60,7 @@ public ResponseEntity inValidIdHandler ( InValidId id, WebRequest } + + + } diff --git a/onlinecabbookingapp/src/main/java/com/cabBooking/models/AbstractUser.java b/onlinecabbookingapp/src/main/java/com/cabBooking/models/AbstractUser.java index 228de2d..f653d9a 100644 --- a/onlinecabbookingapp/src/main/java/com/cabBooking/models/AbstractUser.java +++ b/onlinecabbookingapp/src/main/java/com/cabBooking/models/AbstractUser.java @@ -1,11 +1,27 @@ package com.cabBooking.models; +import javax.persistence.Entity; import javax.persistence.MappedSuperclass; import javax.validation.constraints.Email; +import javax.validation.constraints.Max; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.ToString; + + + +@Data +@AllArgsConstructor +@NoArgsConstructor +@ToString @MappedSuperclass + public class AbstractUser { @NotNull(message ="username should not be null") @@ -26,8 +42,11 @@ public class AbstractUser { private String email; - // constructor + public AbstractUser() { + + } + public AbstractUser( @NotNull(message = "username should not be null") @Size(min = 3, max = 15, message = "length of username must be between 3 & 15") String username, @NotNull(message = "user password should not be null") @Size(min = 3, max = 10, message = "length of username must be between 3 & 10") String password, @@ -42,11 +61,7 @@ public AbstractUser( this.email = email; } - public AbstractUser() { - - } - -// getter&& setter +// getter-address public String getUsername() { return username; @@ -88,7 +103,9 @@ public void setEmail(String email) { this.email = email; } -// toString + + +// tostring @Override public String toString() { @@ -98,6 +115,13 @@ public String toString() { + + + + + + + diff --git a/onlinecabbookingapp/src/main/java/com/cabBooking/models/Customer.java b/onlinecabbookingapp/src/main/java/com/cabBooking/models/Customer.java index b3f269d..4bafd49 100644 --- a/onlinecabbookingapp/src/main/java/com/cabBooking/models/Customer.java +++ b/onlinecabbookingapp/src/main/java/com/cabBooking/models/Customer.java @@ -15,6 +15,8 @@ public class Customer extends AbstractUser { private Integer customerId; + + //getter-setter public Integer getCustomerId() { return customerId; diff --git a/onlinecabbookingapp/src/main/java/com/cabBooking/repository/CustomerRepository.java b/onlinecabbookingapp/src/main/java/com/cabBooking/repository/CustomerRepository.java index dbfb9c6..5328c0d 100644 --- a/onlinecabbookingapp/src/main/java/com/cabBooking/repository/CustomerRepository.java +++ b/onlinecabbookingapp/src/main/java/com/cabBooking/repository/CustomerRepository.java @@ -8,5 +8,6 @@ @Repository public interface CustomerRepository extends JpaRepository { + public Customer findByMobileNumber(String mobileNumber); } diff --git a/onlinecabbookingapp/src/main/java/com/cabBooking/services/CustomerServiceImpl.java b/onlinecabbookingapp/src/main/java/com/cabBooking/services/CustomerServiceImpl.java new file mode 100644 index 0000000..6994a49 --- /dev/null +++ b/onlinecabbookingapp/src/main/java/com/cabBooking/services/CustomerServiceImpl.java @@ -0,0 +1,119 @@ +package com.cabBooking.services; + +import java.util.List; +import java.util.Optional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.cabBooking.exceptions.CustomerNotFound; +import com.cabBooking.exceptions.InValidId; +import com.cabBooking.models.Customer; +import com.cabBooking.repository.CustomerRepository; + + +@Service +public class CustomerServiceImpl implements CustomerServices { + + @Autowired + private CustomerRepository customerRepo; + + + @Override + public Customer insertCustomer(Customer customer) throws CustomerNotFound { + Customer existingCustomer = customerRepo.findByMobileNumber(customer.getMobileNumber()); + + if(existingCustomer!=null) { + throw new CustomerNotFound("Customer Already Register with this Mobile number"); + } + return customerRepo.save(customer); + } + + @Override + public Customer updateCustomer(Customer customer) throws CustomerNotFound { + + Optional cust = customerRepo.findById(customer.getCustomerId()); + + if(cust.isPresent()) { + Customer updateCustomer = customerRepo.save(customer) ; + + return updateCustomer; + }else { + throw new CustomerNotFound("customer detail Error."); + } + + + } + + @Override + public Customer deleteCustomer(Integer customerId) throws CustomerNotFound,InValidId{ + Optional cust = customerRepo.findById(customerId); + + if(cust.isPresent()) { + Customer cus = cust.get(); + customerRepo.delete(cus); + return cus; + + }else { + + throw new InValidId("customer id is invalid or not present:" + customerId); + } + + + + } + + @Override + public List viewCustomers()throws CustomerNotFound { + + List cust = customerRepo.findAll(); + + if(cust.size()==0) { + throw new CustomerNotFound("Customer not found"); + } + return cust; + + + } + + @Override + public Customer viewCustomerById(Integer customerId)throws InValidId{ + + Optional cust = customerRepo.findById(customerId); + + if(cust.isPresent()) { + Customer cus = cust.get(); + return cus; + + }else { + + throw new InValidId("customer id is invalid or not present:" + customerId); + } + + + } + + @Override + public Customer validateCustomer(String username, String password) throws CustomerNotFound{ + + List cust = customerRepo.findAll(); + + for(Customer c :cust) { + if(c.getUsername().equalsIgnoreCase(username) + && c.getPassword().equals(password)) { + return c; + + } + + } + + + throw new CustomerNotFound("Customer should not present with this username:" +username + "password :"+ password); + + } + + + + + +} diff --git a/onlinecabbookingapp/src/main/java/com/cabBooking/services/CustomerServices.java b/onlinecabbookingapp/src/main/java/com/cabBooking/services/CustomerServices.java index 21f3d24..64c43a2 100644 --- a/onlinecabbookingapp/src/main/java/com/cabBooking/services/CustomerServices.java +++ b/onlinecabbookingapp/src/main/java/com/cabBooking/services/CustomerServices.java @@ -1,8 +1,19 @@ package com.cabBooking.services; +import java.util.List; + +import com.cabBooking.exceptions.CustomerNotFound; +import com.cabBooking.exceptions.InValidId; +import com.cabBooking.models.Customer; public interface CustomerServices { + public Customer insertCustomer(Customer customer)throws CustomerNotFound; + public Customer updateCustomer(Customer customer) throws CustomerNotFound ; + public Customer deleteCustomer(Integer customerId) throws CustomerNotFound,InValidId; + public List viewCustomers()throws CustomerNotFound; + public Customer viewCustomerById(Integer customerId) throws InValidId ; + public Customer validateCustomer(String username, String password) throws CustomerNotFound; } diff --git a/onlinecabbookingapp/src/main/resources/application.properties b/onlinecabbookingapp/src/main/resources/application.properties index 8b13789..e69de29 100644 --- a/onlinecabbookingapp/src/main/resources/application.properties +++ b/onlinecabbookingapp/src/main/resources/application.properties @@ -1 +0,0 @@ -