Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions jmp-cloud-bank-impl/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module application {
uses org.x.service.BankService;
requires jmp.cloud.bank.impl;
requires jmp.dto;
}
28 changes: 28 additions & 0 deletions jmp-cloud-bank-impl/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.x</groupId>
<artifactId>java-backend</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>jmp-cloud-bank-impl</artifactId>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
26 changes: 26 additions & 0 deletions jmp-cloud-bank-impl/src/main/java/org/x/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.x;

import org.x.dto.BankCard;
import org.x.dto.BankCardType;
import org.x.dto.User;
import org.x.service.BankService;
import org.x.service.BankServiceImpl;

public class Main {
public static void main(String[] args) {
BankService bankService = new BankServiceImpl();
User user = new User();
user.setName("John");
user.setSurname("Doe");

BankCard creditCard = bankService.createCard(user, BankCardType.CREDIT);
BankCard debitCard = bankService.createCard(user, BankCardType.DEBIT);

System.out.println("Credit Card: " + creditCard);
System.out.println("Debit Card: " + debitCard);

System.out.println("All subscriptions: " + bankService.getAllSubscriptions());
System.out.println("Subscriptions by condition: " + bankService
.getAllSubscriptionsByCondition(subscription -> subscription.getPrice() > 5));
}
}
17 changes: 17 additions & 0 deletions jmp-cloud-bank-impl/src/main/java/org/x/dto/BankCard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.x.dto;

import lombok.Data;

@Data
public class BankCard {
private User user;
private BankCardType bankCardType;
private String cardNumber;
private String cvv;
private String expirationDate;

public BankCard(User user, BankCardType bankCardType) {
this.user = user;
this.bankCardType = bankCardType;
}
}
6 changes: 6 additions & 0 deletions jmp-cloud-bank-impl/src/main/java/org/x/dto/BankCardType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.x.dto;

public enum BankCardType {
CREDIT,
DEBIT
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.x.dto;

public class CreditBankCard extends BankCard {
public CreditBankCard(User user) {
super(user, BankCardType.CREDIT);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.x.dto;

public class DebitBankCard extends BankCard {
public DebitBankCard(User user) {
super(user, BankCardType.DEBIT);
}

}
18 changes: 18 additions & 0 deletions jmp-cloud-bank-impl/src/main/java/org/x/dto/Subscription.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.x.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.With;

@Data
@With
@AllArgsConstructor
@NoArgsConstructor
public class Subscription {
private Long id;
private String name;
private String description;
private Double price;
private Boolean active;
}
18 changes: 18 additions & 0 deletions jmp-cloud-bank-impl/src/main/java/org/x/dto/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.x.dto;

import java.time.LocalDate;

import lombok.Data;

@Data
public class User {
private String name;
private String surname;
private String email;
private String password;
private String phone;
private String address;
private String passport;
private String role;
private LocalDate birthDate;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.x.exception;

public class SubscriptionNotFoundException extends RuntimeException {
public SubscriptionNotFoundException(String message) {
super(message);
}
}
23 changes: 23 additions & 0 deletions jmp-cloud-bank-impl/src/main/java/org/x/service/BankService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.x.service;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.function.Predicate;

import org.x.dto.BankCard;
import org.x.dto.BankCardType;
import org.x.dto.Subscription;
import org.x.dto.User;

public interface BankService {
BankCard createCard(User user, BankCardType bankCardType);

List<Subscription> getAllSubscriptions();

List<Subscription> getAllSubscriptionsByCondition(Predicate<Subscription> condition);

static boolean isPayableUser(User user) {
return ChronoUnit.YEARS.between(user.getBirthDate(), LocalDate.now()) >= 18;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.x.service;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;

import org.x.dto.BankCard;
import org.x.dto.BankCardType;
import org.x.dto.CreditBankCard;
import org.x.dto.DebitBankCard;
import org.x.dto.Subscription;
import org.x.dto.User;

public class BankServiceImpl implements BankService {

private static final Map<BankCardType, Function<User, BankCard>> cardCreators = Map.of(
BankCardType.CREDIT, CreditBankCard::new,
BankCardType.DEBIT, DebitBankCard::new
);

@Override
public BankCard createCard(User user, BankCardType bankCardType) {
return cardCreators.get(bankCardType).apply(user);
}

@Override
public List<Subscription> getAllSubscriptions() {
return List.of(
new Subscription().withId(1L).withPrice(4.99),
new Subscription().withId(3L).withPrice(9.99),
new Subscription().withId(2L).withPrice(7.99),
new Subscription().withId(4L).withPrice(5.99));
}

@Override
public List<Subscription> getAllSubscriptionsByCondition(Predicate<Subscription> condition) {
return getAllSubscriptions()
.stream().filter(condition)
.toList();
}
}
18 changes: 18 additions & 0 deletions jmp-cloud-bank-impl/src/main/java/org/x/service/UserService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.x.service;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;

import org.x.dto.User;

public interface UserService {
List<User> getAllUsers();

default double getAverageUsersAge() {
return getAllUsers().stream()
.mapToDouble(user -> ChronoUnit.YEARS.between(user.getBirthDate(), LocalDate.now()))
.average()
.orElse(0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.x.service;

import java.util.List;

import org.x.dto.User;

public class UserServiceImpl implements UserService {
@Override
public List<User> getAllUsers() {
return List.of();
}
}
17 changes: 17 additions & 0 deletions modules/54. Redis/Code Examples/bin/main/ratelimitRules.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 2 requests per minute per account Id
- accountId: ""
allowedNumberOfRequests: 2
timeInterval: MINUTE
# 1 Slow request per account per minute
- accountId: ""
allowedNumberOfRequests: 1
timeInterval: MINUTE
requestType: SLOW
# 1 request for 192.168.100.150 per HOUR
- clientIp: 192.168.100.150
allowedNumberOfRequests: 1
timeInterval: HOUR
# 10 requests for important customer per minute
- accountId: ImportantCustomerId
allowedNumberOfRequests: 10
timeInterval: MINUTE
37 changes: 37 additions & 0 deletions modules/54. Redis/Code Examples/bin/test/redis/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# On windows host network mode doesn't work (expect wsl 2 backend)
# We use --cluster-announce-ip + different ports for each instance + DNS-based nodes access to make cluster accessible from localhost
version: "3.9"

services:
redis-master-0:
image: redis:7.0.5-alpine
command: redis-server --cluster-enabled yes --port 30000 --cluster-port 40000 --cluster-announce-ip redis-master-0
ports:
- "30000:30000"
- "40000:40000"

redis-master-1:
image: redis:7.0.5-alpine
command: redis-server --cluster-enabled yes --port 30001 --cluster-port 40001 --cluster-announce-ip redis-master-1
ports:
- "30001:30001"
- "40001:40001"


redis-master-2:
image: redis:7.0.5-alpine
command: redis-server --cluster-enabled yes --port 30002 --cluster-port 40002 --cluster-announce-ip redis-master-2
ports:
- "30002:30002"
- "40002:40002"


redis-init-cluster:
image: redis:7.0.5-alpine
command: redis-cli --cluster create redis-master-0:30000 redis-master-1:30001 redis-master-2:30002 --cluster-replicas 0 --cluster-yes
restart: "no"
depends_on:
- redis-master-0
- redis-master-1
- redis-master-2

4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
<groupId>org.x</groupId>
<artifactId>java-backend</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>jmp-cloud-bank-impl</module>
</modules>

<properties>
<maven.compiler.source>17</maven.compiler.source>
Expand Down