Skip to content

Add submissionCalendar field #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/leetcode/api/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public StatsResponse getStats(@PathVariable Optional<String> username) {
} else {
String status = "error";
String msg = "please enter your username (ex: leetcode-stats-api.herokuapp.com/LeetCodeUsername)";
return new StatsResponse(status, msg, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
return StatsResponse.error(status, msg);
}
}
}
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/leetcode/api/model/StatsResponse.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package leetcode.api.model;

import java.util.Collections;
import java.util.Map;

public class StatsResponse {
private final String status;
private final String message;
Expand All @@ -15,8 +18,9 @@ public class StatsResponse {
private final int ranking;
private final int contributionPoints;
private final int reputation;
private final Map<String, Integer> submissionCalendar;

public StatsResponse(String status, String message, int totalSolved, int totalQuestions, int easySolved, int totalEasy, int mediumSolved, int totalMedium, int hardSolved, int totalHard, float acceptanceRate, int ranking, int contributionPoints, int reputation) {
public StatsResponse(String status, String message, int totalSolved, int totalQuestions, int easySolved, int totalEasy, int mediumSolved, int totalMedium, int hardSolved, int totalHard, float acceptanceRate, int ranking, int contributionPoints, int reputation, Map<String, Integer> submissionCalendar) {
this.status = status;
this.message = message;
this.totalSolved = totalSolved;
Expand All @@ -31,6 +35,11 @@ public StatsResponse(String status, String message, int totalSolved, int totalQu
this.ranking = ranking;
this.contributionPoints = contributionPoints;
this.reputation = reputation;
this.submissionCalendar = submissionCalendar;
}

public static StatsResponse error(String status, String message) {
return new StatsResponse(status, message, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Collections.emptyMap());
}

public String getStatus() {
Expand Down Expand Up @@ -87,6 +96,10 @@ public int getReputation() {
return reputation;
}

public Map<String, Integer> getSubmissionCalendar() {
return submissionCalendar;
}

public boolean equals(StatsResponse s) {
// Compared with itself
if (s == this) {
Expand Down
22 changes: 16 additions & 6 deletions src/main/java/leetcode/api/service/StatsServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Map;
import java.util.TreeMap;

import org.json.JSONException;
import org.json.JSONArray;
Expand All @@ -23,7 +25,7 @@ public StatsResponse getStats(String username) {
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("application/json");
String query = String.format("{\"query\":\"query getUserProfile($username: String!) { allQuestionsCount { difficulty count } matchedUser(username: $username) { contributions { points } profile { reputation ranking } submitStats { acSubmissionNum { difficulty count submissions } totalSubmissionNum { difficulty count submissions } } } } \",\"variables\":{\"username\":\"%s\"}}", username);
String query = String.format("{\"query\":\"query getUserProfile($username: String!) { allQuestionsCount { difficulty count } matchedUser(username: $username) { contributions { points } profile { reputation ranking } submissionCalendar submitStats { acSubmissionNum { difficulty count submissions } totalSubmissionNum { difficulty count submissions } } } } \",\"variables\":{\"username\":\"%s\"}}", username);
RequestBody body = RequestBody.create(mediaType, query);
Request request = new Request.Builder()
.url("https://leetcode.com/graphql/")
Expand All @@ -44,15 +46,15 @@ public StatsResponse getStats(String username) {

// User not found
if (jsonObject.has("errors")) {
return new StatsResponse("error", "user does not exist", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
return StatsResponse.error("error", "user does not exist");
} else { // Parse user info
return decodeGraphqlJson(jsonObject);
}
} else {
return new StatsResponse("error", jsonObject.getString("error"), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
return StatsResponse.error("error", jsonObject.getString("error"));
}
} catch (IOException | JSONException ex) {
return new StatsResponse("error", ex.getMessage(), 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
return StatsResponse.error("error", ex.getMessage());
}

}
Expand All @@ -71,6 +73,8 @@ private StatsResponse decodeGraphqlJson(JSONObject json) {
int contributionPoints = 0;
int reputation = 0;

final Map<String, Integer> submissionCalendar = new TreeMap<>();

try {
JSONObject data = json.getJSONObject("data");
JSONArray allQuestions = data.getJSONArray("allQuestionsCount");
Expand Down Expand Up @@ -102,11 +106,17 @@ private StatsResponse decodeGraphqlJson(JSONObject json) {
reputation = matchedUser.getJSONObject("profile").getInt("reputation");
ranking = matchedUser.getJSONObject("profile").getInt("ranking");

final JSONObject submissionCalendarJson = new JSONObject(matchedUser.getString("submissionCalendar"));

for (String timeKey: submissionCalendarJson.keySet()) {
submissionCalendar.put(timeKey, submissionCalendarJson.getInt(timeKey));
}

} catch (JSONException ex) {
return new StatsResponse("error", ex.getMessage(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
return StatsResponse.error("error", ex.getMessage());
}

return new StatsResponse("success", "retrieved", totalSolved, totalQuestions, easySolved, totalEasy, mediumSolved, totalMedium, hardSolved, totalHard, acceptanceRate, ranking, contributionPoints, reputation);
return new StatsResponse("success", "retrieved", totalSolved, totalQuestions, easySolved, totalEasy, mediumSolved, totalMedium, hardSolved, totalHard, acceptanceRate, ranking, contributionPoints, reputation, submissionCalendar);
}

private float round(float d, int decimalPlace) {
Expand Down
18 changes: 16 additions & 2 deletions src/test/java/leetcode/api/StatsResponseTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.Map;

@SpringBootTest
public class StatsResponseTests {
StatsResponse s = new StatsResponse("success", "retrieved", 1, 2, 3, 4, 5, 6, 7, 8, (float) 99.99, 10, 11, 12);
final Map<String, Integer> submissionCalendar = new HashMap<>();

{
submissionCalendar.put("1610755200", 2);
}

StatsResponse s = new StatsResponse("success", "retrieved", 1, 2, 3, 4, 5, 6, 7, 8, (float) 99.99, 10, 11, 12, submissionCalendar);

@Test
void statusCorrect() {
Expand Down Expand Up @@ -85,9 +94,14 @@ void sameRefEqualCorrect() {
assertEquals(s, s);
}

@Test
void submissionCalendarCorrect() {
assertEquals(submissionCalendar, s.getSubmissionCalendar());
}

@Test
void sameValEqualCorrect() {
StatsResponse copy = new StatsResponse("success", "retrieved", 1, 2, 3, 4, 5, 6, 7, 8, (float) 99.99, 10, 11, 12);
StatsResponse copy = new StatsResponse("success", "retrieved", 1, 2, 3, 4, 5, 6, 7, 8, (float) 99.99, 10, 11, 12, submissionCalendar);
assertTrue(s.equals(copy));
}
}
15 changes: 10 additions & 5 deletions src/test/java/leetcode/api/UserControllerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;

import java.util.HashMap;
import java.util.Map;

@WebMvcTest(UserController.class)
public class UserControllerTests {
@Autowired
Expand All @@ -26,31 +29,33 @@ public class UserControllerTests {

@Test
void validUsername() throws Exception {
StatsResponse mockResponse = new StatsResponse("success", "retrieved", 1, 2,3, 4, 5, 6, 7, 8, (float) 99.99, 10, 11, 12);
final Map<String, Integer> submissionCalendar = new HashMap<>();
submissionCalendar.put("1610755200", 2);
StatsResponse mockResponse = new StatsResponse("success", "retrieved", 1, 2,3, 4, 5, 6, 7, 8, (float) 99.99, 10, 11, 12, submissionCalendar);
when(service.getStats("user_exists")).thenReturn(mockResponse);

MvcResult result = mockMvc.perform(get("/user_exists")).andExpect(status().isOk()).andReturn();
String resultStr = result.getResponse().getContentAsString();
String expected = "{\"status\":\"success\",\"message\":\"retrieved\",\"totalSolved\":1,\"totalQuestions\":2,\"easySolved\":3,\"totalEasy\":4,\"mediumSolved\":5,\"totalMedium\":6,\"hardSolved\":7,\"totalHard\":8,\"acceptanceRate\":99.99,\"ranking\":10,\"contributionPoints\":11,\"reputation\":12}";
String expected = "{\"status\":\"success\",\"message\":\"retrieved\",\"totalSolved\":1,\"totalQuestions\":2,\"easySolved\":3,\"totalEasy\":4,\"mediumSolved\":5,\"totalMedium\":6,\"hardSolved\":7,\"totalHard\":8,\"acceptanceRate\":99.99,\"ranking\":10,\"contributionPoints\":11,\"reputation\":12,\"submissionCalendar\":{\"1610755200\":2}}";
assertEquals(resultStr, expected);
}

@Test
void noUsername() throws Exception {
MvcResult result = mockMvc.perform(get("/")).andExpect(status().isOk()).andReturn();
String resultStr = result.getResponse().getContentAsString();
String expected = "{\"status\":\"error\",\"message\":\"please enter your username (ex: leetcode-stats-api.herokuapp.com/LeetCodeUsername)\",\"totalSolved\":0,\"totalQuestions\":0,\"easySolved\":0,\"totalEasy\":0,\"mediumSolved\":0,\"totalMedium\":0,\"hardSolved\":0,\"totalHard\":0,\"acceptanceRate\":0.0,\"ranking\":0,\"contributionPoints\":0,\"reputation\":0}";
String expected = "{\"status\":\"error\",\"message\":\"please enter your username (ex: leetcode-stats-api.herokuapp.com/LeetCodeUsername)\",\"totalSolved\":0,\"totalQuestions\":0,\"easySolved\":0,\"totalEasy\":0,\"mediumSolved\":0,\"totalMedium\":0,\"hardSolved\":0,\"totalHard\":0,\"acceptanceRate\":0.0,\"ranking\":0,\"contributionPoints\":0,\"reputation\":0,\"submissionCalendar\":{}}";
assertEquals(resultStr, expected);
}

@Test
void nonValidUsername() throws Exception {
StatsResponse mockResponse = new StatsResponse("error", "user does not exist", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
StatsResponse mockResponse = StatsResponse.error("error", "user does not exist");
when(service.getStats("user_does_not_exist")).thenReturn(mockResponse);

MvcResult result = mockMvc.perform(get("/user_does_not_exist")).andExpect(status().isOk()).andReturn();
String resultStr = result.getResponse().getContentAsString();
String expected = "{\"status\":\"error\",\"message\":\"user does not exist\",\"totalSolved\":0,\"totalQuestions\":0,\"easySolved\":0,\"totalEasy\":0,\"mediumSolved\":0,\"totalMedium\":0,\"hardSolved\":0,\"totalHard\":0,\"acceptanceRate\":0.0,\"ranking\":0,\"contributionPoints\":0,\"reputation\":0}";
String expected = "{\"status\":\"error\",\"message\":\"user does not exist\",\"totalSolved\":0,\"totalQuestions\":0,\"easySolved\":0,\"totalEasy\":0,\"mediumSolved\":0,\"totalMedium\":0,\"hardSolved\":0,\"totalHard\":0,\"acceptanceRate\":0.0,\"ranking\":0,\"contributionPoints\":0,\"reputation\":0,\"submissionCalendar\":{}}";
assertEquals(resultStr, expected);
}
}