Skip to content
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

Feature: Challenge list endpoint #8

Merged
merged 1 commit into from
Oct 3, 2024
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
10 changes: 9 additions & 1 deletion src/api/userChallenge/challenge.crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ import { CreateChallengeDBPayload, FindByParams } from './validation.schema';
import { UserChallenge } from '~/database/models/UserChallenge';

export class UserChallengeCrud {
static findByParams(params: FindByParams) {
static findManyByUserId(userId: string) {
return UserChallenge.findAll({
where: {
userId,
},
});
}

static findOneByParams(params: FindByParams) {
return UserChallenge.findOne({
where: {
id: params.id,
Expand Down
31 changes: 30 additions & 1 deletion src/api/userChallenge/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,41 @@

route.use(authMiddleware);

route.get('/', async (req: Request, res: Response) => {
const user = (req as AuthorizedRequest).user;

try {
const dbresult = await UserChallengeCrud.findManyByUserId(user.id);

return res.status(200).json({
type: 'CHALLENGE_LIST_FETCHED',
statusCode: 200,
message: 'Challenge list fetched successfully',
isSuccess: true,
details: {
challenges: dbresult,
},
});
} catch (error: any) {

Check warning on line 33 in src/api/userChallenge/controller.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
if (error?.errors?.[0] instanceof ValidationErrorItem) {
return res.status(400).json({
isError: true,
type: 'VALIDATION_ERROR',
message: error?.errors?.[0].message,
details: error?.errors?.[0],
});
}

return res.status(500).json({ isError: true });
}
});

route.get('/:challengeId', async (req: Request, res: Response) => {
const user = (req as AuthorizedRequest).user;
const { challengeId } = req.params;

try {
const dbresult = await UserChallengeCrud.findByParams({
const dbresult = await UserChallengeCrud.findOneByParams({
id: challengeId,
userId: user.id,
});
Expand All @@ -44,7 +73,7 @@
challenge: entity,
},
});
} catch (error: any) {

Check warning on line 76 in src/api/userChallenge/controller.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
if (error?.errors?.[0] instanceof ValidationErrorItem) {
return res.status(400).json({
isError: true,
Expand Down Expand Up @@ -84,7 +113,7 @@
challenge: createdEntity,
},
});
} catch (error: any) {

Check warning on line 116 in src/api/userChallenge/controller.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
if (error?.errors?.[0] instanceof ValidationErrorItem) {
return res.status(400).json({
isError: true,
Expand Down Expand Up @@ -119,7 +148,7 @@
challenge: createdEntity,
},
});
} catch (error: any) {

Check warning on line 151 in src/api/userChallenge/controller.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
if (error?.errors?.[0] instanceof ValidationErrorItem) {
return res.status(400).json({
isError: true,
Expand Down Expand Up @@ -150,7 +179,7 @@
challengeProgress: dbresult,
},
});
} catch (error: any) {

Check warning on line 182 in src/api/userChallenge/controller.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
if (error?.errors?.[0] instanceof ValidationErrorItem) {
return res.status(400).json({
isError: true,
Expand Down
Loading