Skip to content
Open
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
12 changes: 12 additions & 0 deletions backend/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ export class AuthService {
) {}

async signUp(signUpDto: SignUpDto): Promise<any> {
const existingUser = await this.userService.findByEmail(signUpDto.email);
if (existingUser) {
throw new ForbiddenException('Email is already registered');
}

const existingUsername = await this.userService.findByUser(
signUpDto.username,
);
if (existingUsername) {
throw new ForbiddenException('Username is already taken');
}

const createdHashedPassword = await bcrypt.hash(signUpDto.pass, 10);

const user = await this.userService.create({
Expand Down
18 changes: 1 addition & 17 deletions backend/src/auth/dto/sign-up.dto.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
IsEmail,
IsNotEmpty,
IsString,
IsStrongPassword,
MaxLength,
} from 'class-validator';
import { IsEmail, IsNotEmpty, IsString, MaxLength } from 'class-validator';

export class SignUpDto {
@IsString()
Expand All @@ -17,16 +11,6 @@ export class SignUpDto {

@IsString()
@IsNotEmpty()
@IsStrongPassword(
{
minLength: 8,
minLowercase: 1,
minNumbers: 1,
minSymbols: 1,
minUppercase: 1,
},
{ message: 'Password is too weak' },
)
@MaxLength(20)
pass: string;
}
4 changes: 2 additions & 2 deletions backend/src/post/post.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import { CreatePostDto } from './dto/create-post.dto';
import { UpdatePostDto } from './dto/update-post.dto';
import { UUIDDto } from './dto/uuid.dto';
import { Post } from './post.entity';
import { PostService } from './post.service';
import IPost, { PostService } from './post.service';

@Controller('posts')
export class PostController {
constructor(private readonly postService: PostService) {}

@Get()
async findAll(): Promise<Post[]> {
async findAll(): Promise<IPost[]> {
return this.postService.findAll();
}

Expand Down
32 changes: 26 additions & 6 deletions backend/src/post/post.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ import { UpdatePostDto } from './dto/update-post.dto';
import { UUIDDto } from './dto/uuid.dto';
import { Post } from './post.entity';

export default interface IPost {
id: string;
content: string;
createdAt: string;
updatedAt: string;
userId: string;
user: string;
}
@Injectable()
export class PostService {
private readonly logger = new Logger('Post Service');
Expand All @@ -17,13 +25,25 @@ export class PostService {
private userService: UserService,
) {}

async findAll(): Promise<Post[]> {
return this.postRepository.find({
order: {
createdAt: 'DESC',
},
});
async findAll(): Promise<IPost[]> {
return this.postRepository
.createQueryBuilder('post')
.leftJoinAndSelect('post.user', 'user') // Join the user entity
.addSelect('user.name') // Only select the user's name
.orderBy('post.createdAt', 'DESC') // Order posts by createdAt
.getMany()
.then((posts) =>
posts.map((post) => ({
id: post.id,
content: post.content,
createdAt: post.createdAt,
updatedAt: post.updatedAt,
userId: post.user.id,
user: post.user.name,
})),
);
}

async findOne(UUIDDto: UUIDDto): Promise<Post> {
return this.postRepository.findOne({ where: { id: UUIDDto.id } });
}
Expand Down
10 changes: 10 additions & 0 deletions backend/src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Param,
Post,
Put,
Query,
} from '@nestjs/common';
import { CreateUserDto } from './dto/create-user.dto';
import { FollowUserDto } from './dto/follow-user-dto';
Expand All @@ -24,6 +25,15 @@ export class UserController {
return this.userService.findAll();
}

@Get(':userId')
async findUserById(
@Param('userId') userId: string,
@Query('select') select?: string,
): Promise<User> {
const selectedFields = select?.split(',') as (keyof User)[] | undefined;
return this.userService.findById(userId, { select: selectedFields });
}

@Get('followers')
async getFollowers(@Body() getFollowersDto: GetFollowersDto) {
const { userId } = getFollowersDto;
Expand Down
8 changes: 5 additions & 3 deletions backend/src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ export class UserService {
return this.userRepository.save(user);
}

async findById(id: string): Promise<User> {
async findById(
id: string,
options?: { select?: (keyof User)[] },
): Promise<User> {
return this.userRepository.findOne({
where: { id },
relations: ['posts', 'followers', 'following'],
select: options?.select,
});
}

async findByUser(name: string): Promise<User> {
return this.userRepository.findOne({
where: { name },
Expand Down
24 changes: 24 additions & 0 deletions frontend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
50 changes: 50 additions & 0 deletions frontend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# React + TypeScript + Vite

This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.

Currently, two official plugins are available:

- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh

## Expanding the ESLint configuration

If you are developing a production application, we recommend updating the configuration to enable type aware lint rules:

- Configure the top-level `parserOptions` property like this:

```js
export default tseslint.config({
languageOptions: {
// other options...
parserOptions: {
project: ['./tsconfig.node.json', './tsconfig.app.json'],
tsconfigRootDir: import.meta.dirname,
},
},
})
```

- Replace `tseslint.configs.recommended` to `tseslint.configs.recommendedTypeChecked` or `tseslint.configs.strictTypeChecked`
- Optionally add `...tseslint.configs.stylisticTypeChecked`
- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and update the config:

```js
// eslint.config.js
import react from 'eslint-plugin-react'

export default tseslint.config({
// Set the react version
settings: { react: { version: '18.3' } },
plugins: {
// Add the react plugin
react,
},
rules: {
// other rules...
// Enable its recommended rules
...react.configs.recommended.rules,
...react.configs['jsx-runtime'].rules,
},
})
```
21 changes: 21 additions & 0 deletions frontend/components.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$schema": "https://ui.shadcn.com/schema.json",
"style": "new-york",
"rsc": false,
"tsx": true,
"tailwind": {
"config": "tailwind.config.js",
"css": "src/index.css",
"baseColor": "zinc",
"cssVariables": true,
"prefix": ""
},
"aliases": {
"components": "@/components",
"utils": "@/lib/utils",
"ui": "@/components/ui",
"lib": "@/lib",
"hooks": "@/hooks"
},
"iconLibrary": "lucide"
}
28 changes: 28 additions & 0 deletions frontend/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import js from '@eslint/js'
import globals from 'globals'
import reactHooks from 'eslint-plugin-react-hooks'
import reactRefresh from 'eslint-plugin-react-refresh'
import tseslint from 'typescript-eslint'

export default tseslint.config(
{ ignores: ['dist'] },
{
extends: [js.configs.recommended, ...tseslint.configs.recommended],
files: ['**/*.{ts,tsx}'],
languageOptions: {
ecmaVersion: 2020,
globals: globals.browser,
},
plugins: {
'react-hooks': reactHooks,
'react-refresh': reactRefresh,
},
rules: {
...reactHooks.configs.recommended.rules,
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
},
},
)
13 changes: 13 additions & 0 deletions frontend/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React + TS</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
Loading