Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resty #7

Merged
merged 3 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions api/src/controllers/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,22 @@ import {IndexService} from "../services/index.js";
import {getPKPSession} from "../libs/lit/index.js";

export const listItems = async (req, res, next) => {
//Todo without embeddings, use chromadb filters, accepts query param.
const { indexId } = req.params;
const { cursor, limit } = req.query;
try {

const itemService = new ItemService();
const response = await itemService.getIndexItems(indexId, cursor, limit)

res.status(200).json(response);

} catch (error) {
res.status(500).json({ error: error.message });
}
};

export const addItem = async (req, res, next) => {
const {indexId, itemId} = req.body;
const {indexId, itemId} = req.params;
try {

const indexService = new IndexService();
Expand All @@ -23,7 +34,7 @@ export const addItem = async (req, res, next) => {
//Queue embeddings.
};
export const removeItem = async (req, res, next) => {
const {indexId, itemId} = req.body;
const {indexId, itemId} = req.params;
try {

const indexService = new IndexService();
Expand Down
11 changes: 6 additions & 5 deletions api/src/packages/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,20 @@ app.delete('/indexes/:id', authCheckMiddleware, validator.params(Joi.object({
})), indexController.deleteIndex)

// Items
app.get('/items', validator.query(Joi.object({
app.get('/indexes/:indexId/items', validator.query(Joi.object({
query: Joi.string().min(1).optional(),
cursor: Joi.string().optional(),
limit: Joi.number().default(24),
})), validator.params(Joi.object({
indexId: Joi.custom(isStreamID, "Index ID").required(),
skip: Joi.number().default(0),
take: Joi.number().default(10),
})), itemController.listItems)

app.post('/items', authCheckMiddleware, validator.body(Joi.object({
app.post('/indexes/:indexId/items/:itemId', authCheckMiddleware, validator.params(Joi.object({
indexId: Joi.custom(isStreamID, "Index ID").required(),
itemId: Joi.custom(isStreamID, "Stream ID").required(),
})), itemController.addItem)

app.delete('/items', authCheckMiddleware, validator.body(Joi.object({
app.delete('/indexes/:indexId/items/:itemId', authCheckMiddleware, validator.params(Joi.object({
indexId: Joi.custom(isStreamID, "Index ID").required(),
itemId: Joi.custom(isStreamID, "Stream ID").required(),
})), itemController.removeItem)
Expand Down
76 changes: 76 additions & 0 deletions api/src/services/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,82 @@ export class ItemService {
}
}

async getIndexItems(indexId, cursor=null, limit= 24) {
try {

let cursorFilter = cursor ? `after: "${cursor}",` : "";

const {data, errors} = await this.client.executeQuery(`{
indexItemIndex(first: ${limit}, ${cursorFilter} filters: {
where: {
indexId: { equalTo: "${indexId}"}
}
}, sorting: { createdAt: DESC}) {
pageInfo {
endCursor
}
edges {
node {
... on IndexItem {
id
indexId
itemId
createdAt
updatedAt
deletedAt
item {
id
__typename
... on WebPage {
title
favicon
url
content
createdAt
updatedAt
deletedAt
}
}
index {
id
title
signerPublicKey
signerFunction
createdAt
updatedAt
deletedAt
}
}
}
}
}
}`);

// Handle GraphQL errors
if (errors) {
throw new Error(`Error getting index item: ${JSON.stringify(errors)}`);
}
// Validate the data response
if (!data || !data.indexItemIndex || !data.indexItemIndex.edges) {
throw new Error('Invalid response data');
}

if (data.indexItemIndex.edges.length === 0) {
return null;
}

return {
endCursor: data.indexItemIndex.pageInfo.endCursor,
items: data.indexItemIndex.edges.map(e => e.node.item),
}

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

async addItem(indexId, itemId) {

if (!this.did) {
Expand Down