-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from KrishnaBishowkarma/working
refactor: Organize CRUD operations in N-tier architecture
- Loading branch information
Showing
7 changed files
with
203 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 + | ||
'}'; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/com/krishna/customer/CustomerController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Customer> getCustomers() { | ||
return customerService.getAllCustomers(); | ||
} | ||
|
||
@GetMapping("api/v1/customers/{customerId}") | ||
public Customer getCustomer( | ||
@PathVariable("customerId") Integer customerId) { | ||
return customerService.getCustomer(customerId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.krishna.customer; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface CustomerDao { | ||
List<Customer> selectAllCustomers(); | ||
Optional<Customer> selectCustomerById(Integer id); | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/com/krishna/customer/CustomerDataAccessService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Customer> customers; | ||
|
||
static { | ||
customers = new ArrayList<>(); | ||
Customer krishna = new Customer( | ||
1, | ||
"Krishna", | ||
"[email protected]", | ||
25 | ||
); | ||
customers.add(krishna); | ||
|
||
Customer janaki = new Customer( | ||
2, | ||
"Janaki", | ||
"[email protected]", | ||
28 | ||
); | ||
customers.add(janaki); | ||
} | ||
|
||
@Override | ||
public List<Customer> selectAllCustomers() { | ||
return customers; | ||
} | ||
|
||
@Override | ||
public Optional<Customer> selectCustomerById(Integer id) { | ||
return customers.stream() | ||
.filter(c -> c.getId().equals(id)) | ||
.findFirst(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Customer> getAllCustomers() { | ||
return customerDao.selectAllCustomers(); | ||
} | ||
|
||
public Customer getCustomer(Integer id) { | ||
return customerDao.selectCustomerById(id) | ||
.orElseThrow(() -> new IllegalArgumentException("Customer with [%s] not found".formatted(id))); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.