Skip to content

Commit

Permalink
chore(snippet): create the column to save the highlighted snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
tericcabrel committed Nov 1, 2023
1 parent c47db3c commit d0ad6c9
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/database/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"lint": "eslint src index.ts",
"test": "jest",
"db:dev": "pscale connect sharingan dev --port 3311",
"db:shadow": "docker run -d --rm -e MYSQL_ROOT_PASSWORD=secret -e MYSQL_DATABASE=sharingan --name sharingan-shadow-db -p 3312:3306 mysql:8.0",
"db:shadow": "docker run -d --rm -e MYSQL_ROOT_PASSWORD=secret -e MYSQL_DATABASE=sharingan --name sharingan-shadow-db -p 3312:3306 mysql:8.0.23",
"db:shadow:stop": "docker kill sharingan-shadow-db && docker container prune -f",
"db:deploy:create": "pscale deploy-request create sharingan dev",
"db:generate": "prisma generate --schema=./prisma/schema.prisma",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE `snippets` ADD COLUMN `content_html` TEXT NULL;
1 change: 1 addition & 0 deletions packages/database/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ model Snippet {
folderId String @map("folder_id")
name String @db.VarChar(255)
content String @db.Text
contentHtml String? @db.Text @map("content_html")
language String @db.VarChar(20)
size Int @default(0) @db.Int
visibility SnippetVisibility @default(public)
Expand Down
1 change: 1 addition & 0 deletions packages/database/prisma/schema.test.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ model Snippet {
folderId String @map("folder_id")
name String @db.VarChar(255)
content String @db.Text
contentHtml String? @db.Text @map("content_html")
language String @db.VarChar(20)
size Int @default(0) @db.Int
visibility SnippetVisibility @default(public)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ describe('Test Create Snippet DTO', () => {
// GIVEN
const dto = new CreateSessionDto({
content: 'import React from "react";\n\nexport const App = () => {\n\n\treturn(\n\t\t<div>Hello</div>\n\t);\n};',
contentHighlighted:
'import React from "react";\n\nexport const App = () => {\n\n\treturn(\n\t\t<div>Hello</div>\n\t);\n}; highlighted',
description: 'Basic react component',
folderId,
language: 'tsx',
Expand All @@ -27,6 +29,8 @@ describe('Test Create Snippet DTO', () => {
// THEN
expect(folder).toMatchObject<Snippet>({
content: 'import React from "react";\n\nexport const App = () => {\n\n\treturn(\n\t\t<div>Hello</div>\n\t);\n};',
contentHtml:
'import React from "react";\n\nexport const App = () => {\n\n\treturn(\n\t\t<div>Hello</div>\n\t);\n}; highlighted',
createdAt: expect.any(Date),
description: 'Basic react component',
folderId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ describe('Test Update Snippet DTO', () => {
const dto = new UpdateSnippetDto({
content:
'import React from "react";\n\nexport const App = () => {\n\n\treturn(\n\t\t<div>Hello Updated</div>\n\t);\n};',
contentHighlighted:
'import React from "react";\n\nexport const App = () => {\n\n\treturn(\n\t\t<div>Hello Updated</div>\n\t);\n}; highlighted',
creatorId: userId,
description: 'Basic react component updated',
language: 'tsx',
Expand All @@ -34,6 +36,8 @@ describe('Test Update Snippet DTO', () => {
const expectedSnippet: Snippet = {
content:
'import React from "react";\n\nexport const App = () => {\n\n\treturn(\n\t\t<div>Hello Updated</div>\n\t);\n};',
contentHtml:
'import React from "react";\n\nexport const App = () => {\n\n\treturn(\n\t\t<div>Hello Updated</div>\n\t);\n}; highlighted',
createdAt: currentSnippet.createdAt,
description: 'Basic react component updated',
folderId: snippetToUpdate.folderId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('Test Snippet service', () => {
// THEN
expect(expectedSnippet).toMatchObject<Snippet>({
content: createSnippetDto.toSnippet().content,
contentHtml: createSnippetDto.toSnippet().contentHtml,
createdAt: expect.any(Date),
description: createSnippetDto.toSnippet().description,
folderId: rootFolder.id,
Expand Down Expand Up @@ -64,7 +65,7 @@ describe('Test Snippet service', () => {
await deleteTestUsersById([user.id]);
});

it('should find all public snippets', async () => {
it('should retrieve all public snippets', async () => {
// GIVEN
const [user, rootFolder] = await createUserWithRootFolder();
const existingSnippets = await Promise.all([
Expand All @@ -77,10 +78,37 @@ describe('Test Snippet service', () => {
]);

// WHEN
const publicSnippets = await snippetService.findPublicSnippet();
const publicSnippets = await snippetService.findPublicSnippet({ itemPerPage: 10 });

// THEN
await expect(publicSnippets).toHaveLength(4);
await expect(publicSnippets.hasMore).toEqual(false);
await expect(publicSnippets.nextCursor).toEqual(null);
await expect(publicSnippets.items).toHaveLength(4);

await deleteTestSnippetsById(existingSnippets.map((snippet) => snippet.id));
await deleteTestFoldersById([rootFolder.id]);
await deleteTestUsersById([user.id]);
});

it('should retrieve a subset of public snippets', async () => {
// GIVEN
const [user, rootFolder] = await createUserWithRootFolder();
const existingSnippets = await Promise.all([
createTestSnippet({ folderId: rootFolder.id, userId: user.id, visibility: 'public' }),
createTestSnippet({ folderId: rootFolder.id, userId: user.id, visibility: 'private' }),
createTestSnippet({ folderId: rootFolder.id, userId: user.id, visibility: 'public' }),
createTestSnippet({ folderId: rootFolder.id, userId: user.id, visibility: 'private' }),
createTestSnippet({ folderId: rootFolder.id, userId: user.id, visibility: 'public' }),
createTestSnippet({ folderId: rootFolder.id, userId: user.id, visibility: 'public' }),
]);

// WHEN
const publicSnippets = await snippetService.findPublicSnippet({ itemPerPage: 3 });

// THEN
await expect(publicSnippets.hasMore).toEqual(true);
await expect(publicSnippets.nextCursor).toEqual(expect.any(String));
await expect(publicSnippets.items).toHaveLength(3);

await deleteTestSnippetsById(existingSnippets.map((snippet) => snippet.id));
await deleteTestFoldersById([rootFolder.id]);
Expand Down
5 changes: 4 additions & 1 deletion packages/domain/__tests__/setup/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,11 @@ export const createTestSnippetDto = (
const languageIndex = randNumber({ max: languages.length - 1, min: 0 });
const themeIndex = randNumber({ max: themes.length - 1, min: 0 });

const snippetContent = randWord({ length: randNumber({ max: 30, min: 5 }) }).join('\n');

return new CreateSnippetDto({
content: randWord({ length: randNumber({ max: 30, min: 5 }) }).join('\n'),
content: snippetContent,
contentHighlighted: `${snippetContent} highlighted`,
description: randWord({ length: randNumber({ max: 20, min: 10 }) }).join(' '),
folderId: args?.folderId ?? generateTestId(),
language: languages[languageIndex],
Expand Down
4 changes: 2 additions & 2 deletions packages/domain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"build": "tsc --project tsconfig.prod.json",
"clean": "rm -rf .turbo dist",
"lint": "eslint --fix",
"db:test": "docker run -d --rm -e MYSQL_ROOT_PASSWORD=secret -e MYSQL_DATABASE=sharingan --name sharingandb -p 3313:3306 mysql:8.0",
"db:test:stop": "docker container kill sharingandb",
"db:test": "docker run -d --rm -e MYSQL_ROOT_PASSWORD=secret -e MYSQL_DATABASE=sharingan --name sharingandb -p 3313:3306 mysql:8.0.23",
"db:test:stop": "docker container kill sharingandb && docker container prune -f",
"db:test:migration": "npx prisma migrate dev --schema=../database/prisma/schema.test.prisma",
"test": "TEST_WITH_DB=true IS_LOCAL=false jest",
"test:unit": "jest",
Expand Down
2 changes: 2 additions & 0 deletions packages/domain/src/snippets/dtos/create-snippet-dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Snippet, SnippetVisibility, dbId } from '@sharingan/database';

type Input = {
content: string;
contentHighlighted: string;
description: string | null;
folderId: string;
language: string;
Expand Down Expand Up @@ -30,6 +31,7 @@ export default class CreateSnippetDto {
toSnippet(): Snippet {
return {
content: this._input.content,
contentHtml: this._input.contentHighlighted,
createdAt: new Date(),
description: this._input.description,
folderId: this._input.folderId,
Expand Down
2 changes: 2 additions & 0 deletions packages/domain/src/snippets/dtos/update-snippet-dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Snippet, SnippetVisibility } from '@sharingan/database';

type Input = {
content: string;
contentHighlighted: string;
creatorId: string;
description: string | null;
language: string;
Expand Down Expand Up @@ -31,6 +32,7 @@ export default class UpdateSnippetDto {
return {
...currentSnippet,
content: this._input.content,
contentHtml: this._input.contentHighlighted,
description: this._input.description,
language: this._input.language,
lineHighlight: this._input.lineHighlight,
Expand Down
1 change: 1 addition & 0 deletions packages/domain/src/snippets/snippet.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default class SnippetService {
return dbClient.snippet.create({
data: {
content: input.content,
contentHtml: input.contentHtml,
description: input.description,
folderId: input.folderId,
id: input.id,
Expand Down

0 comments on commit d0ad6c9

Please sign in to comment.