Skip to content

Commit

Permalink
Included x, y, width, height parameters in creation and updation of n…
Browse files Browse the repository at this point in the history
…otes using PUT request
  • Loading branch information
grootmax committed Apr 29, 2024
1 parent 0e8c3b5 commit 2efa8f7
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 8 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,20 @@ Payload:
}
```

or

```js
{
"x": "integer",
"y": "integer",
"type": "note",
"text": "text on note",
"color": "hexadecimal color code",
"width": "integer",
"height": "integer",
}
```

### GET /api/v1/board/:boardId

Return board current state as JSON.
Expand Down
4 changes: 4 additions & 0 deletions backend/src/api/api-routes.openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,14 @@ const spec: { paths: OpenAPIV3.PathsObject } = {
type: "object",
required: ["type", "text", "color"],
properties: {
x: { type: "number" },
y: { type: "number" },
type: { type: "string", enum: ["note"] },
text: { type: "string" },
color: { type: "string" },
container: { type: "string" },
width: { type: "number" },
height: { type: "number" },
replaceTextIfExists: { type: "boolean" },
replaceColorIfExists: { type: "boolean" },
replaceContainerIfExists: { type: "boolean" },
Expand Down
48 changes: 40 additions & 8 deletions backend/src/api/item-create-or-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ export const itemCreateOrUpdate = route
color: t.string,
}),
t.partial({
x: t.number,
y: t.number,
container: t.string,
width: t.number,
height: t.number,
replaceTextIfExists: t.boolean,
replaceColorIfExists: t.boolean,
replaceContainerIfExists: t.boolean,
Expand All @@ -46,10 +50,14 @@ export const itemCreateOrUpdate = route
checkBoardAPIAccess(request, async (board) => {
const { itemId } = request.routeParams
let {
x,
y,
type,
text,
color,
container,
width,
height,
replaceTextIfExists,
replaceColorIfExists,
replaceContainerIfExists = true,
Expand All @@ -59,51 +67,75 @@ export const itemCreateOrUpdate = route
if (existingItem) {
updateItem(
board,
x ?? existingItem.x,
y ?? existingItem.y,
type,
text,
color,
container,
width ?? existingItem.width,
height ?? existingItem.height,
itemId,
replaceTextIfExists,
replaceColorIfExists,
replaceContainerIfExists,
)
} else {
console.log(`Adding new item`)
addItem(board, type, text, color, container, itemId)
const partialParams = { x, y, width, height }
if (x !== undefined || y !== undefined || width !== undefined || height !== undefined) {
addItem(board, type, text, color, container, itemId, partialParams)
} else {
addItem(board, type, text, color, container, itemId)
}
}
return ok({ ok: true })
}),
)

function updateItem(
board: ServerSideBoardState,
x: number,
y: number,
type: "note",
text: string,
color: Color,
container: string | undefined,
width: number,
height: number,
itemId: string,
replaceTextIfExists: boolean | undefined,
replaceColorIfExists: boolean | undefined,
replaceContainerIfExists: boolean | undefined,
) {
const existingItem = board.board.items[itemId]

if (!isNote(existingItem)) {
throw new InvalidRequest("Unexpected item type")
}
const containerItem = findContainer(container, board.board)
const currentContainer = findContainer(existingItem.containerId, board.board)
const containerAttrs =
replaceContainerIfExists && containerItem !== currentContainer
? getItemAttributesForContainer(container, board.board)
: {}

let updatedItem: Note = {
...existingItem,
...containerAttrs,
x: x !== undefined ? x : existingItem.x,
y: y !== undefined ? y : existingItem.y,
text: replaceTextIfExists !== false ? text : existingItem.text,
color: replaceColorIfExists !== false ? color || existingItem.color : existingItem.color,
width: width !== undefined ? width : existingItem.width,
height: height !== undefined ? height : existingItem.height,
}

if (container && replaceContainerIfExists !== false) {
const containerItem = findContainer(container, board.board)
const currentContainer = findContainer(existingItem.containerId, board.board)
const containerAttrs =
containerItem !== currentContainer ? getItemAttributesForContainer(container, board.board) : {}

updatedItem = {
...updatedItem,
...containerAttrs,
}
}

if (!_.isEqual(updatedItem, existingItem)) {
console.log(`Updating existing item`)
dispatchSystemAppEvent(board, { action: "item.update", boardId: board.board.id, items: [updatedItem] })
Expand Down
6 changes: 6 additions & 0 deletions backend/src/api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,19 @@ export function addItem(
color: Color,
container: string | undefined,
itemId?: string,
partialParams?: Partial<{ x: number; y: number; width: number; height: number }>,
) {
if (type !== "note") throw new InvalidRequest("Expecting type: note")
if (typeof text !== "string" || text.length === 0) throw new InvalidRequest("Expecting non zero-length text")

let itemAttributes: object = getItemAttributesForContainer(container, board.board)
if (itemId) itemAttributes = { ...itemAttributes, id: itemId }

// Merge partial parameters with existing attributes
if (partialParams) {
itemAttributes = { ...itemAttributes, ...partialParams }
}

const item: Note = { ...newNote(text, color || DEFAULT_NOTE_COLOR), ...itemAttributes }
const appEvent: AppEvent = { action: "item.add", boardId: board.board.id, items: [item], connections: [] }
dispatchSystemAppEvent(board, appEvent)
Expand Down
17 changes: 17 additions & 0 deletions playwright/src/tests/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ test.describe("API endpoints", () => {
await board.assertItemPosition(board.getNote("Updated item"), 613, 460)
})

const itemnew = await Api.createNote(accessToken, id, "API New note")

await expect(board.getNote("API New note")).toBeVisible()

await test.step("Update new item", async () => {
await Api.updateItem(accessToken, id, itemnew.id, {
x: 20,
y: 20,
type: "note",
text: "Updated new item",
color: "#000000",
width: 10,
height: 10,
})
await expect(board.getNote("Updated new item")).toBeVisible()
})

await test.step("Get board state", async () => {
const content = await Api.getBoard(accessToken, id)
expect(content).toEqual({
Expand Down

0 comments on commit 2efa8f7

Please sign in to comment.