Skip to content

Commit

Permalink
Wow
Browse files Browse the repository at this point in the history
  • Loading branch information
serefyarar committed Feb 13, 2024
1 parent 809e977 commit 3525d12
Showing 1 changed file with 54 additions and 9 deletions.
63 changes: 54 additions & 9 deletions api/src/services/webpage.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ const getCurrentDateTime = () => moment.utc().toISOString();

import {definition} from "../types/merged-runtime.js";

const webPageFragment = `
id
title
favicon
url
content
createdAt
updatedAt
deletedAt`

export class WebPageService {
constructor() {
this.client = new ComposeClient({
Expand Down Expand Up @@ -38,14 +48,7 @@ export class WebPageService {
mutation CreateWebPage($input: CreateWebPageInput!) {
createWebPage(input: $input) {
document {
id
title
favicon
url
content
createdAt
updatedAt
deletedAt
${webPageFragment}
}
}
}`, {input: {content}});
Expand All @@ -70,19 +73,61 @@ export class WebPageService {
}
}

async updateWebPage(id, params) {
if (!this.did) {
throw new Error("DID not set. Use setDID() to set the did.");
}

try {
const content = {
...params,
updatedAt: getCurrentDateTime(),
};
this.client.setDID(this.did);
const {data, errors} = await this.client.executeQuery(`
mutation UpdateWebPage($input: UpdateWebPageInput!) {
updateWebPage(input: $input) {
document {
${webPageFragment}
}
}
}`, {input: {id, content}});

// Handle GraphQL errors
if (errors) {
throw new Error(`Error updating webPage: ${JSON.stringify(errors)}`);
}

// Validate the data response
if (!data || !data.updateWebPage || !data.updateWebPage.document) {
throw new Error('Invalid response data');
}

// Return the created index document
return data.updateWebPage.document;

} catch (error) {
// Log the error and rethrow it for external handling
console.error('Exception occurred in updateWebPage:', error);
throw error;
}
}

async getWebPageById(webPageId) {

try {
const {data, errors} = await this.client.executeQuery(`
{
node(id: "${webPageId}") {
... on WebPage {
${webPageFragment}
}
}
}`);

// Handle GraphQL errors
if (errors) {
throw new Error(`Error getting index item: ${JSON.stringify(errors)}`);
throw new Error(`Error getting webpage: ${JSON.stringify(errors)}`);
}
// Validate the data response
if (!data || !data.node) {
Expand Down

0 comments on commit 3525d12

Please sign in to comment.