Skip to content

Commit

Permalink
#14 - Added item endpoint, to get item with extra data (Folder, Blob,…
Browse files Browse the repository at this point in the history
… etc.)
  • Loading branch information
Anders164a committed Oct 5, 2023
1 parent 5347359 commit 25866c5
Show file tree
Hide file tree
Showing 13 changed files with 334 additions and 42 deletions.
4 changes: 4 additions & 0 deletions src/locales/da.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@
"required": "id er påkrævet",
"type": "id skal være et tal"
},
"parentId": {
"required": "Parent id er påkrævet",
"type": "Parent id skal være et tal"
},
"blob": {
"notFound": "Blob ikke fundet"
},
Expand Down
4 changes: 4 additions & 0 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@
"required": "id is required",
"type": "id must be a number"
},
"parentId": {
"required": "Parent id is required",
"type": "Parent id must be a number"
},
"blob": {
"notFound": "Blob not found"
},
Expand Down
8 changes: 5 additions & 3 deletions src/modules/auth/auth.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,15 @@ export default async (fastify: FastifyInstance) => {
'/user',
{
schema: {
headers: {
Authorization: true,
},
tags: ['Auth'],
response: {
200: { $ref: 'userResponseSchema' },
},
security: [
{
bearerAuth: [],
},
],
},
onRequest: [fastify.authenticate],
},
Expand Down
32 changes: 20 additions & 12 deletions src/modules/item/blob/blob.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ export default async (fastify: FastifyInstance) => {
'/:id',
{
schema: {
headers: {
Authorization: true,
},
tags: ['Blob'],
params: { $ref: 'readBlobSchema' },
response: {
200: {
$ref: 'readBlobResponseSchema',
},
},
security: [
{
bearerAuth: [],
},
],
},
onRequest: [fastify.authenticate],
},
Expand All @@ -35,16 +37,18 @@ export default async (fastify: FastifyInstance) => {
'/',
{
schema: {
headers: {
Authorization: true,
},
tags: ['Blob'],
body: { $ref: 'editBlobSchema' },
response: {
200: {
$ref: 'editBlobResponseSchema',
},
},
security: [
{
bearerAuth: [],
},
],
},
onRequest: [fastify.authenticate],
},
Expand All @@ -55,14 +59,16 @@ export default async (fastify: FastifyInstance) => {
'/',
{
schema: {
headers: {
Authorization: true,
},
tags: ['Blob'],
body: { $ref: 'uploadBlobSchema' },
response: {
200: { $ref: 'uploadBlobResponseSchema' },
},
security: [
{
bearerAuth: [],
},
],
},
},
blobController.addHandler.bind(blobController),
Expand All @@ -72,11 +78,13 @@ export default async (fastify: FastifyInstance) => {
'/:id',
{
schema: {
headers: {
Authorization: true,
},
tags: ['Blob'],
params: { $ref: 'deleteBlobSchema' },
security: [
{
bearerAuth: [],
},
],
},
onRequest: [fastify.authenticate],
},
Expand Down
2 changes: 1 addition & 1 deletion src/modules/item/folder/folder.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export default class FolderController {
return reply.unauthorized();
}

await this.folderService.deleteFolderByItemId(request.params.id);
await this.folderService.deleteFolderByItemId(folder.id);
return reply.code(204).send();
} catch (e) {
if (e instanceof Error) {
Expand Down
32 changes: 20 additions & 12 deletions src/modules/item/folder/folder.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ export default async (fastify: FastifyInstance) => {
'/:id',
{
schema: {
headers: {
Authorization: true,
},
tags: ['Folder'],
params: { $ref: 'readFolderSchema' },
response: {
200: { $ref: 'readFolderResponseSchema' },
},
security: [
{
bearerAuth: [],
},
],
},
onRequest: [fastify.authenticate],
},
Expand All @@ -34,14 +36,16 @@ export default async (fastify: FastifyInstance) => {
'/',
{
schema: {
headers: {
Authorization: true,
},
tags: ['Folder'],
body: { $ref: 'editFolderSchema' },
response: {
200: { $ref: 'editFolderResponseSchema' },
},
security: [
{
bearerAuth: [],
},
],
},
onRequest: [fastify.authenticate],
},
Expand All @@ -52,14 +56,16 @@ export default async (fastify: FastifyInstance) => {
'/',
{
schema: {
headers: {
Authorization: true,
},
tags: ['Folder'],
body: { $ref: 'addFolderSchema' },
response: {
200: { $ref: 'addFolderResponseSchema' },
},
security: [
{
bearerAuth: [],
},
],
},
onRequest: [fastify.authenticate],
},
Expand All @@ -70,11 +76,13 @@ export default async (fastify: FastifyInstance) => {
'/:id',
{
schema: {
headers: {
Authorization: true,
},
tags: ['Folder'],
params: { $ref: 'deleteFolderSchema' },
security: [
{
bearerAuth: [],
},
],
},
onRequest: [fastify.authenticate],
},
Expand Down
8 changes: 8 additions & 0 deletions src/modules/item/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ import folder from './folder';
import { getOptionsWithPrefix } from '..';
import blob from './blob';
import sharing from './sharing';
import { itemSchemas } from './item.schema';
import itemRoute from './item.route';

export default fastifyPlugin(async (fastify: FastifyInstance, options: FastifyPluginOptions) => {
await fastify.register(blob, getOptionsWithPrefix(options, '/blob'));
await fastify.register(folder, getOptionsWithPrefix(options, '/folder'));
await fastify.register(sharing, getOptionsWithPrefix(options, '/sharing'));

for (const schema of itemSchemas) {
fastify.addSchema(schema);
}

await fastify.register(itemRoute, options);
});
56 changes: 56 additions & 0 deletions src/modules/item/item.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { FastifyReply, FastifyRequest } from 'fastify';
import ItemService from './item.service';
import { ReadInput } from './item.schema';
import AccessService from './sharing/access.service';

export default class ItemController {
private itemService: ItemService;
private accessService: AccessService;

constructor(itemService: ItemService, accessService: AccessService) {
this.itemService = itemService;
this.accessService = accessService;
}

public async browseHandler(request: FastifyRequest, reply: FastifyReply) {

Check warning on line 15 in src/modules/item/item.controller.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
try {
const items = await this.itemService.getByOwnerIdAndParentId(request.user.sub, null);

Check warning on line 17 in src/modules/item/item.controller.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

return reply.code(200).send(items);

Check warning on line 19 in src/modules/item/item.controller.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
} catch (e) {
if (e instanceof Error) {
return reply.badRequest(request.i18n.t(e.message));

Check warning on line 22 in src/modules/item/item.controller.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 23 in src/modules/item/item.controller.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 23 in src/modules/item/item.controller.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

/* istanbul ignore next */
return reply.badRequest();
}

Check warning on line 27 in src/modules/item/item.controller.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

public async readHandler(

Check warning on line 30 in src/modules/item/item.controller.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
request: FastifyRequest<{
Params: ReadInput;
}>,
reply: FastifyReply,
) {
try {
if (!(await this.accessService.hasAccessToItem(request.params.parentId, request.user.sub))) {
return reply.unauthorized();

Check warning on line 38 in src/modules/item/item.controller.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 39 in src/modules/item/item.controller.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 39 in src/modules/item/item.controller.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

const items = await this.itemService.getByOwnerIdAndParentIdAndSharred(
request.user.sub,
request.params.parentId,

Check warning on line 43 in src/modules/item/item.controller.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
);

return reply.code(200).send(items);

Check warning on line 46 in src/modules/item/item.controller.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
} catch (e) {
if (e instanceof Error) {
return reply.badRequest(request.i18n.t(e.message));

Check warning on line 49 in src/modules/item/item.controller.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 50 in src/modules/item/item.controller.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 50 in src/modules/item/item.controller.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

/* istanbul ignore next */
return reply.badRequest();
}

Check warning on line 54 in src/modules/item/item.controller.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}
}
52 changes: 52 additions & 0 deletions src/modules/item/item.route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { FastifyInstance } from 'fastify';
import ItemController from './item.controller';
import ItemService from './item.service';
import AccessService from './sharing/access.service';
import SharingService from './sharing/sharing.service';

export default async (fastify: FastifyInstance) => {
const itemService = new ItemService();
const itemController = new ItemController(
itemService,
new AccessService(itemService, new SharingService()),
);

fastify.get(
'/',
{
schema: {
tags: ['Items'],
response: {
200: { $ref: 'itemsResponseSchema' },
},
security: [
{
bearerAuth: [],
},
],
},
onRequest: [fastify.authenticate],
},
itemController.browseHandler.bind(itemController),
);

fastify.get(
'/:parentId',
{
schema: {
tags: ['Items'],
params: { $ref: 'readItemsSchema' },
response: {
200: { $ref: 'itemsResponseSchema' },
},
security: [
{
bearerAuth: [],
},
],
},
onRequest: [fastify.authenticate],
},
itemController.readHandler.bind(itemController),
);
};
Loading

0 comments on commit 25866c5

Please sign in to comment.