Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ujjawal #18

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<Customer> insertCustomersHandler(@Valid @RequestBody Customer customer) throws CustomerNotFound{
Customer cus = customerService.insertCustomer(customer);

return new ResponseEntity<Customer>(cus, HttpStatus.OK);

}

@PutMapping("/update/customer")
public ResponseEntity<Customer> updateCustomerHandler(@RequestBody Customer customer) throws CustomerNotFound{

Customer cus = customerService.updateCustomer(customer);

return new ResponseEntity<Customer>(cus,HttpStatus.ACCEPTED);
}


@DeleteMapping("/delete/{id}")
public ResponseEntity<Customer> deleteCustomerHandler(@PathVariable("id") Integer id) throws CustomerNotFound, InValidId{

Customer cus = customerService.deleteCustomer(id);

return new ResponseEntity<Customer>(cus, HttpStatus.OK);

}
@GetMapping("/getAllCustomers")
public ResponseEntity<List<Customer>> getAllCustomersHandler() throws CustomerNotFound{

List<Customer> cus = customerService.viewCustomers();

return new ResponseEntity<List<Customer>>(cus,HttpStatus.OK);
}

@GetMapping("/customer/{id}")
public ResponseEntity<Customer> getCustomerByIdHandler(@PathVariable("id") Integer id) throws InValidId{

Customer cus = customerService.viewCustomerById(id);

return new ResponseEntity<Customer> (cus,HttpStatus.OK);

}
@PostMapping("/validateCustomer/{username}/{password}")
// @PostMapping("/validateCustomer")
public ResponseEntity<Customer> vaildCustomerByUserName( @PathVariable("username") String username, @PathVariable("password") String password) throws CustomerNotFound{
Customer cus = customerService.validateCustomer(username, password);

return new ResponseEntity<>(cus,HttpStatus.ACCEPTED);


}



}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,7 @@ public ResponseEntity<MyErrorDetail> inValidIdHandler ( InValidId id, WebRequest
}





}
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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,
Expand All @@ -42,11 +61,7 @@ public AbstractUser(
this.email = email;
}

public AbstractUser() {

}

// getter&& setter
// getter-address

public String getUsername() {
return username;
Expand Down Expand Up @@ -88,7 +103,9 @@ public void setEmail(String email) {
this.email = email;
}

// toString


// tostring

@Override
public String toString() {
Expand All @@ -98,6 +115,13 @@ public String toString() {













Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class Customer extends AbstractUser {
private Integer customerId;




//getter-setter
public Integer getCustomerId() {
return customerId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
@Repository
public interface CustomerRepository extends JpaRepository<Customer, Integer> {

public Customer findByMobileNumber(String mobileNumber);

}
Original file line number Diff line number Diff line change
@@ -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<Customer> 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<Customer> 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<Customer> viewCustomers()throws CustomerNotFound {

List<Customer> cust = customerRepo.findAll();

if(cust.size()==0) {
throw new CustomerNotFound("Customer not found");
}
return cust;


}

@Override
public Customer viewCustomerById(Integer customerId)throws InValidId{

Optional<Customer> 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<Customer> 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);

}





}
Original file line number Diff line number Diff line change
@@ -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<Customer> viewCustomers()throws CustomerNotFound;
public Customer viewCustomerById(Integer customerId) throws InValidId ;
public Customer validateCustomer(String username, String password) throws CustomerNotFound;


}
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@