From cdb3c4ae5d7917603d7c789e22e20ea9847fb592 Mon Sep 17 00:00:00 2001 From: Krishna Bishowkarma Date: Mon, 11 Nov 2024 20:16:50 +0545 Subject: [PATCH 1/6] feat: modeling costumer --- src/main/java/com/krishna/Main.java | 87 +++++++++++++++++++ .../com/krishna/firstspringbootapp/Main.java | 40 --------- 2 files changed, 87 insertions(+), 40 deletions(-) create mode 100644 src/main/java/com/krishna/Main.java delete mode 100644 src/main/java/com/krishna/firstspringbootapp/Main.java diff --git a/src/main/java/com/krishna/Main.java b/src/main/java/com/krishna/Main.java new file mode 100644 index 0000000..bde895e --- /dev/null +++ b/src/main/java/com/krishna/Main.java @@ -0,0 +1,87 @@ +package com.krishna; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Objects; + +@SpringBootApplication +@RestController +public class Main { + public static void main(String[] args) { + SpringApplication.run(Main.class, args); + } + + 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/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 From aa318c5c1fcecf06b33581a3e4d80ea671a69853 Mon Sep 17 00:00:00 2001 From: Krishna Bishowkarma Date: Mon, 11 Nov 2024 20:30:49 +0545 Subject: [PATCH 2/6] feat: create in-memory customer database (List) and print contents to console --- src/main/java/com/krishna/Main.java | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/krishna/Main.java b/src/main/java/com/krishna/Main.java index bde895e..2ac65a4 100644 --- a/src/main/java/com/krishna/Main.java +++ b/src/main/java/com/krishna/Main.java @@ -4,16 +4,41 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RestController; +import java.util.ArrayList; +import java.util.List; import java.util.Objects; @SpringBootApplication @RestController public class Main { + + 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); + } + public static void main(String[] args) { + System.out.println(customers); SpringApplication.run(Main.class, args); } - class Customer { + static class Customer { private Integer id; private String name; private String email; From dc2b900af80e36600c82a2aeed584a1e48af9d71 Mon Sep 17 00:00:00 2001 From: Krishna Bishowkarma Date: Mon, 11 Nov 2024 20:43:07 +0545 Subject: [PATCH 3/6] feat: add API endpoint using spring-boot --- src/main/java/com/krishna/Main.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/krishna/Main.java b/src/main/java/com/krishna/Main.java index 2ac65a4..7006474 100644 --- a/src/main/java/com/krishna/Main.java +++ b/src/main/java/com/krishna/Main.java @@ -2,6 +2,9 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; @@ -34,10 +37,21 @@ public class Main { } public static void main(String[] args) { - System.out.println(customers); SpringApplication.run(Main.class, args); } + /* + @RequestMapping(value = "api/v1/customers", + method = RequestMethod.GET + ) + This is the same as below : + */ + + @GetMapping("api/v1/customers") + public List getCustomers() { + return customers; + } + static class Customer { private Integer id; private String name; From a461bfefd3ee929e4b7b91672e7da203bfca267c Mon Sep 17 00:00:00 2001 From: Krishna Bishowkarma Date: Mon, 11 Nov 2024 22:01:53 +0545 Subject: [PATCH 4/6] feat: add @PathVariable to be able to get customer by given ID --- src/main/java/com/krishna/Main.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/krishna/Main.java b/src/main/java/com/krishna/Main.java index 7006474..90e2050 100644 --- a/src/main/java/com/krishna/Main.java +++ b/src/main/java/com/krishna/Main.java @@ -3,8 +3,7 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; @@ -40,6 +39,16 @@ public static void main(String[] args) { SpringApplication.run(Main.class, args); } + @GetMapping("api/v1/customers/{customerId}") + public Customer getCustomerById( + @PathVariable("customerId") Integer customerId) { + Customer customer = customers.stream() + .filter(c -> c.getId().equals(customerId)) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException("Customer with [%s] not found".formatted(customerId))); + return customer; + } + /* @RequestMapping(value = "api/v1/customers", method = RequestMethod.GET From e56c6caea121740daefdb842487e2f12ecc45e9c Mon Sep 17 00:00:00 2001 From: Krishna Bishowkarma Date: Mon, 11 Nov 2024 22:51:03 +0545 Subject: [PATCH 5/6] refactor: change project structure to follow N-tier architecture --- src/main/java/com/krishna/Main.java | 123 +----------------- .../java/com/krishna/customer/Customer.java | 75 +++++++++++ .../krishna/customer/CustomerController.java | 36 +++++ .../com/krishna/customer/CustomerDao.java | 9 ++ .../customer/CustomerDataAccessService.java | 42 ++++++ .../com/krishna/customer/CustomerService.java | 21 +++ 6 files changed, 184 insertions(+), 122 deletions(-) create mode 100644 src/main/java/com/krishna/customer/Customer.java create mode 100644 src/main/java/com/krishna/customer/CustomerController.java create mode 100644 src/main/java/com/krishna/customer/CustomerDao.java create mode 100644 src/main/java/com/krishna/customer/CustomerDataAccessService.java create mode 100644 src/main/java/com/krishna/customer/CustomerService.java diff --git a/src/main/java/com/krishna/Main.java b/src/main/java/com/krishna/Main.java index 90e2050..d4d7362 100644 --- a/src/main/java/com/krishna/Main.java +++ b/src/main/java/com/krishna/Main.java @@ -2,134 +2,13 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RestController; - -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; @SpringBootApplication -@RestController -public class Main { - - 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); - } +public class Main { public static void main(String[] args) { SpringApplication.run(Main.class, args); } - @GetMapping("api/v1/customers/{customerId}") - public Customer getCustomerById( - @PathVariable("customerId") Integer customerId) { - Customer customer = customers.stream() - .filter(c -> c.getId().equals(customerId)) - .findFirst() - .orElseThrow(() -> new IllegalArgumentException("Customer with [%s] not found".formatted(customerId))); - return customer; - } - - /* - @RequestMapping(value = "api/v1/customers", - method = RequestMethod.GET - ) - This is the same as below : - */ - - @GetMapping("api/v1/customers") - public List getCustomers() { - return customers; - } - - static 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/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..287af32 --- /dev/null +++ b/src/main/java/com/krishna/customer/CustomerDataAccessService.java @@ -0,0 +1,42 @@ +package com.krishna.customer; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +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..dfe7dc6 --- /dev/null +++ b/src/main/java/com/krishna/customer/CustomerService.java @@ -0,0 +1,21 @@ +package com.krishna.customer; + +import java.util.List; + +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))); + } +} From 6927090aa65d96ee37d4da89826ef4ce407dab66 Mon Sep 17 00:00:00 2001 From: Krishna Bishowkarma Date: Mon, 11 Nov 2024 23:01:51 +0545 Subject: [PATCH 6/6] fix: add @RestController, @Service, and @Repository annotations to enable application functionality --- .../java/com/krishna/customer/CustomerDataAccessService.java | 3 +++ src/main/java/com/krishna/customer/CustomerService.java | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/main/java/com/krishna/customer/CustomerDataAccessService.java b/src/main/java/com/krishna/customer/CustomerDataAccessService.java index 287af32..dd7485a 100644 --- a/src/main/java/com/krishna/customer/CustomerDataAccessService.java +++ b/src/main/java/com/krishna/customer/CustomerDataAccessService.java @@ -1,9 +1,12 @@ 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 diff --git a/src/main/java/com/krishna/customer/CustomerService.java b/src/main/java/com/krishna/customer/CustomerService.java index dfe7dc6..a7728b0 100644 --- a/src/main/java/com/krishna/customer/CustomerService.java +++ b/src/main/java/com/krishna/customer/CustomerService.java @@ -1,7 +1,10 @@ package com.krishna.customer; +import org.springframework.stereotype.Service; + import java.util.List; +@Service public class CustomerService { private final CustomerDao customerDao;