Skip to content

Commit

Permalink
Merge pull request #1 from KrishnaBishowkarma/working
Browse files Browse the repository at this point in the history
refactor: Organize CRUD operations in N-tier architecture
  • Loading branch information
KrishnaBishowkarma authored Nov 11, 2024
2 parents e878343 + 6927090 commit e581c2c
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 40 deletions.
14 changes: 14 additions & 0 deletions src/main/java/com/krishna/Main.java
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);
}

}
75 changes: 75 additions & 0 deletions src/main/java/com/krishna/customer/Customer.java
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 src/main/java/com/krishna/customer/CustomerController.java
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);
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/krishna/customer/CustomerDao.java
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 src/main/java/com/krishna/customer/CustomerDataAccessService.java
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();
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/krishna/customer/CustomerService.java
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)));
}
}
40 changes: 0 additions & 40 deletions src/main/java/com/krishna/firstspringbootapp/Main.java

This file was deleted.

0 comments on commit e581c2c

Please sign in to comment.