Skip to content

Commit

Permalink
#10 - Added shortcut endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Anders164a committed Oct 9, 2023
1 parent 07ebc51 commit 1733649
Show file tree
Hide file tree
Showing 21 changed files with 1,667 additions and 14 deletions.
23 changes: 23 additions & 0 deletions prisma/migrations/20231009104422_item_shortcut/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-- CreateTable
CREATE TABLE "ItemShortcut" (
"id" SERIAL NOT NULL,
"itemId" INTEGER NOT NULL,
"linkedItemId" INTEGER NOT NULL,

CONSTRAINT "ItemShortcut_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "ItemShortcut_itemId_key" ON "ItemShortcut"("itemId");

-- CreateIndex
CREATE UNIQUE INDEX "ItemShortcut_linkedItemId_key" ON "ItemShortcut"("linkedItemId");

-- CreateIndex
CREATE UNIQUE INDEX "ItemShortcut_itemId_linkedItemId_key" ON "ItemShortcut"("itemId", "linkedItemId");

-- AddForeignKey
ALTER TABLE "ItemShortcut" ADD CONSTRAINT "ItemShortcut_itemId_fkey" FOREIGN KEY ("itemId") REFERENCES "Item"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ItemShortcut" ADD CONSTRAINT "ItemShortcut_linkedItemId_fkey" FOREIGN KEY ("linkedItemId") REFERENCES "Item"("id") ON DELETE CASCADE ON UPDATE CASCADE;
25 changes: 19 additions & 6 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ model Item {
ownerId Int
parentId Int?
owner User @relation(fields: [ownerId], references: [id])
parentItem Item? @relation("ItemToItem", fields: [parentId], references: [id])
Items Item[] @relation("ItemToItem")
ItemFolder ItemFolder?
ItemBlob ItemBlob?
ItemSharing ItemSharing[]
owner User @relation(fields: [ownerId], references: [id])
parentItem Item? @relation("ItemToItem", fields: [parentId], references: [id])
Items Item[] @relation("ItemToItem")
ItemFolder ItemFolder?
ItemBlob ItemBlob?
ItemSharing ItemSharing[]
ItemShortcut ItemShortcut? @relation("shortcutItem")
LinkedItemShortcut ItemShortcut? @relation("linkedItem")
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
Expand Down Expand Up @@ -96,3 +98,14 @@ model ItemSharing {
@@index([itemId])
@@index([userId])
}

model ItemShortcut {
id Int @id @default(autoincrement())
itemId Int @unique
linkedItemId Int @unique
shortcutItem Item @relation("shortcutItem", fields: [itemId], references: [id], onDelete: Cascade)
linkedItem Item @relation("linkedItem", fields: [linkedItemId], references: [id], onDelete: Cascade)
@@unique([itemId, linkedItemId])
}
20 changes: 20 additions & 0 deletions src/locales/da.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@
"folder": {
"notFound": "Mappen blev ikke fundet"
},
"shortcut": {
"notFound": "Genvej blev ikke fundet"
},
"notFound": "Item ikke fundet"
},
"folder": {
Expand All @@ -123,5 +126,22 @@
"parentId": {
"type": "Parent id skal være et tal"
}
},
"shortcut": {
"id": {
"required": "id er påkrævet",
"type": "id skal være et tal"
},
"name": {
"required": "Navn er påkrævet",
"type": "Navn skal være en tekst"
},
"linkedItemId": {
"required": "Det forbundet itemId er påkrævet",
"type": "Det forbundet itemId skal være en tekst"
},
"parentId": {
"type": "Parent id skal være et tal"
}
}
}
20 changes: 20 additions & 0 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@
"folder": {
"notFound": "Folder not found"
},
"shortcut": {
"notFound": "Shortcut not found"
},
"notFound": "Item not found"
},
"folder": {
Expand All @@ -123,5 +126,22 @@
"parentId": {
"type": "Parent id must be a number"
}
},
"shortcut": {
"id": {
"required": "id is required",
"type": "id must be a number"
},
"name": {
"required": "Name is required",
"type": "Name must be a string"
},
"linkedItemId": {
"required": "Linked itemId is required",
"type": "Linked itemId must be a number"
},
"parentId": {
"type": "Parent id must be a number"
}
}
}
31 changes: 29 additions & 2 deletions src/modules/item/__test__/item.read.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import UserService from '../../auth/user.service';
import FolderService from '../folder/folder.service';
import AuthService from '../../auth/auth.service';
import BlobService from '../blob/blob.service';
import ShortcutService from '../shortcut/shortcut.service';

