-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6256 from signalco-io/feat/uier/db-comments
feat(uier): Connected toolbar to API and DB
- Loading branch information
Showing
28 changed files
with
365 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
COSMOSDB_CONNECTION_STRING=secret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { createComment, getComments, updateComment } from '../../../src/lib/repo/commentsRepo'; | ||
|
||
export async function GET(request: Request) { | ||
const domain = request.headers.get('host'); | ||
if (!domain) | ||
return new Response('Couldn\'t resolve domain', { status: 402 }); | ||
|
||
const comments = await getComments(domain); | ||
return Response.json(comments); | ||
} | ||
|
||
export async function POST(request: Request) { | ||
const domain = request.headers.get('host'); | ||
if (!domain) | ||
return new Response('Couldn\'t resolve domain', { status: 402 }); | ||
|
||
const comment = await request.json() as { | ||
id?: string, | ||
path: string, | ||
position: object, // TODO: Types | ||
thread: object, // TODO: Types | ||
device?: object, // TODO: Types | ||
resolved?: boolean | ||
}; | ||
|
||
if (comment.id) { | ||
await updateComment(domain, comment.id, comment); | ||
return Response.json({ id: comment.id }); | ||
} else { | ||
const id = await createComment({ domain, ...comment }); | ||
return Response.json({ id }, { status: 201 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { type Container, CosmosClient, type Database } from '@azure/cosmos'; | ||
|
||
let client: CosmosClient | null = null; | ||
let dataDb: Database | null = null; | ||
|
||
let commentsContainer: Container | null = null; | ||
|
||
function cosmosClient() { | ||
if (client == null) { | ||
const connectionString = process.env.COSMOSDB_CONNECTION_STRING; | ||
if (!connectionString) | ||
throw new Error('COSMOSDB_CONNECTION_STRING is not available in the environment. Please set it before using this feature.'); | ||
|
||
client = new CosmosClient(connectionString); | ||
} | ||
|
||
return client; | ||
} | ||
|
||
function cosmosDataDb() { | ||
return dataDb = dataDb ?? cosmosClient().database('uierdata'); | ||
} | ||
|
||
export function cosmosDataContainerComments() { | ||
return commentsContainer = commentsContainer ?? cosmosDataDb().container('comments'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export type DbComment = { | ||
id: string; | ||
domain: string; | ||
path: string; | ||
position: object, // TODO: Type | ||
thread: object, // TODO: Type | ||
device?: object, // TODO: Type | ||
resolved?: boolean; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { nanoid } from 'nanoid'; | ||
import { DbComment } from '../db/schema'; | ||
import { cosmosDataContainerComments } from '../db/client'; | ||
|
||
export async function getComments(domain: string) { | ||
return (await cosmosDataContainerComments().items.query<DbComment>({ | ||
query: 'SELECT * FROM c WHERE c.domain = @domain', | ||
parameters: [{ name: '@domain', value: domain }] | ||
}).fetchAll()).resources; | ||
} | ||
|
||
export async function createComment({ domain, path, position, thread, device }: { domain: string, path: string, position: object, thread: object, device?: object }) { | ||
const container = cosmosDataContainerComments(); | ||
const commentId = `comment_${nanoid()}`; | ||
await container.items.create<DbComment>({ | ||
id: commentId, | ||
domain, | ||
path, | ||
position, // TODO: Sanitize | ||
thread, // TODO: Sanitize | ||
device, // TODO: Sanitize | ||
}); | ||
return commentId; | ||
} | ||
|
||
export async function updateComment(domain: string, id: string, comment: { path: string, position: object, thread: object, device?: object, resolved?: boolean }) { | ||
const container = cosmosDataContainerComments(); | ||
await container.item(id, domain).replace<DbComment>({ | ||
id, | ||
domain, | ||
...comment | ||
}); | ||
} | ||
|
||
export async function deleteComment(domain: string, id: string) { | ||
await cosmosDataContainerComments().item(id, domain).delete(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.