diff --git a/src/main/java/com/krishna/Main.java b/src/main/java/com/krishna/Main.java new file mode 100644 index 0000000..d4d7362 --- /dev/null +++ b/src/main/java/com/krishna/Main.java @@ -0,0 +1,14 @@ +package com.krishna; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication + +public class Main { + + public static void main(String[] args) { + SpringApplication.run(Main.class, args); + } + +} diff --git a/src/main/java/com/krishna/customer/Customer.java b/src/main/java/com/krishna/customer/Customer.java new file mode 100644 index 0000000..247c571 --- /dev/null +++ b/src/main/java/com/krishna/customer/Customer.java @@ -0,0 +1,75 @@ +package com.krishna.customer; + +import java.util.Objects; + +public class Customer { + private Integer id; + private String name; + private String email; + private Integer age; + + public Customer() { + } + + public Customer(Integer id, String name, String email, Integer age) { + this.id = id; + this.name = name; + this.email = email; + this.age = age; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Customer customer = (Customer) o; + return Objects.equals(id, customer.id) && Objects.equals(name, customer.name) && Objects.equals(email, customer.email) && Objects.equals(age, customer.age); + } + + @Override + public int hashCode() { + return Objects.hash(id, name, email, age); + } + + @Override + public String toString() { + return "Customer{" + + "id=" + id + + ", name='" + name + '\'' + + ", email='" + email + '\'' + + ", age=" + age + + '}'; + } +} diff --git a/src/main/java/com/krishna/customer/CustomerController.java b/src/main/java/com/krishna/customer/CustomerController.java new file mode 100644 index 0000000..e3712f1 --- /dev/null +++ b/src/main/java/com/krishna/customer/CustomerController.java @@ -0,0 +1,36 @@ +package com.krishna.customer; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +public class CustomerController { + + private final CustomerService customerService; + + + public CustomerController(CustomerService customerService) { + this.customerService = customerService; + } + + /* + @RequestMapping(value = "api/v1/customers", + method = RequestMethod.GET + ) + This is the same as below : + */ + + @GetMapping("api/v1/customers") + public List getCustomers() { + return customerService.getAllCustomers(); + } + + @GetMapping("api/v1/customers/{customerId}") + public Customer getCustomer( + @PathVariable("customerId") Integer customerId) { + return customerService.getCustomer(customerId); + } +} diff --git a/src/main/java/com/krishna/customer/CustomerDao.java b/src/main/java/com/krishna/customer/CustomerDao.java new file mode 100644 index 0000000..a5e2e96 --- /dev/null +++ b/src/main/java/com/krishna/customer/CustomerDao.java @@ -0,0 +1,9 @@ +package com.krishna.customer; + +import java.util.List; +import java.util.Optional; + +public interface CustomerDao { + List selectAllCustomers(); + Optional selectCustomerById(Integer id); +} diff --git a/src/main/java/com/krishna/customer/CustomerDataAccessService.java b/src/main/java/com/krishna/customer/CustomerDataAccessService.java new file mode 100644 index 0000000..dd7485a --- /dev/null +++ b/src/main/java/com/krishna/customer/CustomerDataAccessService.java @@ -0,0 +1,45 @@ +package com.krishna.customer; + +import org.springframework.stereotype.Repository; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +@Repository +public class CustomerDataAccessService implements CustomerDao { + + //db + private static List customers; + + static { + customers = new ArrayList<>(); + Customer krishna = new Customer( + 1, + "Krishna", + "krishna@gmail.com", + 25 + ); + customers.add(krishna); + + Customer janaki = new Customer( + 2, + "Janaki", + "janaki@gmail.com", + 28 + ); + customers.add(janaki); + } + + @Override + public List selectAllCustomers() { + return customers; + } + + @Override + public Optional selectCustomerById(Integer id) { + return customers.stream() + .filter(c -> c.getId().equals(id)) + .findFirst(); + } +} diff --git a/src/main/java/com/krishna/customer/CustomerService.java b/src/main/java/com/krishna/customer/CustomerService.java new file mode 100644 index 0000000..a7728b0 --- /dev/null +++ b/src/main/java/com/krishna/customer/CustomerService.java @@ -0,0 +1,24 @@ +package com.krishna.customer; + +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class CustomerService { + + private final CustomerDao customerDao; + + public CustomerService(CustomerDao customerDao) { + this.customerDao = customerDao; + } + + public List getAllCustomers() { + return customerDao.selectAllCustomers(); + } + + public Customer getCustomer(Integer id) { + return customerDao.selectCustomerById(id) + .orElseThrow(() -> new IllegalArgumentException("Customer with [%s] not found".formatted(id))); + } +} diff --git a/src/main/java/com/krishna/firstspringbootapp/Main.java b/src/main/java/com/krishna/firstspringbootapp/Main.java deleted file mode 100644 index bac616e..0000000 --- a/src/main/java/com/krishna/firstspringbootapp/Main.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.krishna.firstspringbootapp; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; - -import java.util.List; - -@SpringBootApplication -@RestController -public class Main { - public static void main(String[] args) { - SpringApplication.run(Main.class, args); - } - - @GetMapping("/greet") - public GreetResponse greet( - @RequestParam(value = "name", required = false) String name - ) { - String greetMessage = name == null || name.isBlank() ? "Hello" : "Hello " + name; - GreetResponse response = new GreetResponse( - greetMessage, - List.of("Java", "Go", "Javascript"), - new Person("Parwati", 21, 24_200) - ); - return response; - } - - record Person(String name, int age, double savings) { - } - - record GreetResponse( - String greet, - List faveProgrammingLanguages, - Person person - ) { - } -} \ No newline at end of file