diff --git a/backend/package.json b/backend/package.json index fbd39344..f6effa59 100644 --- a/backend/package.json +++ b/backend/package.json @@ -10,7 +10,7 @@ "format": "prettier --write \\\"src/**/*.ts\\\" \\\"__test__/**/*.ts\\\"", "start": "NODE_ENV=development PORT=5001 nest start", "typeorm": "typeorm-ts-node-commonjs", - "start:debug": "NODE_ENV=development nest start --debug --watch", + "start:debug": "NODE_ENV=development PORT=5001 nest start --debug --watch", "start:prod": "node dist/main", "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", "test": "jest", diff --git a/backend/src/snippets/snippets.controller.ts b/backend/src/snippets/snippets.controller.ts index 6a4c12dd..e1cf8e97 100644 --- a/backend/src/snippets/snippets.controller.ts +++ b/backend/src/snippets/snippets.controller.ts @@ -33,6 +33,14 @@ export class SnippetsController { return this.snippetsService.findAll(); } + @Get(':login/:slug') + async findOneByLoginSlug( + @Param('login') login: string, + @Param('slug') slug: string, + ): Promise { + return this.snippetsService.findByLoginSlug(login, slug); + } + @Get(':id') async findOne(@Param('id', new ParseIntPipe()) id: number): Promise { return this.snippetsService.findOne(id); diff --git a/backend/src/snippets/snippets.service.ts b/backend/src/snippets/snippets.service.ts index 257d36dc..805b14d5 100644 --- a/backend/src/snippets/snippets.service.ts +++ b/backend/src/snippets/snippets.service.ts @@ -26,13 +26,24 @@ export class SnippetsService { return this.snippetsRepository.findOneBy({ id }); } + async findByLoginSlug(login: string, slug: string): Promise { + const user = await this.usersRepository.findOneBy({ login }); + const snippet = await this.snippetsRepository.findOne({ + where: { + user: { + id: user.id, + }, + slug, + }, + }); + return snippet; + } + async getSlug(name: string, login: string, id: number): Promise { const trimmedName = name.trim(); const extension = path.extname(trimmedName); const basename = path.basename(trimmedName, extension); - const slug = `${basename - .replace(/\s/g, '-') - .toLowerCase()}_${extension.slice(1)}`; + const slug = `${basename.replace(/\s/g, '-').toLowerCase()}`; const snippets = await this.snippetManager .createQueryBuilder(Snippets, 'snippet') .where('snippet.userId= :id', { id }) diff --git a/backend/src/snippets/validation/validation.pipe.ts b/backend/src/snippets/validation/validation.pipe.ts index bf59b1dc..d320a64a 100644 --- a/backend/src/snippets/validation/validation.pipe.ts +++ b/backend/src/snippets/validation/validation.pipe.ts @@ -18,7 +18,7 @@ export class ValidationPipe implements PipeTransform { const object = plainToInstance(metatype, value); const errors = await validate(object); if (errors.length > 0) { - throw new BadRequestException('Validation failed'); + throw new BadRequestException('Validation failed', errors.join(' ')); } return value; } diff --git a/backend/src/users/validation/validation.pipe.ts b/backend/src/users/validation/validation.pipe.ts index bf59b1dc..d320a64a 100644 --- a/backend/src/users/validation/validation.pipe.ts +++ b/backend/src/users/validation/validation.pipe.ts @@ -18,7 +18,7 @@ export class ValidationPipe implements PipeTransform { const object = plainToInstance(metatype, value); const errors = await validate(object); if (errors.length > 0) { - throw new BadRequestException('Validation failed'); + throw new BadRequestException('Validation failed', errors.join(' ')); } return value; } diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index 00006b0b..e82ccd13 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -1,9 +1,10 @@ import { useTranslation } from 'react-i18next'; import React, { useEffect } from 'react'; import { useDispatch, useSelector } from 'react-redux'; -import { useLoaderData } from 'react-router'; +// import { useLoaderData } from 'react-router'; +import { useParams } from 'react-router-dom'; import { MonacoEditor } from './components/Editor/index.jsx'; -import { Button } from './components/Button/index.jsx'; +import { SnippetButton } from './components/SnippetButton/index.jsx'; import { Terminal } from './components/Terminal/index.jsx'; import { actions } from './slices/index.js'; import { useSnippets } from './hooks'; @@ -11,13 +12,17 @@ import { useSnippets } from './hooks'; export function App() { const dispatch = useDispatch(); const snippetApi = useSnippets(); - const loaderData = useLoaderData(); + const params = useParams(); useEffect(() => { const loadSnippet = async () => { - if (snippetApi.hasViewSnippetParams(loaderData)) { + const snippetParams = { + login: params.login, + slug: params.slug, + }; + if (snippetApi.hasViewSnippetParams(snippetParams)) { const snippetData = await snippetApi.getSnippetDataByViewParams( - loaderData, + snippetParams, ); dispatch(actions.updateCode(snippetData.code)); } @@ -32,7 +37,7 @@ export function App() {
-