Skip to content

Commit ef57561

Browse files
Autowired JDA instance to Controller classes
1 parent 9bb2cb2 commit ef57561

File tree

5 files changed

+48
-27
lines changed

5 files changed

+48
-27
lines changed

src/main/java/net/javadiscord/javabot/api/routes/JDAEntity.java

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/main/java/net/javadiscord/javabot/api/routes/metrics/MetricsController.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
import com.github.benmanes.caffeine.cache.Caffeine;
44
import lombok.extern.slf4j.Slf4j;
5+
import net.dv8tion.jda.api.JDA;
56
import net.dv8tion.jda.api.entities.Guild;
67
import net.javadiscord.javabot.Bot;
78
import net.javadiscord.javabot.api.response.ApiResponseBuilder;
89
import net.javadiscord.javabot.api.response.ApiResponses;
910
import net.javadiscord.javabot.api.routes.CaffeineCache;
10-
import net.javadiscord.javabot.api.routes.JDAEntity;
1111
import net.javadiscord.javabot.api.routes.metrics.model.MetricsData;
1212
import net.javadiscord.javabot.data.config.guild.MetricsConfig;
13+
import org.springframework.beans.factory.annotation.Autowired;
1314
import org.springframework.http.HttpStatus;
1415
import org.springframework.http.MediaType;
1516
import org.springframework.http.ResponseEntity;
@@ -24,16 +25,21 @@
2425
*/
2526
@Slf4j
2627
@RestController
27-
public class MetricsController extends CaffeineCache<Long, MetricsData> implements JDAEntity {
28+
public class MetricsController extends CaffeineCache<Long, MetricsData> {
29+
private final JDA jda;
2830

2931
/**
3032
* The constructor of this class which initializes the {@link Caffeine} cache.
33+
*
34+
* @param jda The {@link Autowired} {@link JDA} instance to use.
3135
*/
32-
public MetricsController() {
36+
@Autowired
37+
public MetricsController(final JDA jda) {
3338
super(Caffeine.newBuilder()
3439
.expireAfterWrite(15, TimeUnit.MINUTES)
3540
.build()
3641
);
42+
this.jda = jda;
3743
}
3844

3945
/**
@@ -47,7 +53,7 @@ public MetricsController() {
4753
produces = MediaType.APPLICATION_JSON_VALUE
4854
)
4955
public ResponseEntity<String> getMetrics(@PathVariable(value = "guild_id") String guildId) {
50-
Guild guild = getJDA().getGuildById(guildId);
56+
Guild guild = jda.getGuildById(guildId);
5157
if (guild == null) {
5258
return new ResponseEntity<>(ApiResponses.INVALID_GUILD_IN_REQUEST, HttpStatus.BAD_REQUEST);
5359
}

src/main/java/net/javadiscord/javabot/api/routes/qotw_leaderboard/QOTWLeaderboardController.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package net.javadiscord.javabot.api.routes.qotw_leaderboard;
22

33
import com.github.benmanes.caffeine.cache.Caffeine;
4+
import net.dv8tion.jda.api.JDA;
45
import net.dv8tion.jda.api.entities.Guild;
56
import net.javadiscord.javabot.Bot;
67
import net.javadiscord.javabot.api.response.ApiResponseBuilder;
78
import net.javadiscord.javabot.api.response.ApiResponses;
89
import net.javadiscord.javabot.api.routes.CaffeineCache;
9-
import net.javadiscord.javabot.api.routes.JDAEntity;
1010
import net.javadiscord.javabot.api.routes.qotw_leaderboard.model.QOTWMemberData;
1111
import net.javadiscord.javabot.systems.qotw.QOTWPointsService;
1212
import net.javadiscord.javabot.util.Checks;
13+
import org.springframework.beans.factory.annotation.Autowired;
1314
import org.springframework.http.HttpStatus;
1415
import org.springframework.http.MediaType;
1516
import org.springframework.http.ResponseEntity;
@@ -25,16 +26,21 @@
2526
* Handles all GET-Requests on the {guild_id}/qotw/leaderboard route.
2627
*/
2728
@RestController
28-
public class QOTWLeaderboardController extends CaffeineCache<Long, List<QOTWMemberData>> implements JDAEntity {
29+
public class QOTWLeaderboardController extends CaffeineCache<Long, List<QOTWMemberData>> {
30+
private final JDA jda;
2931

3032
/**
3133
* The constructor of this class which initializes the {@link Caffeine} cache.
34+
*
35+
* @param jda The {@link JDA} instance to use.
3236
*/
33-
public QOTWLeaderboardController() {
37+
@Autowired
38+
public QOTWLeaderboardController(final JDA jda) {
3439
super(Caffeine.newBuilder()
3540
.expireAfterWrite(10, TimeUnit.MINUTES)
3641
.build()
3742
);
43+
this.jda = jda;
3844
}
3945

4046
/**
@@ -53,7 +59,7 @@ public ResponseEntity<String> getQOTWLeaderboard(
5359
@PathVariable(value = "guild_id") String guildId,
5460
@RequestParam(value = "amount", defaultValue = "3") String amountParam
5561
) {
56-
Guild guild = getJDA().getGuildById(guildId);
62+
Guild guild = jda.getGuildById(guildId);
5763
if (guild == null) {
5864
return new ResponseEntity<>(ApiResponses.INVALID_GUILD_IN_REQUEST, HttpStatus.BAD_REQUEST);
5965
}

src/main/java/net/javadiscord/javabot/api/routes/user_profile/UserProfileController.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package net.javadiscord.javabot.api.routes.user_profile;
22

33
import com.github.benmanes.caffeine.cache.Caffeine;
4+
import net.dv8tion.jda.api.JDA;
45
import net.dv8tion.jda.api.entities.Guild;
56
import net.dv8tion.jda.api.entities.User;
67
import net.javadiscord.javabot.Bot;
78
import net.javadiscord.javabot.api.response.ApiResponseBuilder;
89
import net.javadiscord.javabot.api.response.ApiResponses;
910
import net.javadiscord.javabot.api.routes.CaffeineCache;
10-
import net.javadiscord.javabot.api.routes.JDAEntity;
1111
import net.javadiscord.javabot.api.routes.user_profile.model.HelpAccountData;
1212
import net.javadiscord.javabot.api.routes.user_profile.model.UserProfileData;
1313
import net.javadiscord.javabot.systems.help.HelpExperienceService;
@@ -20,6 +20,7 @@
2020
import net.javadiscord.javabot.systems.user_preferences.model.UserPreference;
2121
import net.javadiscord.javabot.util.ExceptionLogger;
2222
import net.javadiscord.javabot.util.Pair;
23+
import org.springframework.beans.factory.annotation.Autowired;
2324
import org.springframework.http.HttpStatus;
2425
import org.springframework.http.MediaType;
2526
import org.springframework.http.ResponseEntity;
@@ -38,16 +39,21 @@
3839
* Handles all GET-Requests on the {guild_id}/{user_id} route.
3940
*/
4041
@RestController
41-
public class UserProfileController extends CaffeineCache<Pair<Long, Long>, UserProfileData> implements JDAEntity {
42+
public class UserProfileController extends CaffeineCache<Pair<Long, Long>, UserProfileData> {
43+
private final JDA jda;
4244

4345
/**
4446
* The constructor of this class which initializes the {@link Caffeine} cache.
47+
*
48+
* @param jda The {@link Autowired} {@link JDA} instance to use.
4549
*/
46-
public UserProfileController() {
50+
@Autowired
51+
public UserProfileController(final JDA jda) {
4752
super(Caffeine.newBuilder()
4853
.expireAfterWrite(10, TimeUnit.MINUTES)
4954
.build()
5055
);
56+
this.jda = jda;
5157
}
5258

5359
/**
@@ -65,11 +71,11 @@ public ResponseEntity<String> getUserProfile(
6571
@PathVariable(value = "guild_id") String guildId,
6672
@PathVariable(value = "user_id") String userId
6773
) {
68-
Guild guild = getJDA().getGuildById(guildId);
74+
Guild guild = jda.getGuildById(guildId);
6975
if (guild == null) {
7076
return new ResponseEntity<>(ApiResponses.INVALID_GUILD_IN_REQUEST, HttpStatus.BAD_REQUEST);
7177
}
72-
User user = getJDA().retrieveUserById(userId).complete();
78+
User user = jda.retrieveUserById(userId).complete();
7379
if (user == null) {
7480
return new ResponseEntity<>(ApiResponses.INVALID_USER_IN_REQUEST, HttpStatus.BAD_REQUEST);
7581
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package net.javadiscord.javabot.data;
2+
3+
import net.dv8tion.jda.api.JDA;
4+
import net.javadiscord.javabot.Bot;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
8+
/**
9+
* This class holds all configuration settings and {@link Bean}s.
10+
*/
11+
@Configuration
12+
public class SpringConfig {
13+
@Bean
14+
public JDA getJDA() {
15+
return Bot.getDih4jda().getJDA();
16+
}
17+
}

0 commit comments

Comments
 (0)