Skip to content

Commit 1f25139

Browse files
committed
implement notification module & connect that to customer with OpenFeign
1 parent 3953c0e commit 1f25139

File tree

12 files changed

+209
-9
lines changed

12 files changed

+209
-9
lines changed

.idea/encodings.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.hrk.clients.notification;
2+
3+
import com.hrk.clients.fraud.FraudCheckResponse;
4+
import org.springframework.cloud.openfeign.FeignClient;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.PostMapping;
7+
import org.springframework.web.bind.annotation.RequestBody;
8+
9+
@FeignClient(
10+
value = "notification",
11+
path = "api/v1/notification"
12+
)
13+
public interface NotificationClient {
14+
15+
@PostMapping
16+
void sendNotification(@RequestBody NotificationRequest notificationRequest);
17+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.hrk.clients.notification;
2+
3+
public record NotificationRequest(
4+
Integer toCustomerId,
5+
String toCustomerEmail,
6+
String message
7+
) {
8+
}

customer/src/main/java/com/hrk/customer/CustomerService.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
import com.hrk.clients.fraud.FraudCheckResponse;
44
import com.hrk.clients.fraud.FraudClient;
5+
import com.hrk.clients.notification.NotificationClient;
6+
import com.hrk.clients.notification.NotificationRequest;
57
import org.springframework.stereotype.Service;
68
import org.springframework.web.client.RestTemplate;
79

810
@Service
9-
public record CustomerService(RestTemplate restTemplate, CustomerRepository repository, FraudClient fraudClient) {
11+
public record CustomerService(CustomerRepository repository, FraudClient fraudClient, NotificationClient notificationClient) {
1012
public void registerCustomer(CustomerRegistrationRequest request) {
1113
Customer customer = Customer.builder()
1214
.firstName(request.firstName())
@@ -16,18 +18,18 @@ public void registerCustomer(CustomerRegistrationRequest request) {
1618
// todo: check if email valid
1719
// todo: check if email not taken
1820
repository.saveAndFlush(customer);
19-
// FraudCheckResponse fraudCheckResponse = restTemplate.getForObject(
20-
// "http://FRAUD/api/v1/fraud-check/{customerId}",
21-
// FraudCheckResponse.class,
22-
// customer.getId()
23-
// );
2421

25-
FraudCheckResponse fraudCheckResponse =
26-
fraudClient.isFraudster(customer.getId());
22+
FraudCheckResponse fraudCheckResponse = fraudClient.isFraudster(customer.getId());
2723

2824
if (fraudCheckResponse.isFraudster())
2925
throw new IllegalStateException("fraudster");
3026

31-
// todo: send notification
27+
notificationClient.sendNotification(
28+
new NotificationRequest(
29+
customer.getId(),
30+
customer.getEmail(),
31+
String.format("Hi %s welcome to HRK Academy...", customer.getFirstName())
32+
)
33+
);
3234
}
3335
}

notification/pom.xml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.hrk</groupId>
8+
<artifactId>microservices-tutorial</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>notification</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>20</maven.compiler.source>
16+
<maven.compiler.target>20</maven.compiler.target>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-web</artifactId>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-data-jpa</artifactId>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.postgresql</groupId>
33+
<artifactId>postgresql</artifactId>
34+
<scope>runtime</scope>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>org.springframework.cloud</groupId>
39+
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>com.hrk</groupId>
44+
<artifactId>clients</artifactId>
45+
<version>1.0-SNAPSHOT</version>
46+
<scope>compile</scope>
47+
</dependency>
48+
</dependencies>
49+
50+
</project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.hrk.notification;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
import javax.persistence.*;
9+
import java.time.LocalDateTime;
10+
11+
@Data
12+
@Builder
13+
@AllArgsConstructor
14+
@NoArgsConstructor
15+
@Entity
16+
public class Notification {
17+
18+
@Id
19+
@SequenceGenerator(
20+
name = "notification_id_sequence",
21+
sequenceName = "notification_id_sequence"
22+
)
23+
@GeneratedValue(
24+
strategy = GenerationType.SEQUENCE,
25+
generator = "notification_id_sequence"
26+
)
27+
private Integer id;
28+
private Integer toCustomerId;
29+
private String toCustomerEmail;
30+
private String sender;
31+
private String message;
32+
private LocalDateTime sentAt;
33+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.hrk.notification;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
6+
7+
@SpringBootApplication
8+
@EnableEurekaClient
9+
public class NotificationApplication {
10+
public static void main(String[] args) {
11+
SpringApplication.run(NotificationApplication.class, args);
12+
}
13+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.hrk.notification;
2+
3+
import com.hrk.clients.notification.NotificationRequest;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.springframework.web.bind.annotation.PostMapping;
6+
import org.springframework.web.bind.annotation.RequestBody;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
@Slf4j
11+
@RestController
12+
@RequestMapping("api/v1/notification")
13+
public record NotificationController(NotificationService service) {
14+
15+
@PostMapping
16+
public void sendNotification(@RequestBody NotificationRequest notificationRequest){
17+
log.info("new notification sent {}", notificationRequest);
18+
service.send(notificationRequest);
19+
}
20+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.hrk.notification;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
5+
public interface NotificationRepository extends JpaRepository<Notification, Integer> {
6+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.hrk.notification;
2+
3+
import com.hrk.clients.notification.NotificationRequest;
4+
import org.springframework.stereotype.Service;
5+
6+
import java.time.LocalDateTime;
7+
8+
@Service
9+
public record NotificationService(NotificationRepository repository) {
10+
11+
public void send(NotificationRequest notificationRequest) {
12+
repository.save(
13+
Notification.builder()
14+
.toCustomerId(notificationRequest.toCustomerId())
15+
.toCustomerEmail(notificationRequest.toCustomerEmail())
16+
.message(notificationRequest.message())
17+
.sender("HRK")
18+
.sentAt(LocalDateTime.now())
19+
.build()
20+
);
21+
}
22+
}

0 commit comments

Comments
 (0)