Skip to content

Commit

Permalink
MoneyTransfer example API ready, w/o database part and validation
Browse files Browse the repository at this point in the history
  • Loading branch information
iryndin committed Apr 11, 2017
1 parent 0b5cb6b commit ce47d90
Show file tree
Hide file tree
Showing 16 changed files with 655 additions and 6 deletions.
19 changes: 13 additions & 6 deletions money_transfer_api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ or

{
"status": "FAIL",
"errorId": 12345,
"errorCode": 12345,
"errorMessage": "No such account"
}
```
Expand All @@ -81,7 +81,8 @@ Body:
``` json
{
"amount": 12345,
"type": 11
"type": 11,
"description": "example tx"
}
```

Expand All @@ -99,11 +100,17 @@ or

{
"status": "FAIL",
"errorId": 12345,
"errorCode": 12345,
"errorMessage": "Not enough money on credit account"
}
```

CURL command line:

```
curl -X PUT --data "{\"amount\": 12345, \"type\": 11, \"description\": \"example tx\"}" -H "Content-Type: application/json" http://localhost:8080/transfer/1/2
```

### Get transaction object by ID

*GET /transfer/{tx_id}*
Expand All @@ -128,7 +135,7 @@ or

{
"status": "FAIL",
"errorId": 12345,
"errorCode": 12345,
"errorMessage": "No such transaction"
}
```
Expand Down Expand Up @@ -174,7 +181,7 @@ or

{
"status": "FAIL",
"errorId": 12345,
"errorCode": 12345,
"errorMessage": "Wrong format of credit_account_id parameter"
}
```
```
30 changes: 30 additions & 0 deletions money_transfer_api/app.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#template: Hello, %s!
#defaultName: ${DW_DEFAULT_NAME:-Stranger}

# Database settings.
database:
# the name of your JDBC driver
driverClass: org.h2.Driver
# the username
user: sa
# the password
password:
# the JDBC URL
url: jdbc:h2:mem:~/money;DB_CLOSE_ON_EXIT=FALSE

# use the simple server factory if you only want to run on a single port
#server:
# type: simple
# connector:
# type: http
# port: 8080

server:
# softNofileLimit: 1000
# hardNofileLimit: 1000
applicationConnectors:
- type: http
port: 8080
adminConnectors:
- type: http
port: 8081
20 changes: 20 additions & 0 deletions money_transfer_api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,30 @@
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.fork>true</maven.compiler.fork>

<dropwizard.version>1.1.0</dropwizard.version>
<h2.version>1.4.194</h2.version>
<junit.version>4.12</junit.version>
</properties>

<dependencies>
<!-- Dropwizard dependencies -->

<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>${dropwizard.version}</version>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-hibernate</artifactId>
<version>${dropwizard.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package net.iryndin.mtapi;

import io.dropwizard.Application;
import io.dropwizard.db.DataSourceFactory;
import io.dropwizard.hibernate.HibernateBundle;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import net.iryndin.mtapi.core.AccountEntity;
import net.iryndin.mtapi.resources.AccountResource;
import net.iryndin.mtapi.resources.TransactionResource;

/**
* @author iryndin
* @since 10/04/17
*/
public class MTAPIApplication extends Application<MTAPIConfiguration> {

public static void main(String[] args) throws Exception {
new MTAPIApplication().run(args);
}

private final HibernateBundle<MTAPIConfiguration> hibernateBundle =
new HibernateBundle<MTAPIConfiguration>(AccountEntity.class) {
@Override
public DataSourceFactory getDataSourceFactory(MTAPIConfiguration configuration) {
return configuration.getDataSourceFactory();
}
};

@Override
public String getName() {
return "Money Transfer API";
}

@Override
public void initialize(Bootstrap<MTAPIConfiguration> bootstrap) {
bootstrap.addBundle(hibernateBundle);
}

@Override
public void run(MTAPIConfiguration configuration, Environment environment) {
environment.jersey().register(new AccountResource());
environment.jersey().register(new TransactionResource());
/*
environment.healthChecks().register("template", new TemplateHealthCheck(template));
environment.admin().addTask(new EchoTask());
environment.jersey().register(DateRequiredFeature.class);
environment.jersey().register(new AuthDynamicFeature(new BasicCredentialAuthFilter.Builder<User>()
.setAuthenticator(new ExampleAuthenticator())
.setAuthorizer(new ExampleAuthorizer())
.setRealm("SUPER SECRET STUFF")
.buildAuthFilter()));
environment.jersey().register(new AuthValueFactoryProvider.Binder<>(User.class));
environment.jersey().register(RolesAllowedDynamicFeature.class);
environment.jersey().register(new HelloWorldResource(template));
environment.jersey().register(new ViewResource());
environment.jersey().register(new ProtectedResource());
environment.jersey().register(new PeopleResource(dao));
environment.jersey().register(new PersonResource(dao));
environment.jersey().register(new FilteredResource());*/
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.iryndin.mtapi;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.dropwizard.Configuration;
import io.dropwizard.db.DataSourceFactory;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
* @author iryndin
* @since 10/04/17
*/
public class MTAPIConfiguration extends Configuration {

@Valid
@NotNull
private DataSourceFactory database = new DataSourceFactory();

@JsonProperty("database")
public DataSourceFactory getDataSourceFactory() {
return database;
}

@JsonProperty("database")
public void setDataSourceFactory(DataSourceFactory dataSourceFactory) {
this.database = dataSourceFactory;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package net.iryndin.mtapi.api;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Optional;

/**
* @author iryndin
* @since 11/04/17
*/
public class ApiResponse {
public static String STATUS_OK = "OK";
public static String STATUS_FAIL = "FAIL";

private String status;

public ApiResponse() {
}

public ApiResponse(String status) {
this.status = status;

}



@JsonProperty
public String getStatus() {
return status;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package net.iryndin.mtapi.api;

/**
* @author iryndin
* @since 11/04/17
*/
public class ApiResponseError extends ApiResponse {
private String errorMessage;
private Integer errorCode;

public ApiResponseError() {
}

public ApiResponseError(String errorMessage, Integer errorCode) {
super(STATUS_FAIL);
this.errorMessage = errorMessage;
this.errorCode = errorCode;
}

public String getErrorMessage() {
return errorMessage;
}

public Integer getErrorCode() {
return errorCode;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package net.iryndin.mtapi.api;

import com.fasterxml.jackson.annotation.JsonProperty;

/**
* @author iryndin
* @since 11/04/17
*/
public class ApiResponseOK<T> extends ApiResponse {
private T data;

public ApiResponseOK() {
}

public ApiResponseOK(T data) {
super(STATUS_OK);
this.data = data;
}

@JsonProperty
public T getData() {
return data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package net.iryndin.mtapi.api;

import com.fasterxml.jackson.annotation.JsonProperty;

/**
* @author iryndin
* @since 11/04/17
*/
public class BalanceResponse {
private long balance;

public BalanceResponse() {
}

public BalanceResponse(long balance) {
this.balance = balance;
}

@JsonProperty
public long getBalance() {
return balance;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package net.iryndin.mtapi.api;

import com.fasterxml.jackson.annotation.JsonProperty;

/**
* @author iryndin
* @since 11/04/17
*/
public class TransferRequest {
private Long amount;
private Integer type;
private String description;

public TransferRequest() {
}

public TransferRequest(Long amount, Integer type, String description) {
this.amount = amount;
this.type = type;
this.description = description;
}

@JsonProperty
public Long getAmount() {
return amount;
}

@JsonProperty
public Integer getType() {
return type;
}

@JsonProperty
public String getDescription() {
return description;
}

@Override
public String toString() {
return "TransferRequest{" +
"amount=" + amount +
", type=" + type +
", description='" + description + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package net.iryndin.mtapi.api;

import com.fasterxml.jackson.annotation.JsonProperty;

/**
* @author iryndin
* @since 11/04/17
*/
public class TransferResponse {
private long txId;

public TransferResponse() {
}

public TransferResponse(long txId) {
this.txId = txId;
}

@JsonProperty
public long getTxId() {
return txId;
}
}
Loading

0 comments on commit ce47d90

Please sign in to comment.