Skip to content

Commit

Permalink
refactor: api routes (#142)
Browse files Browse the repository at this point in the history
* refactor: api routes

* fix: created_at timestamp for shopping_list_item table

* fix: sort shopping list by created_at

* fix: missing await for removing task group
  • Loading branch information
invertedEcho authored Dec 6, 2024
1 parent fbd4758 commit 96aac1a
Show file tree
Hide file tree
Showing 16 changed files with 631 additions and 33 deletions.
6 changes: 3 additions & 3 deletions backend/src/assignment/assignment.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Controller, Get, Param, Patch, Query } from '@nestjs/common';
import {
dbChangeAssignmentState,
dbUpdateAssignmentState,
dbGetAssignmentsForUserGroupFromCurrentInterval,
} from 'src/db/functions/assignment';
import { AssignmentState } from 'src/db/schema';
Expand All @@ -16,10 +16,10 @@ export class AssignmentController {
}

@Patch('/:id/:state')
async changeAssignmentState(
async updateAssignmentState(
@Param('id') id: number,
@Param('state') state: AssignmentState,
) {
await dbChangeAssignmentState(id, state);
await dbUpdateAssignmentState(id, state);
}
}
2 changes: 2 additions & 0 deletions backend/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import { dbGetUserById } from 'src/db/functions/user';
import { AuthGuard } from './auth.guard';
import { Public } from './constants';

// TODO: why exactly are classes needed here? seems like we want a type instead.

class RegisterDto {
username: string;
email: string;
Expand Down
7 changes: 3 additions & 4 deletions backend/src/auth/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ export class AuthGuard implements CanActivate {
private reflector: Reflector,
) {}

// If this function returns true, the jwt token (if existing) in the incoming request is valid,
// and the request gets added a 'user' field, which contains the decoded data from the jwt secret.
// It works by checking if the request contains an authorization header with a Bearer token,
// and validates it.
// The function returns true if the auth header in the incoming request is valid.
// The request gets added a 'user' field, which contains the decoded data from the jwt secret.
async canActivate(context: ExecutionContext): Promise<boolean> {
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
context.getHandler(),
Expand All @@ -30,6 +28,7 @@ export class AuthGuard implements CanActivate {
if (isPublic) {
return true;
}

const request = context.switchToHttp().getRequest();
const token = extractTokenFromAuthHeader(request.headers.authorization);

Expand Down
2 changes: 1 addition & 1 deletion backend/src/db/functions/assignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export async function dbGetAssignmentsForUserGroupFromCurrentInterval(
});
}

export async function dbChangeAssignmentState(
export async function dbUpdateAssignmentState(
assignmentId: number,
state: AssignmentState,
) {
Expand Down
1 change: 0 additions & 1 deletion backend/src/db/functions/user-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export async function dbGetUserGroupOfUser(userId: number) {
eq(userGroupTable.id, userUserGroupTable.groupId),
)
.limit(1);
if (userGroups.length === 0) return undefined;
return userGroups[0];
}

Expand Down
1 change: 1 addition & 0 deletions backend/src/db/migrations/0010_nifty_dexter_bennett.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "shopping_list_item" ADD COLUMN "created_at" timestamp DEFAULT now() NOT NULL;
Loading

0 comments on commit 96aac1a

Please sign in to comment.