Skip to content

Commit

Permalink
update editor api's, and generate static types for all
Browse files Browse the repository at this point in the history
  • Loading branch information
Himasnhu-AT committed Sep 30, 2024
1 parent 1280ad1 commit 5f20d40
Show file tree
Hide file tree
Showing 30 changed files with 618 additions and 51 deletions.
3 changes: 1 addition & 2 deletions apps/backend/apps/backend/src/api/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import {
} from '@nestjs/common';
import { AuthService } from './auth.service';
import { AuthGuard } from '@nestjs/passport';
import { SignUpDto } from './dto/signup.dto';
import { SignInDto } from './dto/signin.dto';
import { Public } from 'libs/custom.decorator/custom.decorator';
import RetrieveInfoFromRequest from 'libs/handlers/retriveInfoFromRequest.global';
import { SignUpDto, SignInDto } from 'libs/types';

@Controller('auth')
export class AuthController {
Expand Down
3 changes: 1 addition & 2 deletions apps/backend/apps/backend/src/api/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import { JwtService } from '@nestjs/jwt';
import Redis from 'ioredis';
import * as bcrypt from 'bcrypt';
import { randomBytes } from 'crypto';
import { SignUpDto } from './dto/signup.dto';
import { SignInDto } from './dto/signin.dto';
import sendEmail from 'libs/handlers/email.global';
import handleErrors from 'libs/handlers/handleErrors.global';
import RetrieveInfoFromRequest from 'libs/handlers/retriveInfoFromRequest.global';
import { PrismaService } from 'libs/prisma/prisma.service';
import { SignUpDto, SignInDto } from 'libs/types';

@Injectable()
export class AuthService {
Expand Down
4 changes: 1 addition & 3 deletions apps/backend/apps/backend/src/api/user/book.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ import {
} from '@nestjs/common';
import { UserService } from './book.service';
import { AuthGuard } from '@nestjs/passport';
import { BookDto } from './dto/book.dto';
import { SectionDto } from './dto/section.dto';
import { NoteDto } from './dto/note.dto';
import { Public } from 'libs/custom.decorator/custom.decorator';
import { BookDto, SectionDto, NoteDto } from 'libs/types';

@Controller('user')
export class UserController {
Expand Down
27 changes: 13 additions & 14 deletions apps/backend/apps/backend/src/api/user/book.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { Injectable } from '@nestjs/common';
import { BookDto } from './dto/book.dto';
import { SectionDto } from './dto/section.dto';
import { NoteDto } from './dto/note.dto';
import RetrieveInfoFromRequest from 'libs/handlers/retriveInfoFromRequest.global';
import { PrismaService } from 'libs/prisma/prisma.service';
import { BookDto, SectionDto, NoteDto } from 'libs/types';

@Injectable()
export class UserService {
Expand Down Expand Up @@ -90,17 +88,18 @@ export class UserService {
throw new Error('Section not found');
}

return await this.prisma.note.create({
data: {
// TODO save content
// content: note.content,
section: {
connect: {
id: sectionId,
},
},
},
});
// return await this.prisma.note.create({
// data: {
// // TODO save content
// // content: note.content,
// section: {
// connect: {
// id: sectionId,
// },
// },
// },
// });
return 'TODO';
}

async getBooks(request: any) {
Expand Down
7 changes: 0 additions & 7 deletions apps/backend/apps/backend/src/api/user/dto/note.dto.ts

This file was deleted.

9 changes: 6 additions & 3 deletions apps/backend/apps/content_mgmt/src/content_mgmt.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Module } from '@nestjs/common';
import { ContentMgmtController } from './content_mgmt.controller';
import { ContentMgmtService } from './content_mgmt.service';
import { PrismaModule } from 'libs/prisma/prisma.module';
import { EditorModule } from './editor/editor.module';
import { EditorService } from './editor/editor.service';

@Module({
imports: [],
controllers: [ContentMgmtController],
providers: [ContentMgmtService],
imports: [PrismaModule],
controllers: [ContentMgmtController, EditorModule],
providers: [ContentMgmtService, EditorService],
})
export class ContentMgmtModule {}
70 changes: 70 additions & 0 deletions apps/backend/apps/content_mgmt/src/editor/editor.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import {
Controller,
Get,
Post,
Body,
Param,
Patch,
Delete,
} from '@nestjs/common';
import { EditorService } from './editor.service';
import {
CreateBlockDto,
CreateNoteDto,
UpdateBlockDto,
UpdateNoteDto,
} from 'libs/types';

@Controller('editor')
export class EditorController {
constructor(private readonly editorService: EditorService) {}

// POST /api/documents - Create a new document
@Post('note/new')
createDocument(@Body() createDocumentDto: CreateNoteDto) {
return this.editorService.createDocument(createDocumentDto);
}

// GET /api/documents/:id - Retrieve a document
@Get('note/:id')
getDocument(@Param('id') id: string) {
return this.editorService.getDocument(id);
}

// PATCH /api/documents/:id - Update a document
@Patch('note/:id')
updateDocument(
@Param('id') id: string,
@Body() updateDocumentDto: UpdateNoteDto,
) {
return this.editorService.updateDocument(id, updateDocumentDto);
}

// DELETE /api/documents/:id - Delete a document
@Delete('note/:id')
deleteDocument(@Param('id') id: string) {
return this.editorService.deleteDocument(id);
}

// POST /api/documents/:id/blocks - Add a new block to a document
@Post('note/:id/block/new')
addBlock(@Param('id') id: string, @Body() createBlockDto: CreateBlockDto) {
return this.editorService.addBlock(id, createBlockDto);
}

// PATCH /api/documents/:id/blocks/:blockId - Update a block
@Patch('note/:id/block/:blockId')
updateBlock(
@Param('id') id: string,
@Param('blockId') blockId: string,
@Body() updateBlockDto: UpdateBlockDto,
) {
return this.editorService.updateBlock(id, blockId, updateBlockDto);
}

// DELETE /api/documents/:id/blocks/:blockId - Delete a block
@Delete('note/:id/block/:blockId')
deleteBlock(@Param('id') id: string, @Param('blockId') blockId: string) {
return this.editorService.deleteBlock(id, blockId);
}
}
10 changes: 10 additions & 0 deletions apps/backend/apps/content_mgmt/src/editor/editor.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { EditorController } from './editor.controller';
import { PrismaService } from 'libs/prisma/prisma.service';
import { EditorService } from './editor.service';

@Module({
controllers: [EditorController],
providers: [EditorService, PrismaService],
})
export class EditorModule {}
99 changes: 99 additions & 0 deletions apps/backend/apps/content_mgmt/src/editor/editor.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { PrismaService } from 'libs/prisma/prisma.service';
import {
CreateBlockDto,
CreateNoteDto,
UpdateBlockDto,
UpdateNoteDto,
} from 'libs/types';

@Injectable()
export class EditorService {
constructor(private readonly prisma: PrismaService) {}

async createDocument(createDocumentDto: CreateNoteDto) {
return `TODO ${createDocumentDto}`;
// return this.prisma.note.create({
// data: {
// title: createDocumentDto.title,
// blocks: {
// create: createDocumentDto.blocks,
// },
// },
// });
}

async getDocument(id: string) {
const document = await this.prisma.note.findUnique({
where: { id },
include: { blocks: true },
});
if (!document) {
throw new NotFoundException(`Document with ID "${id}" not found`);
}
return document;
}

async updateDocument(id: string, DocumentDto: UpdateNoteDto) {
const document = await this.getDocument(id);
return `TODO ${document} ${DocumentDto}`;
// return this.prisma.note.update({
// where: { id },
// data: {
// title: DocumentDto.title ?? document.title,
// blocks: {
// update: DocumentDto.blocks,
// },
// },
// });
}

async deleteDocument(id: string) {
const result = await this.prisma.note.delete({
where: { id },
});
if (!result) {
throw new NotFoundException(`Document with ID "${id}" not found`);
}
}

async addBlock(id: string, createBlockDto: CreateBlockDto) {
return `TODO ${id} ${createBlockDto}`;
// return this.prisma.block.create({
// data: {
// ...createBlockDto,
// id: id,
// },
// });
}

async updateBlock(
id: string,
blockId: string,
updateBlockDto: UpdateBlockDto,
) {
const document = await this.getDocument(id);
const block = document.blocks.find((b) => b.id === blockId);
if (!block) {
throw new NotFoundException(
`Block with ID "${blockId}" not found in document "${id}"`,
);
}
// return this.prisma.block.update({
// where: { id: blockId },
// data: updateBlockDto,
// });
return `TODO ${updateBlockDto}`;
}

async deleteBlock(id: string, blockId: string) {
const document = await this.getDocument(id);
const block = document.blocks.find((b) => b.id === blockId);
if (!block) {
throw new NotFoundException(
`Block with ID "${blockId}" not found in document "${id}"`,
);
}
await this.prisma.block.delete({ where: { id: blockId } });
}
}
6 changes: 6 additions & 0 deletions apps/backend/libs/types/editor/block-type.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum BlockTypeEnum {
TEXT = 'TEXT',
IMAGE = 'IMAGE',
LINK = 'LINK',
INTERNAL_LINK = 'INTERNAL_LINK',
}
26 changes: 26 additions & 0 deletions apps/backend/libs/types/editor/block.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { IsDate, IsEnum, IsNotEmpty, IsString } from 'class-validator';
import { BlockTypeEnum } from './block-type.enum';

export class BlockDto {
@IsString()
@IsNotEmpty()
id: string;

@IsString()
@IsNotEmpty()
noteId: string;

@IsEnum(BlockTypeEnum)
@IsNotEmpty()
type: BlockTypeEnum;

@IsString()
@IsNotEmpty()
content: string;

@IsDate()
createdAt: Date;

@IsDate()
updatedAt: Date;
}
27 changes: 27 additions & 0 deletions apps/backend/libs/types/editor/book.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { IsDate, IsNotEmpty, IsString, ValidateNested } from 'class-validator';
import { Type } from 'class-transformer';
import { SectionDto } from './section.dto';

export class BookDto {
@IsString()
@IsNotEmpty()
id: string;

@IsString()
@IsNotEmpty()
title: string;

@IsString()
@IsNotEmpty()
userId: string;

@ValidateNested({ each: true })
@Type(() => SectionDto)
sections: SectionDto[];

@IsDate()
createdAt: Date;

@IsDate()
updatedAt: Date;
}
16 changes: 16 additions & 0 deletions apps/backend/libs/types/editor/create.block.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { IsEnum, IsNotEmpty, IsString } from 'class-validator';
import { BlockTypeEnum } from './block-type.enum';

export class CreateBlockDto {
@IsString()
@IsNotEmpty()
noteId: string;

@IsEnum(BlockTypeEnum)
@IsNotEmpty()
type: BlockTypeEnum;

@IsString()
@IsNotEmpty()
content: string;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { IsNotEmpty, IsString } from 'class-validator';

export class BookDto {
export class CreateBookDto {
@IsString()
@IsNotEmpty()
title: string;

@IsString()
@IsNotEmpty()
userId: string;
}
17 changes: 17 additions & 0 deletions apps/backend/libs/types/editor/create.note.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { IsNotEmpty, IsString, ValidateNested } from 'class-validator';
import { Type } from 'class-transformer';
import { CreateBlockDto } from './create.block';

export class CreateNoteDto {
@IsString()
@IsNotEmpty()
title: string;

@IsString()
@IsNotEmpty()
sectionId: string;

@ValidateNested({ each: true })
@Type(() => CreateBlockDto)
blocks: CreateBlockDto[];
}
Loading

0 comments on commit 5f20d40

Please sign in to comment.