Finished initial implementation of circle endpoints
This commit is contained in:
74
src/endpoints/circles.ts
Normal file
74
src/endpoints/circles.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import { orm } from '../orm/orm';
|
||||
import { UnwrappedRequest } from '../utilities/guard';
|
||||
import { CreatedResponse, ErrorResponse, OkResponse, PagedResponse } from '../utilities/responseHelper';
|
||||
import { CreateCircleRequest, InviteToCircleRequest, UpdateCircleRequest } from '../utilities/requestModels';
|
||||
import { CircleId, PlayerId, UserId } from '../utilities/secureIds';
|
||||
|
||||
async function create(request: UnwrappedRequest<CreateCircleRequest>): Promise<Response> {
|
||||
try {
|
||||
return new CreatedResponse(await orm.circles.create(request.body, request.claims));
|
||||
} catch (error: any) {
|
||||
return new ErrorResponse(error as Error);
|
||||
}
|
||||
}
|
||||
|
||||
async function invite(request: UnwrappedRequest<InviteToCircleRequest>): Promise<Response> {
|
||||
try {
|
||||
let relatedEntityId: UserId | PlayerId | string | undefined;
|
||||
if (request.body.userId) {
|
||||
relatedEntityId = UserId.fromHash(request.body.userId);
|
||||
} else if (request.body.playerId) {
|
||||
relatedEntityId = PlayerId.fromHash(request.body.playerId);
|
||||
} else {
|
||||
relatedEntityId = request.body.email;
|
||||
}
|
||||
return new CreatedResponse(
|
||||
await orm.circles.invite(CircleId.fromHash(request.params.id), relatedEntityId, request.claims),
|
||||
);
|
||||
} catch (error: any) {
|
||||
return new ErrorResponse(error as Error);
|
||||
}
|
||||
}
|
||||
|
||||
async function get(request: UnwrappedRequest): Promise<Response> {
|
||||
try {
|
||||
return new OkResponse(await orm.circles.get(CircleId.fromHash(request.params.id)));
|
||||
} catch (error: any) {
|
||||
return new ErrorResponse(error as Error);
|
||||
}
|
||||
}
|
||||
|
||||
async function update(request: UnwrappedRequest<UpdateCircleRequest>): Promise<Response> {
|
||||
try {
|
||||
return new OkResponse(
|
||||
await orm.circles.update(CircleId.fromHash(request.params.id), request.body),
|
||||
);
|
||||
} catch (error: any) {
|
||||
return new ErrorResponse(error as Error);
|
||||
}
|
||||
}
|
||||
|
||||
async function drop(request: UnwrappedRequest): Promise<Response> {
|
||||
try {
|
||||
return new OkResponse(await orm.circles.drop(CircleId.fromHash(request.params.id)));
|
||||
} catch (error: any) {
|
||||
return new ErrorResponse(error as Error);
|
||||
}
|
||||
}
|
||||
|
||||
async function query(request: UnwrappedRequest): Promise<Response> {
|
||||
try {
|
||||
return new PagedResponse(request, await orm.circles.query(request.params.query));
|
||||
} catch (error: any) {
|
||||
return new ErrorResponse(error as Error);
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
create,
|
||||
get,
|
||||
update,
|
||||
drop,
|
||||
query,
|
||||
invite,
|
||||
};
|
||||
Reference in New Issue
Block a user