-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a945630
commit 70aa277
Showing
27 changed files
with
747 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,75 @@ | ||
import { Controller, Request, Get, UseGuards, Param } from '@nestjs/common'; | ||
import { | ||
Controller, | ||
Request, | ||
Get, | ||
UseGuards, | ||
Param, | ||
Patch, | ||
Body, | ||
} from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
import { UsersService } from './users.service'; | ||
import { User } from './user.entity'; | ||
import { UpdateUser, User } from './user.entity'; | ||
import { | ||
ApiBearerAuth, | ||
ApiBody, | ||
ApiParam, | ||
ApiParamOptions, | ||
} from '@nestjs/swagger'; | ||
|
||
const usernameParam: ApiParamOptions = { | ||
name: 'username', | ||
required: true, | ||
type: 'string', | ||
description: 'Username', | ||
}; | ||
|
||
interface GetByUsernameParam { | ||
username: string; | ||
} | ||
|
||
@Controller() | ||
export class UserController { | ||
constructor(private userService: UsersService) {} | ||
|
||
@Get('profile') | ||
@UseGuards(AuthGuard('jwt')) | ||
@ApiBearerAuth() | ||
getProfile(@Request() req): Promise<User> { | ||
return this.userService.findOneByJwt(req.user); | ||
} | ||
|
||
@Get('user/:username') | ||
getOneUser(@Param() params: any): Promise<User> { | ||
@ApiParam(usernameParam) | ||
getOneUser(@Param() params: GetByUsernameParam): Promise<User> { | ||
return this.userService.findOneBy({ username: params.username }); | ||
} | ||
|
||
@Patch('user/:username') | ||
@UseGuards(AuthGuard('jwt')) | ||
@ApiBody({ | ||
required: true, | ||
type: UpdateUser, | ||
description: 'Updated user parameters', | ||
}) | ||
@ApiParam(usernameParam) | ||
@ApiBearerAuth() | ||
updateUser( | ||
@Request() req, | ||
@Param() params: GetByUsernameParam, | ||
@Body() updatedUser: UpdateUser, | ||
): Promise<void> { | ||
const fromJwt = User.fromJwt(req.user); | ||
const isSameUser = fromJwt.username === params.username; | ||
const isAdmin = fromJwt.admin; | ||
|
||
if (isAdmin || isSameUser) { | ||
return this.userService.updateUser( | ||
{ username: params.username }, | ||
updatedUser, | ||
); | ||
} | ||
|
||
throw new Error('Not allowed to update this user'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface GetByIdParameter { | ||
id: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Label, TextInput } from 'flowbite-react'; | ||
|
||
interface TextInputWithLabelProps { | ||
id: string; | ||
name: string; | ||
placeholder?: string; | ||
required?: boolean; | ||
disabled?: boolean; | ||
value?: string; | ||
helperText?: React.ReactNode; | ||
} | ||
|
||
export function TextInputWithLabel({ | ||
id, | ||
name, | ||
placeholder, | ||
required, | ||
disabled, | ||
value, | ||
helperText, | ||
}: TextInputWithLabelProps) { | ||
return ( | ||
<div> | ||
<div className="mb-2 block"> | ||
<Label htmlFor={id} value={name} /> | ||
</div> | ||
<TextInput | ||
id={id} | ||
required={required} | ||
placeholder={placeholder} | ||
disabled={disabled} | ||
value={value} | ||
helperText={helperText} | ||
/> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.