|
| 1 | +// Copyright (c) 2023, Oracle and/or its affiliates. |
| 2 | +// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ |
| 3 | + |
| 4 | +package com.example.customer.controller; |
| 5 | + |
| 6 | +import java.util.List; |
| 7 | +import java.util.Optional; |
| 8 | + |
| 9 | +import com.example.customer.model.Customers; |
| 10 | +import com.example.customer.repository.CustomersRepository; |
| 11 | +import org.springframework.http.HttpStatus; |
| 12 | +import org.springframework.http.ResponseEntity; |
| 13 | +import org.springframework.web.bind.annotation.DeleteMapping; |
| 14 | +import org.springframework.web.bind.annotation.GetMapping; |
| 15 | +import org.springframework.web.bind.annotation.PathVariable; |
| 16 | +import org.springframework.web.bind.annotation.PostMapping; |
| 17 | +import org.springframework.web.bind.annotation.PutMapping; |
| 18 | +import org.springframework.web.bind.annotation.RequestBody; |
| 19 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 20 | +import org.springframework.web.bind.annotation.ResponseStatus; |
| 21 | +import org.springframework.web.bind.annotation.RestController; |
| 22 | + |
| 23 | +@RestController |
| 24 | +@RequestMapping("/api/v1") |
| 25 | +public class CustomerController { |
| 26 | + final CustomersRepository customersRepository; |
| 27 | + |
| 28 | + public CustomerController(CustomersRepository customersRepository) { |
| 29 | + this.customersRepository = customersRepository; |
| 30 | + } |
| 31 | + |
| 32 | + @ResponseStatus(HttpStatus.OK) |
| 33 | + @GetMapping("/customer") |
| 34 | + public List<Customers> findAll() { |
| 35 | + return customersRepository.findAll(); |
| 36 | + } |
| 37 | + |
| 38 | + @ResponseStatus(HttpStatus.OK) |
| 39 | + @GetMapping("/customer/name/{customerName}") |
| 40 | + public List<Customers> findByCustomerByName(@PathVariable String customerName) { |
| 41 | + return customersRepository.findByCustomerNameIsContaining(customerName); |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + /** |
| 46 | + * Get Customer with specific ID. |
| 47 | + * @param id The CustomerId |
| 48 | + * @return If the customers is found, a customer and HTTP Status code. |
| 49 | + */ |
| 50 | + @GetMapping("/customer/{id}") |
| 51 | + public ResponseEntity<Customers> getCustomerById(@PathVariable("id") String id) { |
| 52 | + Optional<Customers> customerData = customersRepository.findById(id); |
| 53 | + try { |
| 54 | + return customerData.map(customers -> new ResponseEntity<>(customers, HttpStatus.OK)) |
| 55 | + .orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND)); |
| 56 | + } catch (Exception e) { |
| 57 | + return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Get customer that contains an email. |
| 63 | + * @param email of the customer |
| 64 | + * @return Returns a customer if found |
| 65 | + */ |
| 66 | + @GetMapping("/customer/byemail/{email}") |
| 67 | + public List<Customers> getCustomerByEmail(@PathVariable("email") String email) { |
| 68 | + return customersRepository.findByCustomerEmailIsContaining(email); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Create a customer. |
| 73 | + * @param customer Customer object with the customer details |
| 74 | + * @return Returns a HTTP Status code |
| 75 | + */ |
| 76 | + @PostMapping("/customer") |
| 77 | + public ResponseEntity<Customers> createCustomer(@RequestBody Customers customer) { |
| 78 | + try { |
| 79 | + Customers newCustomer = customersRepository.save(new Customers( |
| 80 | + customer.getCustomerId(), |
| 81 | + customer.getCustomerName(), |
| 82 | + customer.getCustomerEmail(), |
| 83 | + customer.getCustomerOtherDetails())); |
| 84 | + return new ResponseEntity<>(newCustomer, HttpStatus.CREATED); |
| 85 | + |
| 86 | + } catch (Exception e) { |
| 87 | + return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Update a specific Customer (ID). |
| 93 | + * @param id The id of the customer |
| 94 | + * @param customer A customer object |
| 95 | + * @return A Http Status code |
| 96 | + */ |
| 97 | + @PutMapping("/customer/{id}") |
| 98 | + public ResponseEntity<Customers> updateCustomer(@PathVariable("id") String id, @RequestBody Customers customer) { |
| 99 | + Optional<Customers> customerData = customersRepository.findById(id); |
| 100 | + try { |
| 101 | + if (customerData.isPresent()) { |
| 102 | + Customers updCustomer = customerData.get(); |
| 103 | + updCustomer.setCustomerName(customer.getCustomerName()); |
| 104 | + updCustomer.setCustomerEmail(customer.getCustomerEmail()); |
| 105 | + updCustomer.setCustomerOtherDetails(customer.getCustomerOtherDetails()); |
| 106 | + return new ResponseEntity<>(customersRepository.save(updCustomer), HttpStatus.OK); |
| 107 | + } else { |
| 108 | + return new ResponseEntity<>(HttpStatus.NOT_FOUND); |
| 109 | + } |
| 110 | + } catch (Exception e) { |
| 111 | + return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Delete a specific customer (ID). |
| 117 | + * @param customerId the Id of the customer to be deleted |
| 118 | + * @return A Http Status code |
| 119 | + */ |
| 120 | + @DeleteMapping("/customer/{customerId}") |
| 121 | + public ResponseEntity<HttpStatus> deleteCustomer(@PathVariable("customerId") String customerId) { |
| 122 | + try { |
| 123 | + customersRepository.deleteById(customerId); |
| 124 | + return new ResponseEntity<>(HttpStatus.NO_CONTENT); |
| 125 | + } catch (Exception e) { |
| 126 | + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Method isn't implemented. |
| 132 | + * @param amount Loan amount |
| 133 | + * @return A Http Status |
| 134 | + */ |
| 135 | + @PostMapping("/customer/applyLoan/{amount}") |
| 136 | + public ResponseEntity<HttpStatus> applyForLoan(@PathVariable ("amount") long amount) { |
| 137 | + try { |
| 138 | + // Check Credit Rating |
| 139 | + // Amount vs Rating approval? |
| 140 | + // Create Account |
| 141 | + // Update Account Balance |
| 142 | + // Notify |
| 143 | + return new ResponseEntity<>(HttpStatus.I_AM_A_TEAPOT); |
| 144 | + } catch (Exception e) { |
| 145 | + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments