Skip to content

Commit

Permalink
feat | add redisservice
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagochirana committed Aug 4, 2024
1 parent 784c7f1 commit 19f9f2d
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 8 deletions.
5 changes: 5 additions & 0 deletions app/backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package br.com.streampix.app.controllers;

import br.com.streampix.app.models.GetRedis;
import br.com.streampix.app.models.records.checkout.CheckoutJson;
import br.com.streampix.app.services.CheckoutService;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
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.RestController;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/checkout")
Expand All @@ -17,7 +16,12 @@ public class CheckoutController {
private final CheckoutService checkoutService;

@PostMapping
public ResponseEntity doCheckout(@RequestBody CheckoutJson json){
public ResponseEntity doCheckout(@RequestBody CheckoutJson json) throws JsonProcessingException {
return checkoutService.doCheckout(json);
}

@GetMapping
public ResponseEntity getCheckout(@RequestBody GetRedis json) throws JsonProcessingException {
return checkoutService.getRedis(json.nickname());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package br.com.streampix.app.models;

public record GetRedis(
String nickname
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import static org.springframework.http.HttpMethod.GET;
import static org.springframework.http.HttpMethod.POST;
import static org.springframework.security.config.http.SessionCreationPolicy.STATELESS;

Expand All @@ -32,6 +33,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
.permitAll()
.requestMatchers(POST,"/api/v1/auth/login").permitAll()
.requestMatchers(POST,"/api/v1/checkout").permitAll()
.requestMatchers(GET,"/api/v1/checkout").permitAll()
.requestMatchers(POST,"/api/v1/auth/sign_up").permitAll()
.requestMatchers(POST,"/api/v1/auth/refresh_token").permitAll()
.anyRequest()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,33 @@
import br.com.streampix.app.models.checkout.Checkout;
import br.com.streampix.app.models.records.checkout.CheckoutJson;
import br.com.streampix.app.repositories.CheckoutRepositoryRedis;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
public class CheckoutService {

private final CheckoutRepositoryRedis checkoutRepositoryRedis;
private final RedisService redis;

public ResponseEntity doCheckout(CheckoutJson json){
public ResponseEntity doCheckout(CheckoutJson json) throws JsonProcessingException {
Checkout checkout = new Checkout(null, json.nickname(), json.message(), json.value(), "100");
Checkout co = checkoutRepositoryRedis.save(checkout);
return ResponseEntity.ok().body("Deu bom rapaziada ===> " + co.toString());
redis.put("CO_"+json.nickname(), checkout);
return ResponseEntity.ok().body("Deu bom em por no Redis");
}

public ResponseEntity getRedis(String nickname) throws JsonProcessingException {
List<Checkout> checkouts = redis.findByAttribute("CO_"+nickname, "nickname", nickname, Checkout.class);
if (checkouts.isEmpty()) {
return ResponseEntity.notFound().build();
} else {
return ResponseEntity.ok(checkouts);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package br.com.streampix.app.services;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
@RequiredArgsConstructor
public class RedisService {

private final RedisTemplate<String, String> redisTemplate;
private final ObjectMapper objectMapper;

public <T> void put(String key, T model) throws JsonProcessingException {
String modelJson = objectMapper.writeValueAsString(model);
redisTemplate.opsForValue().set(key, modelJson);
}

public <T> T get(String key, Class<T> clazz) throws JsonProcessingException {
String modelJson = redisTemplate.opsForValue().get(key);
if (modelJson != null) {
return objectMapper.readValue(modelJson, clazz);
} else {
return null;
}
}

public <T> List<T> findByAttribute(String prefix, String attributeName, String attributeValue, Class<T> clazz) throws JsonProcessingException, JsonMappingException {
List<T> models = new ArrayList<>();
for (String key : Objects.requireNonNull(redisTemplate.keys(prefix + "*"))) {
String modelJson = redisTemplate.opsForValue().get(key);
if (modelJson != null) {
T model = objectMapper.readValue(modelJson, clazz);
String attributeJsonValue = objectMapper.convertValue(model, Map.class).get(attributeName).toString();
if (attributeValue.equals(attributeJsonValue)) {
models.add(model);
}
}
}
return models;
}
}

0 comments on commit 19f9f2d

Please sign in to comment.