Skip to content

Commit

Permalink
Implement server with redis
Browse files Browse the repository at this point in the history
  • Loading branch information
hdescottes committed Nov 17, 2023
1 parent 9995262 commit d19f16c
Show file tree
Hide file tree
Showing 23 changed files with 944 additions and 1 deletion.
26 changes: 26 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,32 @@ jobs:

steps:
- uses: actions/checkout@v4

- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'adopt'
- name: Validate Gradle wrapper
uses: gradle/wrapper-validation-action@e6e38bacfdf1a337459f332974bb2327a31aaf4b
- name: Cache Gradle packages
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: Build with Gradle
run: ./gradlew build
- name: Cleanup Gradle Cache
# Remove some files from the Gradle cache, so they aren't cached by GitHub Actions.
# Restoring these files from a GitHub Actions cache might cause problems for future builds.
run: |
rm -f ~/.gradle/caches/modules-2/modules-2.lock
rm -f ~/.gradle/caches/modules-2/gc.properties
- name: Use Node 18.x
uses: actions/setup-node@v4
with:
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ yarn-error.logy
*/coverage
/libpeerconnection.log
testem.log
/typings
/typings
*.exe
60 changes: 60 additions & 0 deletions app-api/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
val springBootVersion by extra { "3.1.5" }
val redisJedisVersion by extra { "5.0.2" }
val apacheLang3Version by extra { "3.13.0" }

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:3.1.5")
}
}

plugins {
java
id("org.springframework.boot") version "3.1.5"
id("io.spring.dependency-management") version "1.1.3"
}

group = "com.project"
version = "0.0.1-SNAPSHOT"

java {
sourceCompatibility = JavaVersion.VERSION_17
}

tasks.named<Test>("test") {
useJUnitPlatform()
}

abstract class Redis : DefaultTask() {
@TaskAction
fun greet() {
project.exec {
commandLine("redis-server.exe")
}
}
}

// Create a task using the task type
tasks.register<Redis>("redis")

dependencyManagement {
imports {
mavenBom("org.springframework.boot:spring-boot-starter:$springBootVersion")
}
}

repositories {
mavenCentral()
}

dependencies {
implementation("org.springframework.boot:spring-boot-starter:$springBootVersion")
implementation("org.springframework.boot:spring-boot-starter-web:$springBootVersion")
implementation("org.springframework.boot:spring-boot-starter-data-redis:$springBootVersion")
implementation("redis.clients:jedis:$redisJedisVersion")
testImplementation("org.springframework.boot:spring-boot-starter-test:$springBootVersion")
testImplementation("org.apache.commons:commons-lang3:$apacheLang3Version")
}
Binary file added app-api/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
7 changes: 7 additions & 0 deletions app-api/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
13 changes: 13 additions & 0 deletions app-api/src/main/java/com/project/reactdashboard/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.project.reactdashboard;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.project.reactdashboard.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

private static final String HOST = "localhost";

private static final int PORT = 6379;

@Bean
RedisConnectionFactory jedisConnectionFactory() {
RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(HOST, PORT);
return new LettuceConnectionFactory(configuration);
}

@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(jedisConnectionFactory());
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new JdkSerializationRedisSerializer());
template.setValueSerializer(new JdkSerializationRedisSerializer());
template.setEnableTransactionSupport(true);
template.afterPropertiesSet();
return template;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.project.reactdashboard.entities;

import org.springframework.data.redis.core.RedisHash;

import java.io.Serializable;

@RedisHash("Model")
public class Model implements Serializable {

private int id;

private String value;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public Model() {
}

private Model(ModelBuilder builder) {
this.id = builder.id;
this.value = builder.value;
}

public static class ModelBuilder{

private int id;

private String value;

public ModelBuilder(){
}

public ModelBuilder withId(int id) {
this.id = id;
return this;
}

public ModelBuilder withValue(String value) {
this.value = value;
return this;
}

public Model build(){
return new Model(this);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.project.reactdashboard.entities;

public class ModelDto {

private int id;

private String value;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public ModelDto() {
}

private ModelDto(ModelDtoBuilder builder) {
this.id = builder.id;
this.value = builder.value;
}

public static class ModelDtoBuilder{

private int id;

private String value;

public ModelDtoBuilder(){
}

public ModelDtoBuilder withId(int id) {
this.id = id;
return this;
}

public ModelDtoBuilder withValue(String value) {
this.value = value;
return this;
}

public ModelDto build(){
return new ModelDto(this);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.project.reactdashboard.stockmarket;

import com.project.reactdashboard.entities.Model;
import com.project.reactdashboard.entities.ModelDto;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/stock-market")
public class StockMarketController {

private final StockMarketMapper mapper;

private final StockMarketService service;

public StockMarketController(StockMarketMapper mapper, StockMarketService service) {
this.mapper = mapper;
this.service = service;
}

@GetMapping
@ResponseStatus(HttpStatus.OK)
public ResponseEntity<List<ModelDto>> findAll() {
List<Model> models = service.findAll();
List<ModelDto> dtos = mapper.toListDto(models);
return ResponseEntity.ok(dtos);
}

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public ResponseEntity<Integer> create(@RequestBody ModelDto modeldto) {
Model model = mapper.toEntity(modeldto);
service.create(model);
return ResponseEntity.ok(1);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.project.reactdashboard.stockmarket;

import com.project.reactdashboard.entities.Model;
import com.project.reactdashboard.entities.ModelDto;

import java.util.List;

public class StockMarketMapper {

public Model toEntity(ModelDto dto) {
return new Model.ModelBuilder()
.withId(dto.getId())
.withValue(dto.getValue())
.build();
}

public ModelDto toDto(Model entity) {
return new ModelDto.ModelDtoBuilder()
.withId(entity.getId())
.withValue(entity.getValue())
.build();
}

public List<ModelDto> toListDto(List<Model> models) {
return models.stream()
.map(this::toDto)
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.project.reactdashboard.stockmarket;

import com.project.reactdashboard.entities.Model;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface StockMarketRepository extends CrudRepository<Model, String> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.project.reactdashboard.stockmarket;

import com.project.reactdashboard.entities.Model;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StockMarketService {

private final StockMarketRepository repository;

public StockMarketService(StockMarketRepository repository) {
this.repository = repository;
}

public void create(Model model) {
repository.save(model);
}

public List<Model> findAll() {
return (List<Model>) repository.findAll();
}
}
1 change: 1 addition & 0 deletions app-api/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Loading

0 comments on commit d19f16c

Please sign in to comment.