Skip to content

Commit 0ce9a3f

Browse files
committed
Added user profile and recent submissions
1 parent 9f75244 commit 0ce9a3f

6 files changed

+117
-4
lines changed

graphql/user/discussions.graphql.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import gql from "graphql-tag"
33

44
export function getUserDiscussionsQuery(): DocumentNode {
55
return gql`
6-
query getUserDiscussionsQuery($username: String!, $first: Int, $skip: Int) {
6+
query getUserDiscussions($username: String!, $first: Int, $skip: Int) {
77
userCategoryTopics(username: $username, first: $first, skip: $skip) {
88
pageInfo {
99
hasNextPage

graphql/user/problems-solved-beats-stats.graphql.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import gql from "graphql-tag"
33

44
export function getUserProblemsSolvedBeatsStatsQuery(): DocumentNode {
55
return gql`
6-
query getUserProblemsSolvedBeatsStatsQuery($username: String!) {
6+
query getUserProblemsSolvedBeatsStats($username: String!) {
77
matchedUser(username: $username) {
88
problemsSolvedBeatsStats {
99
difficulty

graphql/user/recent-submissions.graphql.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import gql from "graphql-tag"
33

44
export function getUserRecentSubmissionsQuery(): DocumentNode {
55
return gql`
6-
query getUserRecentSubmissions($username: String!, $limit: Int!) {
6+
query getUserRecentSubmissions($username: String!, $limit: Int) {
77
recentAcSubmissionList(username: $username, limit: $limit) {
88
id
99
title

graphql/user/solutions.graphql.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import gql from "graphql-tag"
33

44
export function getUserSolutionsQuery(): DocumentNode {
55
return gql`
6-
query getUserSolutionsQuery($username: String!, $skip: Int, $first: Int) {
6+
query getUserSolutions($username: String!, $skip: Int, $first: Int) {
77
userSolutionTopics(username: $username, skip: $skip, first: $first) {
88
pageInfo {
99
hasNextPage

index.ts

+12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import { ContestRankingResponse } from './models/user/contest-ranking'
1515
import { DiscussionsResponse } from './models/user/discussions'
1616
import { LanguagesResponse } from './models/user/languages'
1717
import { ProblemsSolvedBeatsStatsResponse } from './models/user/problems-solved-beats-stats'
18+
import { ProfileResponse } from './models/user/profile'
19+
import { RecentSubmissionsResponse } from './models/user/recent-submissions'
1820

1921
export async function getUserBadges(req: { params: { username: string } }, res: { send: (arg0: BadgeResponse) => void }) {
2022
res.send(await UserService.getUserBadges(req.params.username))
@@ -52,6 +54,14 @@ export async function getUserProblemsSolvedBeatsStats(req: { params: { username:
5254
res.send(await UserService.getUserProblemsSolvedBeatsStats(req.params.username))
5355
}
5456

57+
export async function getUserProfile(req: { params: { username: string } }, res: { send: (arg0: ProfileResponse) => void }) {
58+
res.send(await UserService.getUserProfile(req.params.username))
59+
}
60+
61+
export async function getUserRecentSubmissions(req: { params: { username: string }, query: { limit: string } }, res: { send: (arg0: RecentSubmissionsResponse) => void }) {
62+
res.send(await UserService.getUserRecentSubmissions(req.params.username, req.query.limit))
63+
}
64+
5565
export async function getLeetQuestionsCount(req: any, res: { send: (arg0: QuestionsResponse) => void }) {
5666
let questionsResponse: QuestionsResponse
5767
const questions: AllQuestionsCount = await LeetProfileService.getAllQuestionsCount()
@@ -89,6 +99,8 @@ app.get('/user/:username/contest-ranking', getUserContestRanking)
8999
app.get('/user/:username/discussions', getUserDiscussions)
90100
app.get('/user/:username/languages', getUserLanguages)
91101
app.get('/user/:username/problems-solved-beats-stats', getUserProblemsSolvedBeatsStats)
102+
app.get('/user/:username/profile', getUserProfile)
103+
app.get('/user/:username/recent-submissions', getUserRecentSubmissions)
92104

93105
const port = 3200
94106

services/user.service.ts

+101
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ import { getUserDiscussionsQuery } from "../graphql/user/discussions.graphql"
1616
import { getUserLanguagesQuery } from "../graphql/user/languages.graphql"
1717
import { ProblemsSolvedBeatsStatsMatchedUser, ProblemsSolvedBeatsStatsResponse } from "../models/user/problems-solved-beats-stats"
1818
import { getUserProblemsSolvedBeatsStatsQuery } from "../graphql/user/problems-solved-beats-stats.graphql"
19+
import { ProfileMatchedUser, ProfileResponse } from "../models/user/profile"
20+
import { getUserProfileQuery } from "../graphql/user/profile.graphql"
21+
import { RecentSubmissionsAcList, RecentSubmissionsResponse } from "../models/user/recent-submissions"
22+
import { getUserRecentSubmissionsQuery } from "../graphql/user/recent-submissions.graphql"
1923

2024
function getInvalidUsernameErrorResponse(username: string): ErrorResponse {
2125
let errorResponse: ErrorResponse
@@ -302,4 +306,101 @@ export class UserService {
302306
return problemsSolvedBeatsStatsResponse
303307
}
304308

309+
static async getUserProfile(username: string): Promise<ProfileResponse> {
310+
const invalidUsernameResponse: ErrorResponse = getInvalidUsernameErrorResponse(username)
311+
if (invalidUsernameResponse !== null) {
312+
return {
313+
isError: true,
314+
error: invalidUsernameResponse,
315+
profile: null
316+
}
317+
}
318+
let profileResponse: ProfileResponse
319+
const response: { data: ProfileMatchedUser } = await apolloClient.query<ProfileMatchedUser>({
320+
query: getUserProfileQuery(),
321+
variables: { "username": username }
322+
}).catch(err => {
323+
profileResponse = {
324+
isError: true,
325+
error: {
326+
errorCode: 500,
327+
errorMessage: `An error occurred while retrieving user profile - ${err}. Please inform the developer.`
328+
},
329+
profile: null
330+
}
331+
return null
332+
})
333+
if (profileResponse === undefined) {
334+
profileResponse = {
335+
isError: false,
336+
error: null,
337+
profile: response.data.matchedUser.profile
338+
}
339+
}
340+
return profileResponse
341+
}
342+
343+
static async getUserRecentSubmissions(username: string, limit?: string): Promise<RecentSubmissionsResponse> {
344+
const invalidUsernameResponse: ErrorResponse = getInvalidUsernameErrorResponse(username)
345+
if (invalidUsernameResponse !== null) {
346+
return {
347+
isError: true,
348+
error: invalidUsernameResponse,
349+
recentSubmissions: null
350+
}
351+
}
352+
let recentSubmissionsResponse: RecentSubmissionsResponse
353+
let variables = {
354+
"username": username
355+
}
356+
if (limit !== undefined) {
357+
let limitValue = Number(limit)
358+
if (isNaN(limitValue)) {
359+
recentSubmissionsResponse = {
360+
isError: true,
361+
error: {
362+
errorCode: 400,
363+
errorMessage: `The provided limit value ${limit} is not a number, please provide a correct limit numeric value greater than zero`
364+
},
365+
recentSubmissions: null
366+
}
367+
return recentSubmissionsResponse
368+
}
369+
if (limitValue <= 0) {
370+
recentSubmissionsResponse = {
371+
isError: true,
372+
error: {
373+
errorCode: 400,
374+
errorMessage: `The provided limit value should be greater than zero, but you have provided ${limit}`
375+
},
376+
recentSubmissions: null
377+
}
378+
return recentSubmissionsResponse
379+
}
380+
variables["limit"] = limit
381+
}
382+
const response: { data: RecentSubmissionsAcList } = await apolloClient.query<RecentSubmissionsAcList>({
383+
query: getUserRecentSubmissionsQuery(),
384+
variables: variables
385+
}).catch(err => {
386+
recentSubmissionsResponse = {
387+
isError: true,
388+
error: {
389+
errorCode: 500,
390+
errorMessage: `An error occurred while retrieving user recent submissions - ${err}. Please inform the developer.`
391+
},
392+
recentSubmissions: null
393+
}
394+
return null
395+
})
396+
if (recentSubmissionsResponse === undefined) {
397+
recentSubmissionsResponse = {
398+
isError: false,
399+
error: null,
400+
recentSubmissions: response.data.recentAcSubmissionList
401+
}
402+
}
403+
return recentSubmissionsResponse
404+
}
405+
305406
}

0 commit comments

Comments
 (0)