Skip to content

Commit 8b2b4f2

Browse files
committed
implmeneting crud using JPA
1 parent d435d22 commit 8b2b4f2

15 files changed

+342
-19
lines changed

docker-compose.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
services:
2+
db:
3+
container_name: postgres-spring
4+
image: postgres
5+
environment:
6+
POSTGRES_USER: harimahat
7+
POSTGRES_PASSWORD: password
8+
PGDATA: /data/postgres-spring
9+
volumes:
10+
- db:/data/postgres-spring
11+
ports:
12+
- "5432:5432"
13+
networks:
14+
- db
15+
restart: unless-stopped
16+
17+
networks:
18+
db:
19+
driver: bridge
20+
volumes:
21+
db:
22+

pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@
2727
<artifactId>spring-boot-starter-test</artifactId>
2828
<scope>test</scope>
2929
</dependency>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-data-jpa</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.postgresql</groupId>
36+
<artifactId>postgresql</artifactId>
37+
<scope>runtime</scope>
38+
</dependency>
3039
</dependencies>
3140

3241
<build>

src/main/java/com/harimahat/Main.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.harimahat;
22

33
import com.harimahat.customer.Customer;
4+
import com.harimahat.customer.CustomerRepository;
5+
import org.springframework.boot.CommandLineRunner;
46
import org.springframework.boot.SpringApplication;
57
import org.springframework.boot.autoconfigure.SpringBootApplication;
8+
import org.springframework.context.annotation.Bean;
69
import org.springframework.web.bind.annotation.*;
710

811
import java.util.ArrayList;
@@ -20,6 +23,17 @@ public static void main(String[] args) {
2023
SpringApplication.run(Main.class, args);
2124

2225
}
26+
@Bean
27+
CommandLineRunner runner( CustomerRepository customerRepository){
28+
return args -> {
29+
Customer alex= new Customer( "Alex", "[email protected]", 29);
30+
Customer john= new Customer( "John", "[email protected]", 49);
31+
Customer alexa= new Customer( "Alexa", "[email protected]", 19);
32+
33+
List<Customer>customers= List.of(alex, john, alexa);
34+
customerRepository.saveAll(customers);
35+
};
36+
}
2337

2438
}
2539

src/main/java/com/harimahat/customer/Customer.java

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,56 @@
11
package com.harimahat.customer;
22

