-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implemented bucket & bucket-file http controller
- Loading branch information
Showing
22 changed files
with
799 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
44 changes: 44 additions & 0 deletions
44
packages/server-storage/src/http/controllers/bucket-file/handlers/delete.ts
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,44 @@ | ||
/* | ||
* Copyright (c) 2022-2024. | ||
* Author Peter Placzek (tada5hi) | ||
* For the full copyright and license information, | ||
* view the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
import { PermissionID } from '@privateaim/core'; | ||
import { ForbiddenError, NotFoundError } from '@ebec/http'; | ||
import { isRealmResourceWritable } from '@authup/core'; | ||
import type { Request, Response } from 'routup'; | ||
import { sendAccepted, useRequestParam } from 'routup'; | ||
import { useDataSource } from 'typeorm-extension'; | ||
import { BucketFileEntity } from '../../../../domains'; | ||
import { useRequestEnv } from '../../../request'; | ||
|
||
export async function executeBucketFileRouteDeleteHandler(req: Request, res: Response) : Promise<any> { | ||
const id = useRequestParam(req, 'id'); | ||
|
||
const ability = useRequestEnv(req, 'ability'); | ||
if (!ability.has(PermissionID.BUCKET_DROP)) { | ||
throw new ForbiddenError(); | ||
} | ||
|
||
const dataSource = await useDataSource(); | ||
const repository = dataSource.getRepository(BucketFileEntity); | ||
const entity = await repository.findOneBy({ id }); | ||
|
||
if (!entity) { | ||
throw new NotFoundError(); | ||
} | ||
|
||
if (!isRealmResourceWritable(useRequestEnv(req, 'realm'), entity.realm_id)) { | ||
throw new ForbiddenError(); | ||
} | ||
|
||
const { id: entityId } = entity; | ||
|
||
await repository.remove(entity); | ||
|
||
entity.id = entityId; | ||
|
||
return sendAccepted(res, entity); | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/server-storage/src/http/controllers/bucket-file/handlers/index.ts
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 @@ | ||
/* | ||
* Copyright (c) 2022-2024. | ||
* Author Peter Placzek (tada5hi) | ||
* For the full copyright and license information, | ||
* view the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
export * from './delete'; | ||
export * from './read'; |
101 changes: 101 additions & 0 deletions
101
packages/server-storage/src/http/controllers/bucket-file/handlers/read.ts
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,101 @@ | ||
/* | ||
* Copyright (c) 2024. | ||
* Author Peter Placzek (tada5hi) | ||
* For the full copyright and license information, | ||
* view the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
import { useRequestQuery } from '@routup/basic/query'; | ||
import type { Request, Response } from 'routup'; | ||
import { send, useRequestParam } from 'routup'; | ||
import { | ||
applyQuery, | ||
useDataSource, | ||
} from 'typeorm-extension'; | ||
import { NotFoundError } from '@ebec/http'; | ||
import { BucketFileEntity } from '../../../../domains'; | ||
|
||
export async function executeBucketFileRouteGetOneHandler(req: Request, res: Response) : Promise<any> { | ||
const id = useRequestParam(req, 'id'); | ||
|
||
const dataSource = await useDataSource(); | ||
const repository = dataSource.getRepository(BucketFileEntity); | ||
const query = repository.createQueryBuilder('bucket') | ||
.where('bucketFile.id = :id', { id }); | ||
|
||
applyQuery(query, useRequestQuery(req), { | ||
defaultAlias: 'bucketFile', | ||
fields: { | ||
default: [ | ||
'id', | ||
'name', | ||
'directory', | ||
'size', | ||
'hash', | ||
'created_at', | ||
'updated_at', | ||
'realm_id', | ||
'robot_id', | ||
'user_id', | ||
], | ||
}, | ||
relations: { | ||
allowed: ['bucket'], | ||
}, | ||
}); | ||
|
||
const entity = await query.getOne(); | ||
|
||
if (!entity) { | ||
throw new NotFoundError(); | ||
} | ||
|
||
return send(res, entity); | ||
} | ||
|
||
export async function executeBucketFileRouteGetManyHandler(req: Request, res: Response) : Promise<any> { | ||
const dataSource = await useDataSource(); | ||
|
||
const repository = dataSource.getRepository(BucketFileEntity); | ||
const query = repository.createQueryBuilder('bucket'); | ||
|
||
const { pagination } = applyQuery(query, useRequestQuery(req), { | ||
defaultAlias: 'bucket', | ||
fields: { | ||
default: [ | ||
'id', | ||
'name', | ||
'directory', | ||
'size', | ||
'hash', | ||
'created_at', | ||
'updated_at', | ||
'realm_id', | ||
'robot_id', | ||
'user_id', | ||
], | ||
}, | ||
relations: { | ||
allowed: ['bucket'], | ||
}, | ||
filters: { | ||
allowed: ['id', 'name', 'directory', 'realm_id', 'user_id', 'robot_id'], | ||
}, | ||
pagination: { | ||
maxLimit: 50, | ||
}, | ||
sort: { | ||
allowed: ['id', 'directory', 'name', 'updated_at', 'created_at'], | ||
}, | ||
}); | ||
|
||
const [entities, total] = await query.getManyAndCount(); | ||
|
||
return send(res, { | ||
data: entities, | ||
meta: { | ||
total, | ||
...pagination, | ||
}, | ||
}); | ||
} |
49 changes: 49 additions & 0 deletions
49
packages/server-storage/src/http/controllers/bucket-file/index.ts
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,49 @@ | ||
/* | ||
* Copyright (c) 2024. | ||
* Author Peter Placzek (tada5hi) | ||
* For the full copyright and license information, | ||
* view the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
import { | ||
DController, DDelete, DGet, DPath, DRequest, DResponse, DTags, | ||
} from '@routup/decorators'; | ||
import type { BucketFileEntity } from '../../../domains'; | ||
import { ForceLoggedInMiddleware } from '../../middlewares'; | ||
import { | ||
executeBucketFileRouteDeleteHandler, | ||
executeBucketFileRouteGetManyHandler, | ||
executeBucketFileRouteGetOneHandler, | ||
} from './handlers'; | ||
|
||
type PartialBucketFile = Partial<BucketFileEntity>; | ||
|
||
@DTags('buckets') | ||
@DController('/bucket-files') | ||
export class BucketFileController { | ||
@DGet('', [ForceLoggedInMiddleware]) | ||
async getMany( | ||
@DRequest() req: any, | ||
@DResponse() res: any, | ||
): Promise<PartialBucketFile[]> { | ||
return await executeBucketFileRouteGetManyHandler(req, res) as PartialBucketFile[]; | ||
} | ||
|
||
@DGet('/:id', [ForceLoggedInMiddleware]) | ||
async getOne( | ||
@DPath('id') id: string, | ||
@DRequest() req: any, | ||
@DResponse() res: any, | ||
): Promise<PartialBucketFile | undefined> { | ||
return await executeBucketFileRouteGetOneHandler(req, res) as PartialBucketFile | undefined; | ||
} | ||
|
||
@DDelete('/:id', [ForceLoggedInMiddleware]) | ||
async drop( | ||
@DPath('id') id: string, | ||
@DRequest() req: any, | ||
@DResponse() res: any, | ||
): Promise<PartialBucketFile | undefined> { | ||
return await executeBucketFileRouteDeleteHandler(req, res) as PartialBucketFile | undefined; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
packages/server-storage/src/http/controllers/bucket/handlers/create.ts
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,36 @@ | ||
/* | ||
* Copyright (c) 2022-2024. | ||
* Author Peter Placzek (tada5hi) | ||
* For the full copyright and license information, | ||
* view the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
import { PermissionID } from '@privateaim/core'; | ||
import { ForbiddenError } from '@ebec/http'; | ||
import type { Request, Response } from 'routup'; | ||
import { sendCreated } from 'routup'; | ||
import { useDataSource } from 'typeorm-extension'; | ||
import { useRequestEnv } from '../../../request'; | ||
import { BucketEntity } from '../../../../domains'; | ||
import { runProjectValidation } from '../utils/validation'; | ||
|
||
export async function executeBucketRouteCreateHandler(req: Request, res: Response) : Promise<any> { | ||
const ability = useRequestEnv(req, 'ability'); | ||
if (!ability.has(PermissionID.BUCKET_ADD)) { | ||
throw new ForbiddenError(); | ||
} | ||
|
||
const result = await runProjectValidation(req, 'create'); | ||
|
||
const dataSource = await useDataSource(); | ||
const repository = dataSource.getRepository(BucketEntity); | ||
const entity = repository.create({ | ||
user_id: useRequestEnv(req, 'userId'), | ||
robot_id: useRequestEnv(req, 'robotId'), | ||
...result.data, | ||
}); | ||
|
||
await repository.save(entity); | ||
|
||
return sendCreated(res, entity); | ||
} |
44 changes: 44 additions & 0 deletions
44
packages/server-storage/src/http/controllers/bucket/handlers/delete.ts
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,44 @@ | ||
/* | ||
* Copyright (c) 2022-2024. | ||
* Author Peter Placzek (tada5hi) | ||
* For the full copyright and license information, | ||
* view the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
import { PermissionID } from '@privateaim/core'; | ||
import { BadRequestError, ForbiddenError, NotFoundError } from '@ebec/http'; | ||
import { isRealmResourceWritable } from '@authup/core'; | ||
import type { Request, Response } from 'routup'; | ||
import { sendAccepted, useRequestParam } from 'routup'; | ||
import { useDataSource } from 'typeorm-extension'; | ||
import { BucketEntity } from '../../../../domains'; | ||
import { useRequestEnv } from '../../../request'; | ||
|
||
export async function executeBucketRouteDeleteHandler(req: Request, res: Response) : Promise<any> { | ||
const id = useRequestParam(req, 'id'); | ||
|
||
const ability = useRequestEnv(req, 'ability'); | ||
if (!ability.has(PermissionID.BUCKET_DROP)) { | ||
throw new ForbiddenError(); | ||
} | ||
|
||
const dataSource = await useDataSource(); | ||
const repository = dataSource.getRepository(BucketEntity); | ||
const entity = await repository.findOneBy({ id }); | ||
|
||
if (!entity) { | ||
throw new NotFoundError(); | ||
} | ||
|
||
if (!isRealmResourceWritable(useRequestEnv(req, 'realm'), entity.realm_id)) { | ||
throw new ForbiddenError(); | ||
} | ||
|
||
const { id: entityId } = entity; | ||
|
||
await repository.remove(entity); | ||
|
||
entity.id = entityId; | ||
|
||
return sendAccepted(res, entity); | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/server-storage/src/http/controllers/bucket/handlers/index.ts
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,11 @@ | ||
/* | ||
* Copyright (c) 2022-2024. | ||
* Author Peter Placzek (tada5hi) | ||
* For the full copyright and license information, | ||
* view the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
export * from './create'; | ||
export * from './delete'; | ||
export * from './read'; | ||
export * from './update'; |
Oops, something went wrong.