Skip to content

Commit

Permalink
quick fix
Browse files Browse the repository at this point in the history
  • Loading branch information
armintalaie committed Mar 29, 2024
1 parent 484e645 commit 8e9ae1b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 27 deletions.
14 changes: 7 additions & 7 deletions src/wiki/doc/getDoc.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -20,7 +25,6 @@ export async function router(
}

const docid = event.pathParameters.docid;

let docRes;

if (event.pathParameters.areaid) {
Expand Down Expand Up @@ -61,7 +65,6 @@ export async function getContentTrue(fileid: string): Promise<any> {
secretAccessKey: process.env.SECRET_ACCESS_KEY,
});

const bucketName = process.env.BUCKET_NAME;
if (!bucketName) {
throw new Error('Bucket not connected');
}
Expand All @@ -76,9 +79,6 @@ export async function getContentTrue(fileid: string): Promise<any> {

return objectData;
} catch (error) {
console.log(error);
return {
msg: 'Error',
};
throw new APIErrorResponse(error.message);
}
}
36 changes: 16 additions & 20 deletions src/wiki/doc/putDoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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',
});
Expand All @@ -59,34 +58,31 @@ 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,
Body: docData.content,
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 (
Expand Down

0 comments on commit 8e9ae1b

Please sign in to comment.