describe('GET /api/item/:parentId', () => {
let userService: UserService;
let folderService: FolderService;
let authService: AuthService;
let blobService: BlobService;
let shortcutService: ShortcutService;

let user: User;
let otherUser: User;
Expand All @@ -18,6 +20,7 @@ describe('GET /api/item/:parentId', () => {
folderService = new FolderService();
authService = new AuthService();
blobService = new BlobService();
shortcutService = new ShortcutService();

user = await userService.createUser({
name: 'Joe Biden the 1st',
Expand All @@ -41,7 +44,7 @@ describe('GET /api/item/:parentId', () => {
parentId: null,
});

await blobService.createBlob({
const blob = await blobService.createBlob({
mimeType: 'text/plain',
name: 'test1.txt',
ownerId: user.id,
Expand All @@ -56,6 +59,13 @@ describe('GET /api/item/:parentId', () => {
parentId: parentFolder.id,
});

await shortcutService.createShortcut({
name: 'Shortcut',
ownerId: user.id,
linkedItemId: blob.id,
parentId: parentFolder.id,
});

const response = await global.fastify.inject({
method: 'GET',
url: '/api/item/' + parentFolder.id,
Expand Down Expand Up @@ -88,6 +98,16 @@ describe('GET /api/item/:parentId', () => {
deletedAt: null,
updatedAt: expect.any(String),
},
{
id: expect.any(Number),
name: 'Shortcut',
parentId: parentFolder.id,
ownerId: user.id,
mimeType: 'application/vnd.cloudstore.shortcut',
createdAt: expect.any(String),
deletedAt: null,
updatedAt: expect.any(String),
},
]);
});

Expand All @@ -99,7 +119,7 @@ describe('GET /api/item/:parentId', () => {
parentId: null,
});

await blobService.createBlob({
const blob = await blobService.createBlob({
mimeType: 'text/plain',
name: 'test1.txt',
ownerId: user.id,
Expand All @@ -113,6 +133,13 @@ describe('GET /api/item/:parentId', () => {
parentId: parentFolder.id,
});

await shortcutService.createShortcut({
name: 'Shortcut',
ownerId: user.id,
linkedItemId: blob.id,
parentId: null,
});

const response = await global.fastify.inject({
method: 'GET',
url: '/api/item/' + parentFolder.id,
Expand Down
31 changes: 29 additions & 2 deletions src/modules/item/__test__/item.root.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import UserService from '../../auth/user.service';
import FolderService from '../folder/folder.service';
import AuthService from '../../auth/auth.service';
import BlobService from '../blob/blob.service';
import ShortcutService from '../shortcut/shortcut.service';

describe('GET /api/item', () => {
let userService: UserService;
let folderService: FolderService;
let authService: AuthService;
let blobService: BlobService;
let shortcutService: ShortcutService;

let user: User;

Expand All @@ -17,6 +19,7 @@ describe('GET /api/item', () => {
folderService = new FolderService();
authService = new AuthService();
blobService = new BlobService();
shortcutService = new ShortcutService();

user = await userService.createUser({
name: 'Joe Biden the 1st',
Expand All @@ -36,7 +39,7 @@ describe('GET /api/item', () => {
blobUrl: 'https://example.com/test1.txt',
});

await folderService.createFolder({
const folder = await folderService.createFolder({
name: 'Folder1',
color: '#123456',
ownerId: user.id,
Expand All @@ -49,6 +52,13 @@ describe('GET /api/item', () => {
parentId: null,
});

await shortcutService.createShortcut({
name: 'Shortcut',
ownerId: user.id,
linkedItemId: folder.id,
parentId: null,
});

const response = await global.fastify.inject({
method: 'GET',
url: '/api/item',
Expand Down Expand Up @@ -92,6 +102,16 @@ describe('GET /api/item', () => {
deletedAt: null,
updatedAt: expect.any(String),
},
{
id: expect.any(Number),
name: 'Shortcut',
parentId: null,
ownerId: user.id,
mimeType: 'application/vnd.cloudstore.shortcut',
createdAt: expect.any(String),
deletedAt: null,
updatedAt: expect.any(String),
},
]);
});

Expand All @@ -104,7 +124,7 @@ describe('GET /api/item', () => {
blobUrl: 'https://example.com/test1.txt',
});

await folderService.createFolder({
const folder = await folderService.createFolder({
name: 'Folder1',
color: '#123456',
ownerId: user.id,
Expand All @@ -117,6 +137,13 @@ describe('GET /api/item', () => {
parentId: null,
});

await shortcutService.createShortcut({
name: 'Shortcut',
ownerId: user.id,
linkedItemId: folder.id,
parentId: null,
});

const response = await global.fastify.inject({
method: 'GET',
url: '/api/item',
Expand Down
Loading

0 comments on commit 1733649

Please sign in to comment.