Skip to content

Commit ff28950

Browse files
Adding user creation endpoint
Adding the user creation endpoint and its persistence in dynamoDB
1 parent 37aa418 commit ff28950

8 files changed

Lines changed: 155 additions & 4 deletions

File tree

minesweeper-api/pom.xml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717
<java.version>11</java.version>
1818
</properties>
1919
<dependencies>
20-
<dependency>
21-
<groupId>org.springframework.boot</groupId>
22-
<artifactId>spring-boot-starter-data-jpa</artifactId>
23-
</dependency>
2420
<dependency>
2521
<groupId>org.springframework.boot</groupId>
2622
<artifactId>spring-boot-starter-web</artifactId>
@@ -31,6 +27,18 @@
3127
<artifactId>spring-boot-starter-test</artifactId>
3228
<scope>test</scope>
3329
</dependency>
30+
31+
<dependency>
32+
<groupId>com.amazonaws</groupId>
33+
<artifactId>aws-java-sdk-dynamodb</artifactId>
34+
<version>1.11.997</version>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>com.github.derjust</groupId>
39+
<artifactId>spring-data-dynamodb</artifactId>
40+
<version>5.1.0</version>
41+
</dependency>
3442
</dependencies>
3543

3644
<build>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.example.minesweeper.config;
2+
3+
import com.amazonaws.auth.AWSCredentialsProvider;
4+
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
5+
import com.amazonaws.client.builder.AwsClientBuilder;
6+
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
7+
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
8+
import org.socialsignin.spring.data.dynamodb.repository.config.EnableDynamoDBRepositories;
9+
import org.springframework.beans.factory.annotation.Value;
10+
import org.springframework.context.annotation.Bean;
11+
import org.springframework.context.annotation.Configuration;
12+
13+
@Configuration
14+
@EnableDynamoDBRepositories(basePackages = "com.example.minesweeper.repository")
15+
public class DynamoDBConfig {
16+
17+
@Value("${amazon.dynamodb.endpoint}")
18+
private String amazonDynamoDBEndpoint;
19+
20+
@Value("${amazon.aws.region}")
21+
private String amazonAWSRegion;
22+
23+
public DynamoDBConfig() {}
24+
25+
@Bean
26+
public AmazonDynamoDB amazonDynamoDB() {
27+
return configDBClientBuilder().build();
28+
}
29+
30+
private AmazonDynamoDBClientBuilder configDBClientBuilder() {
31+
AmazonDynamoDBClientBuilder builder = AmazonDynamoDBClientBuilder.standard();
32+
builder.setEndpointConfiguration(amazonEndpointConfiguration());
33+
builder.withCredentials(amazonAWSCredentials());
34+
return builder;
35+
}
36+
37+
private AwsClientBuilder.EndpointConfiguration amazonEndpointConfiguration() {
38+
return new AwsClientBuilder.EndpointConfiguration(amazonDynamoDBEndpoint, amazonAWSRegion);
39+
}
40+
41+
private AWSCredentialsProvider amazonAWSCredentials() {
42+
return new ProfileCredentialsProvider();
43+
}
44+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.example.minesweeper.controller;
2+
3+
import com.example.minesweeper.controller.request.UserCreationRequest;
4+
import com.example.minesweeper.service.UserService;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.PostMapping;
7+
import org.springframework.web.bind.annotation.RequestBody;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
@RestController
11+
public class UserController {
12+
13+
@Autowired
14+
private UserService userService;
15+
16+
@PostMapping("/user")
17+
public void createUser(@RequestBody UserCreationRequest userCreationRequest) {
18+
userService.createUser(userCreationRequest.name, userCreationRequest.password);
19+
}
20+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.example.minesweeper.controller.request;
2+
3+
public class UserCreationRequest {
4+
5+
public String name;
6+
public String password;
7+
8+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.example.minesweeper.model;
2+
3+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
4+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
5+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
6+
7+
@DynamoDBTable(tableName = "MineSweeperUser")
8+
public class User {
9+
10+
private String name;
11+
12+
private int passwordHash;
13+
14+
public User() {
15+
}
16+
17+
public User(String name, int passwordHash) {
18+
this.name = name;
19+
this.passwordHash = passwordHash;
20+
}
21+
22+
@DynamoDBHashKey(attributeName = "name")
23+
public String getName() {
24+
return name;
25+
}
26+
27+
public void setName(String name) {
28+
this.name = name;
29+
}
30+
31+
@DynamoDBAttribute(attributeName = "passwordHash")
32+
public int getPasswordHash() {
33+
return passwordHash;
34+
}
35+
36+
public void setPasswordHash(int passwordHash) {
37+
this.passwordHash = passwordHash;
38+
}
39+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.example.minesweeper.repository;
2+
3+
import com.example.minesweeper.model.User;
4+
import org.socialsignin.spring.data.dynamodb.repository.EnableScan;
5+
import org.springframework.data.repository.CrudRepository;
6+
7+
@EnableScan
8+
public interface UserRepository extends CrudRepository<User, String> {
9+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.example.minesweeper.service;
2+
3+
import com.example.minesweeper.model.User;
4+
import com.example.minesweeper.repository.UserRepository;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Service;
7+
8+
@Service
9+
public class UserService {
10+
11+
@Autowired
12+
private UserRepository userRepository;
13+
14+
public User createUser(String name, String password) {
15+
int hashedPassword = password.hashCode();
16+
User user = new User(name, hashedPassword);
17+
userRepository.save(user);
18+
return user;
19+
}
20+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1+
amazon.dynamodb.endpoint=https://dynamodb.us-west-2.amazonaws.com
2+
amazon.aws.region=us-west-2
13

4+
spring.main.allow-bean-definition-overriding=true

0 commit comments

Comments
 (0)