Skip to content

Commit

Permalink
add: dependency injection and approving joke service method. #66
Browse files Browse the repository at this point in the history
  • Loading branch information
pitzzahh committed Feb 26, 2023
1 parent 4a251f5 commit 46e9bd3
Showing 1 changed file with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@

package tech.araopj.springpitzzahhbot.commands.slash_command.commands.joke.service;

import tech.araopj.springpitzzahhbot.commands.slash_command.commands.joke.approveJoke.entity.*;
import tech.araopj.springpitzzahhbot.commands.slash_command.commands.joke.getJoke.entity.*;
import tech.araopj.springpitzzahhbot.commands.slash_command.commands.joke.approveJoke.entity.Joke;
import tech.araopj.springpitzzahhbot.config.secret.service.SecretService;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import com.fasterxml.jackson.core.JsonProcessingException;
import tech.araopj.springpitzzahhbot.config.HttpConfig;
Expand All @@ -46,7 +47,10 @@

@Slf4j
@Service
public record JokesService(HttpConfig httpConfig) {
public record JokesService(
SecretService secretService,
HttpConfig httpConfig
) {

public Collection<Category> getCategories() {
var httpResponseCompletableFuture = httpConfig.httpClient()
Expand Down Expand Up @@ -137,7 +141,7 @@ public Collection<Joke> getSubmittedJokes() {
log.error("Error while getting categories", e);
throw new RuntimeException(e);
}
ObjectMapper objectMapper = new ObjectMapper();
var objectMapper = new ObjectMapper();
Collection<Joke> jokes;
try {
jokes = objectMapper.readValue(stringHttpResponse.body(), new TypeReference<>() {});
Expand All @@ -151,4 +155,25 @@ public Collection<Joke> getSubmittedJokes() {
.filter(j -> !j.approved())
.collect(Collectors.toCollection(ArrayList::new));
}

public boolean approveJoke(Joke joke) {
var httpResponseCompletableFuture = httpConfig.httpClient()
.sendAsync(HttpRequest.newBuilder()
.PUT(HttpRequest.BodyPublishers.ofString(new Gson().toJson(new JokeBody(secretService().getKey(), joke.joke()))))
.uri(URI.create("https://jokes.araopj.tech/v1/submit/approve"))
.build(),
HttpResponse.BodyHandlers.ofString()
);
HttpResponse<String> stringHttpResponse;

try {
stringHttpResponse = httpResponseCompletableFuture.get();
} catch (InterruptedException | ExecutionException e) {
log.error("Error while approving joke", e);
throw new RuntimeException(e);
}

log.info("Approve Joke Response: {}", stringHttpResponse.body());
return stringHttpResponse.body().equals("Joke approved successfully");
}
}

0 comments on commit 46e9bd3

Please sign in to comment.