diff --git a/src/wiki/doc/getDoc.ts b/src/wiki/doc/getDoc.ts index 448ebd4..2e55f5f 100644 --- a/src/wiki/doc/getDoc.ts +++ b/src/wiki/doc/getDoc.ts @@ -1,9 +1,14 @@ import { LambdaBuilder } from '../../util/middleware/middleware'; import { APIGatewayProxyEvent } from 'aws-lambda'; -import { APIResponse, SuccessResponse } from '../../util/middleware/response'; +import { + APIErrorResponse, + APIResponse, + SuccessResponse, +} from '../../util/middleware/response'; import { S3 } from 'aws-sdk'; import { getDatabase } from '../../util/db'; +const bucketName = process.env.BUCKET_NAME; const db = getDatabase(); export const handler = new LambdaBuilder(router).build(); @@ -20,7 +25,6 @@ export async function router( } const docid = event.pathParameters.docid; - let docRes; if (event.pathParameters.areaid) { @@ -61,7 +65,6 @@ export async function getContentTrue(fileid: string): Promise { secretAccessKey: process.env.SECRET_ACCESS_KEY, }); - const bucketName = process.env.BUCKET_NAME; if (!bucketName) { throw new Error('Bucket not connected'); } @@ -76,9 +79,6 @@ export async function getContentTrue(fileid: string): Promise { return objectData; } catch (error) { - console.log(error); - return { - msg: 'Error', - }; + throw new APIErrorResponse(error.message); } } diff --git a/src/wiki/doc/putDoc.ts b/src/wiki/doc/putDoc.ts index c74f31a..466d337 100644 --- a/src/wiki/doc/putDoc.ts +++ b/src/wiki/doc/putDoc.ts @@ -7,7 +7,7 @@ import { APIErrorResponse, } from '../../util/middleware/response'; // import { Authorizer } from '../../util/middleware/authorizer'; -import { NewDocuments, getDatabase } from '../../util/db'; +import { getDatabase } from '../../util/db'; const s3 = new S3({ accessKeyId: process.env.ACCESS_KEY, @@ -43,13 +43,12 @@ export async function router( const docData = JSON.parse(event.body || ''); - if (!docData || !docData.title || !docData.content) { + if (!docData) { throw new Error('Request is missing body or body is invalid'); } try { - await verifyDocumentCreation(areaid, docid, docData); - await createDocument(areaid, docid, docData); + await updateDocument(areaid, docid, docData); return new SuccessResponse({ message: 'Document created successfully', }); @@ -59,12 +58,22 @@ export async function router( } } -export const createDocument = async ( +export const updateDocument = async ( areaid: string, docid: string, docData: { title: string; content: string } ) => { - const objectKey = `${areaid}/${docid}.md`; + const doc = await db + .selectFrom('document') + .selectAll() + .where('id', '=', docid as unknown as number) + .executeTakeFirst(); + + if (!doc) { + throw new Error('Document not found'); + } + + const objectKey = doc?.fileid; const putObjectParams: S3.PutObjectRequest = { Bucket: bucketName, Key: objectKey, @@ -72,21 +81,8 @@ export const createDocument = async ( ContentType: 'text/html', // Adjust the content type accordingly }; - const areaIdKey = Number(areaid); await s3.putObject(putObjectParams).promise(); - - const docArgs: NewDocuments = { - areaid: areaIdKey, - title: docData.title, - doclink: objectKey, - }; - - const { insertId } = await db - .insertInto('document') - .values(docArgs) - .executeTakeFirst(); - - return insertId; + return doc.id; }; export const verifyDocumentCreation = async (