Skip to content

Commit 5f2f29a

Browse files
committed
Initial commit for MoneyTransferAPI
0 parents  commit 5f2f29a

File tree

18 files changed

+1009
-0
lines changed

18 files changed

+1009
-0
lines changed

.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# ignore an entire directory
2+
/target/
3+
4+
#ignore classpath
5+
.classpath
6+
7+
#ignore IDE settings
8+
.idea/
9+
10+
#ignore project files
11+
.project
12+
13+
#ignore settings file
14+
.settings

pom.xml

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>org.revolut</groupId>
7+
<artifactId>moneytransfer</artifactId>
8+
<packaging>jar</packaging>
9+
<version>1.0</version>
10+
<name>moneytransfer</name>
11+
12+
<properties>
13+
<maven.compiler.source>1.8</maven.compiler.source>
14+
<maven.compiler.target>1.8</maven.compiler.target>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
</properties>
17+
18+
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
23+
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
24+
<version>2.7</version>
25+
<scope>test</scope>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>junit</groupId>
30+
<artifactId>junit</artifactId>
31+
<version>4.12</version>
32+
<scope>test</scope>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>org.hamcrest</groupId>
37+
<artifactId>hamcrest-all</artifactId>
38+
<version>1.3</version>
39+
<scope>test</scope>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>org.eclipse.jetty</groupId>
44+
<artifactId>jetty-server</artifactId>
45+
<version>9.2.3.v20140905</version>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.eclipse.jetty</groupId>
49+
<artifactId>jetty-servlet</artifactId>
50+
<version>9.2.3.v20140905</version>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.glassfish.jersey.core</groupId>
54+
<artifactId>jersey-server</artifactId>
55+
<version>2.7</version>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.glassfish.jersey.containers</groupId>
59+
<artifactId>jersey-container-servlet-core</artifactId>
60+
<version>2.7</version>
61+
</dependency>
62+
<dependency>
63+
<groupId>org.glassfish.jersey.containers</groupId>
64+
<artifactId>jersey-container-jetty-http</artifactId>
65+
<version>2.7</version>
66+
</dependency>
67+
<dependency>
68+
<groupId>org.glassfish.jersey.media</groupId>
69+
<artifactId>jersey-media-moxy</artifactId>
70+
<version>2.7</version>
71+
</dependency>
72+
73+
<dependency>
74+
<groupId>org.glassfish.jersey.ext</groupId>
75+
<artifactId>jersey-bean-validation</artifactId>
76+
<version>2.7</version>
77+
</dependency>
78+
</dependencies>
79+
80+
<build>
81+
<plugins>
82+
<plugin>
83+
<groupId>org.apache.maven.plugins</groupId>
84+
<artifactId>maven-shade-plugin</artifactId>
85+
<version>1.6</version>
86+
<configuration>
87+
<createDependencyReducedPom>true</createDependencyReducedPom>
88+
<filters>
89+
<filter>
90+
<artifact>*:*</artifact>
91+
<excludes>
92+
<exclude>META-INF/*.SF</exclude>
93+
<exclude>META-INF/*.DSA</exclude>
94+
<exclude>META-INF/*.RSA</exclude>
95+
</excludes>
96+
</filter>
97+
</filters>
98+
</configuration>
99+
100+
<executions>
101+
<execution>
102+
<phase>package</phase>
103+
<goals>
104+
<goal>shade</goal>
105+
</goals>
106+
<configuration>
107+
<transformers>
108+
<transformer
109+
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
110+
<transformer
111+
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
112+
<manifestEntries>
113+
<Main-Class>org.revolut.moneytransfer.Main</Main-Class>
114+
</manifestEntries>
115+
</transformer>
116+
</transformers>
117+
</configuration>
118+
</execution>
119+
</executions>
120+
</plugin>
121+
</plugins>
122+
</build>
123+
</project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.revolut.moneytransfer;
2+
3+
import org.revolut.moneytransfer.exception.CustomExceptionMapper;
4+
import org.revolut.moneytransfer.resource.TransactionResource;
5+
import org.revolut.moneytransfer.resource.UserAccountResource;
6+
import org.eclipse.jetty.server.Server;
7+
import org.eclipse.jetty.servlet.ServletContextHandler;
8+
import org.eclipse.jetty.servlet.ServletHolder;
9+
10+
public class Main {
11+
12+
public static void main(String[] args) throws Exception {
13+
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
14+
context.setContextPath("/");
15+
16+
Server jettyServer = new Server(8080);
17+
jettyServer.setHandler(context);
18+
19+
ServletHolder jerseyServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
20+
jerseyServlet.setInitOrder(0);
21+
22+
jerseyServlet.setInitParameter("jersey.config.server.provider.classnames",
23+
UserAccountResource.class.getCanonicalName() + "," + CustomExceptionMapper.class.getCanonicalName()
24+
+ "," + TransactionResource.class.getCanonicalName());
25+
26+
try {
27+
jettyServer.start();
28+
jettyServer.join();
29+
} finally {
30+
jettyServer.destroy();
31+
}
32+
}
33+
34+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.revolut.moneytransfer.constants;
2+
3+
public class Constants {
4+
public static final String USERS_DOES_NOT_EXISTS = "Users does not exists";
5+
public static final String AMOUNT_TRANSFERED_SUCCESSFULLY = "Amount transfered successfully";
6+
public static final String AMOUNT_WITHDRAWN_SUCCESSFULLY = "Amount withdrawn successfully";
7+
public static final String USER_DOES_NOT_HAVE_SUFFICIENT_BALANCE = "User does not have sufficient balance";
8+
public static final String AMOUNT_DEPOSITED_SUCCESSFULLY = "Amount deposited successfully";
9+
public static final String USER_DOES_NOT_EXISTS = "User does not exists";
10+
public static final String USER_ACCOUNT_DELETED_SUCCESSFULLY = "User Account deleted successfully";
11+
public static final String USER_ACCOUNT_UPDATED_SUCCESSFULLY = "User Account updated successfully";
12+
public static final String USER_CREATED_SUCCESFULLY = "User created succesfully";
13+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.revolut.moneytransfer.dao;
2+
3+
import java.util.concurrent.ConcurrentHashMap;
4+
5+
import org.revolut.moneytransfer.model.UserAcount;
6+
7+
8+
public class InMemoryDataStore {
9+
10+
public static ConcurrentHashMap<Long, UserAcount> map= new ConcurrentHashMap<>();
11+
12+
static
13+
{
14+
map.put(1L, new UserAcount(1, "TEST1", "100"));
15+
map.put(2L,new UserAcount(2, "TEST2", "100"));
16+
}
17+
18+
public static ConcurrentHashMap<Long, UserAcount> getMap() {
19+
return map;
20+
}
21+
22+
public static void setMap(ConcurrentHashMap<Long, UserAcount> map) {
23+
InMemoryDataStore.map = map;
24+
}
25+
26+
27+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.revolut.moneytransfer.exception;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Set;
6+
7+
import javax.validation.ConstraintViolation;
8+
import javax.validation.ConstraintViolationException;
9+
import javax.ws.rs.core.GenericEntity;
10+
import javax.ws.rs.core.MediaType;
11+
import javax.ws.rs.core.Response;
12+
import javax.ws.rs.core.Response.Status;
13+
import javax.ws.rs.ext.ExceptionMapper;
14+
import javax.ws.rs.ext.Provider;
15+
16+
import org.revolut.moneytransfer.model.ErrorEntity;
17+
18+
@Provider
19+
public class CustomExceptionMapper implements ExceptionMapper<ConstraintViolationException> {
20+
21+
@Override
22+
public Response toResponse(ConstraintViolationException e) {
23+
Set<ConstraintViolation<?>> violations = e.getConstraintViolations();
24+
List<ErrorEntity> errors = new ArrayList<>();
25+
for (ConstraintViolation<?> violation : violations) {
26+
ErrorEntity error = new ErrorEntity();
27+
error.setErrorMsg(violation.getMessage());
28+
errors.add(error);
29+
}
30+
GenericEntity<List<ErrorEntity>> messages = new GenericEntity<List<ErrorEntity>>(errors) {
31+
};
32+
return Response.status(Status.BAD_REQUEST).entity(messages).type(MediaType.APPLICATION_JSON).build();
33+
}
34+
35+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.revolut.moneytransfer.model;
2+
3+
import javax.ws.rs.core.Response.Status;
4+
5+
public class CustomResponse {
6+
private Object obj;
7+
private Status responseCode;
8+
9+
10+
public CustomResponse(Object obj, Status responseCode) {
11+
this.obj = obj;
12+
this.responseCode = responseCode;
13+
}
14+
15+
public Object getObject() {
16+
return obj;
17+
}
18+
public void setObject(Object object) {
19+
this.obj = object;
20+
}
21+
public Status getResponse() {
22+
return responseCode;
23+
}
24+
public void setResponse(Status responseCode) {
25+
this.responseCode = responseCode;
26+
}
27+
28+
29+
30+
31+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.revolut.moneytransfer.model;
2+
3+
import javax.xml.bind.annotation.XmlRootElement;
4+
5+
@XmlRootElement
6+
public class ErrorEntity {
7+
8+
private String errorMsg;
9+
10+
public String getErrorMsg() {
11+
return errorMsg;
12+
}
13+
14+
public void setErrorMsg(String errorMsg) {
15+
this.errorMsg = errorMsg;
16+
}
17+
18+
19+
20+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.revolut.moneytransfer.model;
2+
3+
import javax.validation.constraints.Min;
4+
import javax.validation.constraints.Pattern;
5+
import javax.xml.bind.annotation.XmlAccessType;
6+
import javax.xml.bind.annotation.XmlAccessorType;
7+
import javax.xml.bind.annotation.XmlRootElement;
8+
9+
import org.hibernate.validator.constraints.NotBlank;
10+
11+
@XmlRootElement
12+
@XmlAccessorType(XmlAccessType.FIELD)
13+
public class Transaction {
14+
15+
private long fromUserId;
16+
private long toUserId;
17+
@Min(value = 1, message = "Amount should be greater than 0")
18+
@NotBlank(message = "Amount should not be empty")
19+
@Pattern(regexp = "[0-9]+", message = "Balance must be a valid number")
20+
private String amount;
21+
22+
public Transaction() {
23+
24+
}
25+
26+
public Transaction(long fromUserId, long toUserId, String amount) {
27+
super();
28+
this.fromUserId = fromUserId;
29+
this.toUserId = toUserId;
30+
this.amount = amount;
31+
}
32+
33+
public long getFromUserId() {
34+
return fromUserId;
35+
}
36+
37+
public void setFromUserId(long fromUserId) {
38+
this.fromUserId = fromUserId;
39+
}
40+
41+
public void setAmount(Long amount) {
42+
this.amount = amount.toString();
43+
}
44+
45+
public long getToUserId() {
46+
return toUserId;
47+
}
48+
49+
public void setToUserId(long toUserId) {
50+
this.toUserId = toUserId;
51+
}
52+
53+
public long getAmount() {
54+
return Long.parseLong(amount);
55+
}
56+
57+
@Override
58+
public String toString() {
59+
return "Transaction [fromUserId=" + fromUserId + ", toUserId=" + toUserId + ", amount=" + amount + "]";
60+
}
61+
62+
}

0 commit comments

Comments
 (0)