Skip to content

Commit 42a0203

Browse files
Optimized Controller classes
1 parent ef57561 commit 42a0203

File tree

6 files changed

+15
-38
lines changed

6 files changed

+15
-38
lines changed

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
import net.dv8tion.jda.api.JDA;
66
import net.dv8tion.jda.api.entities.Guild;
77
import net.javadiscord.javabot.Bot;
8-
import net.javadiscord.javabot.api.response.ApiResponseBuilder;
98
import net.javadiscord.javabot.api.response.ApiResponses;
109
import net.javadiscord.javabot.api.routes.CaffeineCache;
1110
import net.javadiscord.javabot.api.routes.metrics.model.MetricsData;
1211
import net.javadiscord.javabot.data.config.guild.MetricsConfig;
1312
import org.springframework.beans.factory.annotation.Autowired;
1413
import org.springframework.http.HttpStatus;
15-
import org.springframework.http.MediaType;
1614
import org.springframework.http.ResponseEntity;
1715
import org.springframework.web.bind.annotation.GetMapping;
1816
import org.springframework.web.bind.annotation.PathVariable;
@@ -49,10 +47,9 @@ public MetricsController(final JDA jda) {
4947
* @return The {@link ResponseEntity}.
5048
*/
5149
@GetMapping(
52-
value = "{guild_id}/metrics",
53-
produces = MediaType.APPLICATION_JSON_VALUE
50+
value = "{guild_id}/metrics"
5451
)
55-
public ResponseEntity<String> getMetrics(@PathVariable(value = "guild_id") String guildId) {
52+
public ResponseEntity<?> getMetrics(@PathVariable(value = "guild_id") long guildId) {
5653
Guild guild = jda.getGuildById(guildId);
5754
if (guild == null) {
5855
return new ResponseEntity<>(ApiResponses.INVALID_GUILD_IN_REQUEST, HttpStatus.BAD_REQUEST);
@@ -67,6 +64,6 @@ public ResponseEntity<String> getMetrics(@PathVariable(value = "guild_id") Strin
6764
data.setActiveMembers(config.getActiveMembers());
6865
getCache().put(guild.getIdLong(), data);
6966
}
70-
return new ResponseEntity<>(new ApiResponseBuilder().add("metrics", data).build(), HttpStatus.OK);
67+
return new ResponseEntity<>(data, HttpStatus.OK);
7168
}
7269
}

src/main/java/net/javadiscord/javabot/api/routes/metrics/model/MetricsData.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package net.javadiscord.javabot.api.routes.metrics.model;
22

33
import lombok.Data;
4+
import lombok.EqualsAndHashCode;
45

56
/**
67
* API-Data class which contains all necessary information about a guilds'
78
* metrics.
89
*/
910
@Data
11+
@EqualsAndHashCode(callSuper = false)
1012
public class MetricsData {
1113
private long memberCount;
1214
private long onlineCount;

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
import net.dv8tion.jda.api.JDA;
55
import net.dv8tion.jda.api.entities.Guild;
66
import net.javadiscord.javabot.Bot;
7-
import net.javadiscord.javabot.api.response.ApiResponseBuilder;
87
import net.javadiscord.javabot.api.response.ApiResponses;
98
import net.javadiscord.javabot.api.routes.CaffeineCache;
109
import net.javadiscord.javabot.api.routes.qotw_leaderboard.model.QOTWMemberData;
1110
import net.javadiscord.javabot.systems.qotw.QOTWPointsService;
12-
import net.javadiscord.javabot.util.Checks;
1311
import org.springframework.beans.factory.annotation.Autowired;
1412
import org.springframework.http.HttpStatus;
1513
import org.springframework.http.MediaType;
@@ -48,25 +46,21 @@ public QOTWLeaderboardController(final JDA jda) {
4846
* amount of qotw-points.
4947
*
5048
* @param guildId The guilds' id.
51-
* @param amountParam The amount of users to return. Defaults to 3.
49+
* @param amount The amount of users to return. Defaults to 3.
5250
* @return The {@link ResponseEntity}.
5351
*/
5452
@GetMapping(
5553
value = "{guild_id}/qotw/leaderboard",
5654
produces = MediaType.APPLICATION_JSON_VALUE
5755
)
58-
public ResponseEntity<String> getQOTWLeaderboard(
59-
@PathVariable(value = "guild_id") String guildId,
60-
@RequestParam(value = "amount", defaultValue = "3") String amountParam
56+
public ResponseEntity<?> getQOTWLeaderboard(
57+
@PathVariable(value = "guild_id") long guildId,
58+
@RequestParam(value = "amount", defaultValue = "3") int amount
6159
) {
6260
Guild guild = jda.getGuildById(guildId);
6361
if (guild == null) {
6462
return new ResponseEntity<>(ApiResponses.INVALID_GUILD_IN_REQUEST, HttpStatus.BAD_REQUEST);
6563
}
66-
if (!Checks.checkInteger(amountParam)) {
67-
return new ResponseEntity<>(ApiResponses.INVALID_NUMBER_IN_REQUEST, HttpStatus.BAD_REQUEST);
68-
}
69-
int amount = Integer.parseInt(amountParam);
7064
QOTWPointsService service = new QOTWPointsService(Bot.getDataSource());
7165
List<QOTWMemberData> members = getCache().getIfPresent(guild.getIdLong());
7266
if (members == null || members.isEmpty()) {
@@ -83,6 +77,6 @@ public ResponseEntity<String> getQOTWLeaderboard(
8377
.toList();
8478
getCache().put(guild.getIdLong(), members);
8579
}
86-
return new ResponseEntity<>(new ApiResponseBuilder().add("leaderboard", members.stream().limit(amount).toList()).build(), HttpStatus.OK);
80+
return new ResponseEntity<>(members.stream().limit(amount).toList(), HttpStatus.OK);
8781
}
8882
}

src/main/java/net/javadiscord/javabot/api/routes/qotw_leaderboard/model/QOTWMemberData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
/**
77
* API-Data class which contains all necessary information about a single users'
8-
* qotw account.
8+
* QOTW-Account.
99
*/
1010
@Data
1111
public class QOTWMemberData {

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import net.dv8tion.jda.api.entities.Guild;
66
import net.dv8tion.jda.api.entities.User;
77
import net.javadiscord.javabot.Bot;
8-
import net.javadiscord.javabot.api.response.ApiResponseBuilder;
98
import net.javadiscord.javabot.api.response.ApiResponses;
109
import net.javadiscord.javabot.api.routes.CaffeineCache;
1110
import net.javadiscord.javabot.api.routes.user_profile.model.HelpAccountData;
@@ -67,9 +66,9 @@ public UserProfileController(final JDA jda) {
6766
value = "{guild_id}/{user_id}",
6867
produces = MediaType.APPLICATION_JSON_VALUE
6968
)
70-
public ResponseEntity<String> getUserProfile(
71-
@PathVariable(value = "guild_id") String guildId,
72-
@PathVariable(value = "user_id") String userId
69+
public ResponseEntity<?> getUserProfile(
70+
@PathVariable(value = "guild_id") long guildId,
71+
@PathVariable(value = "user_id") long userId
7372
) {
7473
Guild guild = jda.getGuildById(guildId);
7574
if (guild == null) {
@@ -107,7 +106,7 @@ public ResponseEntity<String> getUserProfile(
107106
// Insert into cache
108107
getCache().put(new Pair<>(guild.getIdLong(), user.getIdLong()), data);
109108
}
110-
return new ResponseEntity<>(new ApiResponseBuilder().add("profile", data).build(), HttpStatus.OK);
109+
return new ResponseEntity<>(data, HttpStatus.OK);
111110
} catch (SQLException e) {
112111
ExceptionLogger.capture(e, getClass().getSimpleName());
113112
return new ResponseEntity<>(ApiResponses.INTERNAL_SERVER_ERROR, HttpStatus.INTERNAL_SERVER_ERROR);

src/main/java/net/javadiscord/javabot/util/Checks.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -103,21 +103,6 @@ public static boolean checkColor(String nm) {
103103
}
104104
}
105105

106-
/**
107-
* Checks if the specified string is a valid {@link Integer}.
108-
*
109-
* @param integer The provided integer, as a string.
110-
* @return Whether the specified string is a valid {@link Integer}.
111-
*/
112-
public static boolean checkInteger(String integer) {
113-
try {
114-
int value = Integer.parseInt(integer);
115-
return true;
116-
} catch (NumberFormatException e) {
117-
return false;
118-
}
119-
}
120-
121106
public static boolean checkHexColor(String hex) {
122107
return HEX_PATTERN.matcher(hex).matches();
123108
}

0 commit comments

Comments
 (0)