3+
import jakarta.persistence.*;
4+
35
/**
46
* @Author hari.mahat on 27.7.2023
57
* Project learn-spring3
68
*/
7-
9+
@Entity
810
public class Customer {
11+
@Id
12+
@SequenceGenerator(
13+
name = "customer_id_sequence",
14+
sequenceName = "customer_id_sequence"
15+
)
16+
@GeneratedValue(
17+
strategy = GenerationType.SEQUENCE,
18+
generator = "customer_id_sequence"
19+
)
920
private Integer id;
21+
@Column(
22+
nullable = false
23+
)
1024
private String name;
25+
@Column(
26+
nullable = false
27+
)
1128
private String email;
29+
@Column(
30+
nullable = false
31+
)
1232
private Integer age;
1333

14-
public Customer(Integer id, String name, String email, Integer age) {
15-
this.id = id;
34+
public Customer() {
35+
36+
}
37+
38+
public Customer( Integer id, String name, String email, Integer age) {
39+
this.id=id;
1640
this.name = name;
1741
this.email = email;
1842
this.age = age;
1943
}
2044

21-
public Integer getId() {
22-
return id;
45+
public Customer(String name, String email, Integer age) {
46+
this.name = name;
47+
this.email = email;
48+
this.age = age;
2349
}
2450

25-
public void setId(Integer id) {
26-
this.id = id;
51+
public Integer getId() {
52+
return id;
2753
}
28-
2954
public String getName() {
3055
return name;
3156
}
Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.harimahat.customer;
22

3-
import org.springframework.web.bind.annotation.PathVariable;
4-
import org.springframework.web.bind.annotation.RequestMapping;
5-
import org.springframework.web.bind.annotation.RestController;
3+
import org.springframework.web.bind.annotation.*;
64

75
import java.util.List;
86

@@ -13,6 +11,7 @@
1311
* Project learn-spring3
1412
*/
1513
@RestController
14+
@RequestMapping("api/v1/customers")
1615
public class CustomerController {
1716

1817
private final CustomerService customerService;
@@ -21,13 +20,29 @@ public CustomerController(CustomerService customerService) {
2120
this.customerService = customerService;
2221
}
2322

24-
@RequestMapping(path = "api/v1/customers",method = GET)
23+
@GetMapping
2524
public List<Customer> getCustomers(){
2625
return customerService.getAllCustomers();
2726
}
2827

29-
@RequestMapping(path = "api/v1/customers/{customerId}", method = GET)
28+
@PostMapping
29+
public void registerCustomer(@RequestBody CustomerRegistrationRequest customerRegistrationRequest){
30+
customerService.addCustomer(customerRegistrationRequest);
31+
}
32+
33+
@GetMapping("/{customerId}")
3034
public Customer getCustomer(@PathVariable("customerId") Integer customerId){
3135
return customerService.getCustomerById(customerId);
3236
}
37+
38+
@DeleteMapping("/{customerId}")
39+
public void deleteCustomer(@PathVariable("customerId") Integer customerId){
40+
customerService.deleteCustomerById(customerId);
41+
}
42+
43+
@PutMapping("/{customerId}")
44+
public void updateCustomer(@PathVariable("customerId") Integer customerId, @RequestBody CustomerUpdateRequest customerUpdateRequest){
45+
customerService.updateCustomerById(customerId, customerUpdateRequest);
46+
}
47+
3348
}

src/main/java/com/harimahat/customer/CustomerDao.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.harimahat.customer;
22

3+
import jakarta.persistence.criteria.CriteriaBuilder;
4+
35
import java.util.List;
46
import java.util.Optional;
57

@@ -10,7 +12,20 @@
1012

1113
public interface CustomerDao {
1214

13-
public List<Customer> selectAllCustomers();
15+
List<Customer> selectAllCustomers();
1416

1517
Optional <Customer> selectCustomerById(Integer customerId);
18+
19+
void insertCustomer( Customer customer);
20+
21+
void deleteCustomerById(Integer customerId);
22+
23+
void updateCustomer(Customer update);
24+
25+
boolean existPersonWithEmail(String emailcustomer);
26+
27+
boolean existPersonWithId(Integer customerId);
28+
29+
30+
1631
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.harimahat.customer;
2+
3+
import org.springframework.stereotype.Repository;
4+
5+
import java.util.List;
6+
import java.util.Optional;
7+
8+
/**
9+
* @Author hari.mahat on 28.7.2023
10+
* Project learn-spring3
11+
*/
12+
@Repository("jpa")
13+
public class CustomerJPADataAccessService implements CustomerDao{
14+
15+
private final CustomerRepository customerRepository;
16+
17+
public CustomerJPADataAccessService(CustomerRepository customerRepository) {
18+
this.customerRepository = customerRepository;
19+
}
20+
21+
@Override
22+
public List<Customer> selectAllCustomers() {
23+
return customerRepository.findAll();
24+
}
25+
26+
@Override
27+
public Optional<Customer> selectCustomerById(Integer customerId) {
28+
return customerRepository.findById(customerId);
29+
}
30+
31+
@Override
32+
public void insertCustomer(Customer customer) {
33+
customerRepository.save(customer);
34+
}
35+
36+
@Override
37+
public void deleteCustomerById(Integer customerId) {
38+
customerRepository.deleteById(customerId);
39+
}
40+
41+
@Override
42+
public void updateCustomer(Customer update) {
43+
customerRepository.save(update);
44+
}
45+
46+
@Override
47+
public boolean existPersonWithEmail(String email) {
48+
return customerRepository.existsCustomerByEmail(email);
49+
}
50+
51+
@Override
52+
public boolean existPersonWithId(Integer customerId) {
53+
return customerRepository.existsCustomerById(customerId);
54+
}
55+
}

src/main/java/com/harimahat/customer/CustomerDataAccessService.java renamed to src/main/java/com/harimahat/customer/CustomerListDataAccessService.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
* Project learn-spring3
1212
*/
1313

14-
@Repository
15-
public class CustomerDataAccessService implements CustomerDao{
14+
@Repository("list")
15+
public class CustomerListDataAccessService implements CustomerDao{
1616

1717

1818

1919
private final List<Customer> customers ;
2020

21-
public CustomerDataAccessService() {
21+
public CustomerListDataAccessService() {
2222
this.customers= new ArrayList<>();
2323
Customer alex= new Customer(1, "Alex", "[email protected]", 29);
2424
Customer john= new Customer(2, "John", "[email protected]", 49);
@@ -39,4 +39,34 @@ public Optional<Customer> selectCustomerById(Integer customerId) {
3939
.filter(c -> c.getId().equals(customerId))
4040
.findFirst();
4141
}
42+
43+
@Override
44+
public void insertCustomer(Customer customer) {
45+
customers.add(customer);
46+
}
47+
48+
@Override
49+
public void deleteCustomerById(Integer customerId) {
50+
customers
51+
.stream()
52+
.filter(c->c.getId().equals(customerId))
53+
.findFirst()
54+
.ifPresent(customers::remove);
55+
}
56+
57+
@Override
58+
public void updateCustomer(Customer update) {
59+
customers.add(update);
60+
}
61+
62+
@Override
63+
public boolean existPersonWithEmail(String email) {
64+
return customers.stream()
65+
.anyMatch(customer -> customer.getEmail().equals(email));
66+
}
67+
68+
@Override
69+
public boolean existPersonWithId(Integer customerId) {
70+
return customers.stream().anyMatch(customer -> customer.getId().equals(customerId));
71+
}
4272
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.harimahat.customer;
2+
3+
/**
4+
* @Author hari.mahat on 28.7.2023
5+
* Project learn-spring3
6+
*/
7+
public record CustomerRegistrationRequest (
8+
String name,
9+
String email,
10+
Integer age
11+
){};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.harimahat.customer;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
5+
/**
6+
* @Author hari.mahat on 28.7.2023
7+
* Project learn-spring3
8+
*/
9+
public interface CustomerRepository extends JpaRepository<Customer, Integer> {
10+
11+
boolean existsCustomerByEmail(String email);
12+
boolean existsCustomerById(Integer id);
13+
}

0 commit comments

Comments
 (0)