Skip to content

Commit 5287805

Browse files
author
Jeremy Tsai
authored
Merge pull request JeremyTsaii#1 from volyx/main
Add `submissionCalendar` field
2 parents 24c0263 + 03a5790 commit 5287805

File tree

5 files changed

+57
-15
lines changed

5 files changed

+57
-15
lines changed

src/main/java/leetcode/api/controller/UserController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public StatsResponse getStats(@PathVariable Optional<String> username) {
2424
} else {
2525
String status = "error";
2626
String msg = "please enter your username (ex: leetcode-stats-api.herokuapp.com/LeetCodeUsername)";
27-
return new StatsResponse(status, msg, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
27+
return StatsResponse.error(status, msg);
2828
}
2929
}
3030
}

src/main/java/leetcode/api/model/StatsResponse.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package leetcode.api.model;
22

3+
import java.util.Collections;
4+
import java.util.Map;
5+
36
public class StatsResponse {
47
private final String status;
58
private final String message;
@@ -15,8 +18,9 @@ public class StatsResponse {
1518
private final int ranking;
1619
private final int contributionPoints;
1720
private final int reputation;
21+
private final Map<String, Integer> submissionCalendar;
1822

19-
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) {
23+
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) {
2024
this.status = status;
2125
this.message = message;
2226
this.totalSolved = totalSolved;
@@ -31,6 +35,11 @@ public StatsResponse(String status, String message, int totalSolved, int totalQu
3135
this.ranking = ranking;
3236
this.contributionPoints = contributionPoints;
3337
this.reputation = reputation;
38+
this.submissionCalendar = submissionCalendar;
39+
}
40+
41+
public static StatsResponse error(String status, String message) {
42+
return new StatsResponse(status, message, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Collections.emptyMap());
3443
}
3544

3645
public String getStatus() {
@@ -87,6 +96,10 @@ public int getReputation() {
8796
return reputation;
8897
}
8998

99+
public Map<String, Integer> getSubmissionCalendar() {
100+
return submissionCalendar;
101+
}
102+
90103
public boolean equals(StatsResponse s) {
91104
// Compared with itself
92105
if (s == this) {

src/main/java/leetcode/api/service/StatsServiceImpl.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.io.IOException;
66
import java.math.BigDecimal;
77
import java.math.RoundingMode;
8+
import java.util.Map;
9+
import java.util.TreeMap;
810

911
import org.json.JSONException;
1012
import org.json.JSONArray;
@@ -23,7 +25,7 @@ public StatsResponse getStats(String username) {
2325
OkHttpClient client = new OkHttpClient().newBuilder()
2426
.build();
2527
MediaType mediaType = MediaType.parse("application/json");
26-
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);
28+
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);
2729
RequestBody body = RequestBody.create(mediaType, query);
2830
Request request = new Request.Builder()
2931
.url("https://leetcode.com/graphql/")
@@ -44,15 +46,15 @@ public StatsResponse getStats(String username) {
4446

4547
// User not found
4648
if (jsonObject.has("errors")) {
47-
return new StatsResponse("error", "user does not exist", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
49+
return StatsResponse.error("error", "user does not exist");
4850
} else { // Parse user info
4951
return decodeGraphqlJson(jsonObject);
5052
}
5153
} else {
52-
return new StatsResponse("error", jsonObject.getString("error"), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
54+
return StatsResponse.error("error", jsonObject.getString("error"));
5355
}
5456
} catch (IOException | JSONException ex) {
55-
return new StatsResponse("error", ex.getMessage(), 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
57+
return StatsResponse.error("error", ex.getMessage());
5658
}
5759

5860
}
@@ -71,6 +73,8 @@ private StatsResponse decodeGraphqlJson(JSONObject json) {
7173
int contributionPoints = 0;
7274
int reputation = 0;
7375

76+
final Map<String, Integer> submissionCalendar = new TreeMap<>();
77+
7478
try {
7579
JSONObject data = json.getJSONObject("data");
7680
JSONArray allQuestions = data.getJSONArray("allQuestionsCount");
@@ -102,11 +106,17 @@ private StatsResponse decodeGraphqlJson(JSONObject json) {
102106
reputation = matchedUser.getJSONObject("profile").getInt("reputation");
103107
ranking = matchedUser.getJSONObject("profile").getInt("ranking");
104108

109+
final JSONObject submissionCalendarJson = new JSONObject(matchedUser.getString("submissionCalendar"));
110+
111+
for (String timeKey: submissionCalendarJson.keySet()) {
112+
submissionCalendar.put(timeKey, submissionCalendarJson.getInt(timeKey));
113+
}
114+
105115
} catch (JSONException ex) {
106-
return new StatsResponse("error", ex.getMessage(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
116+
return StatsResponse.error("error", ex.getMessage());
107117
}
108118

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

112122
private float round(float d, int decimalPlace) {

src/test/java/leetcode/api/StatsResponseTests.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,18 @@
66
import static org.junit.jupiter.api.Assertions.assertTrue;
77
import org.springframework.boot.test.context.SpringBootTest;
88

9+
import java.util.HashMap;
10+
import java.util.Map;
11+
912
@SpringBootTest
1013
public class StatsResponseTests {
11-
StatsResponse s = new StatsResponse("success", "retrieved", 1, 2, 3, 4, 5, 6, 7, 8, (float) 99.99, 10, 11, 12);
14+
final Map<String, Integer> submissionCalendar = new HashMap<>();
15+
16+
{
17+
submissionCalendar.put("1610755200", 2);
18+
}
19+
20+
StatsResponse s = new StatsResponse("success", "retrieved", 1, 2, 3, 4, 5, 6, 7, 8, (float) 99.99, 10, 11, 12, submissionCalendar);
1221

1322
@Test
1423
void statusCorrect() {
@@ -85,9 +94,14 @@ void sameRefEqualCorrect() {
8594
assertEquals(s, s);
8695
}
8796

97+
@Test
98+
void submissionCalendarCorrect() {
99+
assertEquals(submissionCalendar, s.getSubmissionCalendar());
100+
}
101+
88102
@Test
89103
void sameValEqualCorrect() {
90-
StatsResponse copy = new StatsResponse("success", "retrieved", 1, 2, 3, 4, 5, 6, 7, 8, (float) 99.99, 10, 11, 12);
104+
StatsResponse copy = new StatsResponse("success", "retrieved", 1, 2, 3, 4, 5, 6, 7, 8, (float) 99.99, 10, 11, 12, submissionCalendar);
91105
assertTrue(s.equals(copy));
92106
}
93107
}

src/test/java/leetcode/api/UserControllerTests.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
import org.springframework.boot.test.mock.mockito.MockBean;
1616
import org.springframework.test.web.servlet.MockMvc;
1717

18+
import java.util.HashMap;
19+
import java.util.Map;
20+
1821
@WebMvcTest(UserController.class)
1922
public class UserControllerTests {
2023
@Autowired
@@ -26,31 +29,33 @@ public class UserControllerTests {
2629

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

3237
MvcResult result = mockMvc.perform(get("/user_exists")).andExpect(status().isOk()).andReturn();
3338
String resultStr = result.getResponse().getContentAsString();
34-
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}";
39+
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}}";
3540
assertEquals(resultStr, expected);
3641
}
3742

3843
@Test
3944
void noUsername() throws Exception {
4045
MvcResult result = mockMvc.perform(get("/")).andExpect(status().isOk()).andReturn();
4146
String resultStr = result.getResponse().getContentAsString();
42-
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}";
47+
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\":{}}";
4348
assertEquals(resultStr, expected);
4449
}
4550

4651
@Test
4752
void nonValidUsername() throws Exception {
48-
StatsResponse mockResponse = new StatsResponse("error", "user does not exist", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
53+
StatsResponse mockResponse = StatsResponse.error("error", "user does not exist");
4954
when(service.getStats("user_does_not_exist")).thenReturn(mockResponse);
5055

5156
MvcResult result = mockMvc.perform(get("/user_does_not_exist")).andExpect(status().isOk()).andReturn();
5257
String resultStr = result.getResponse().getContentAsString();
53-
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}";
58+
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\":{}}";
5459
assertEquals(resultStr, expected);
5560
}
5661
}

0 commit comments

Comments
 (0)