Skip to content

Commit

Permalink
Fixed services & databases
Browse files Browse the repository at this point in the history
  • Loading branch information
GuilhermeF03 committed May 10, 2024
1 parent 7d73b22 commit daf6790
Show file tree
Hide file tree
Showing 19 changed files with 191 additions and 134 deletions.
6 changes: 2 additions & 4 deletions code/server/src/ts/controllers/http/router.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import express from 'express';
import PromiseRouter from 'express-promise-router';
import { DocumentService, NoteSpaceServices } from '@services/types';
import documentHandlers from '@controllers/http/workspace/documentHandlers';
import errorHandler from '@controllers/http/errorHandler';
import resourcesHandlers from '@controllers/http/workspace/resourcesHandlers';
import { NoteSpaceServices } from '@services/noteSpaceServices';
import workspaceHandlers from '@controllers/http/workspace/workspaceHandlers';
import errorHandler from '@controllers/http/errorHandler';

export default function (service: NoteSpaceServices) {
if (!service) throw new Error('Service parameter is required');
Expand Down
38 changes: 0 additions & 38 deletions code/server/src/ts/controllers/http/workspace/documentHandlers.ts

This file was deleted.

52 changes: 41 additions & 11 deletions code/server/src/ts/controllers/http/workspace/resourcesHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,78 @@
import PromiseRouter from 'express-promise-router';
import { NoteSpaceServices, ResourcesService } from '@services/types';
import { ResourceInputModel, WorkspaceResource } from '@notespace/shared/workspace/types/resource';
import { httpResponse } from '@controllers/http/utils';
import { httpResponse } from '@controllers/http/httpResponse';
import { Request, Response } from 'express';
import { ResourcesService } from '@services/resourcesService';

function resourcesHandlers(services : ResourcesService) {
const router = PromiseRouter();

/**
* Create a new resource -
* @param req
* @param res
*/
const createResource = async (req : Request, res : Response) => {
const resource = req.body as ResourceInputModel;
const id = await services.createResource(resource);

if (!id) return httpResponse.internalServerError(res).send();

return httpResponse.created(res).json({ id });
}

/**
* Get a resource by its id
* @param req
* @param res
*/
const getResource = async (req : Request, res: Response) => {
const id = req.params.resId;
const resource = await services.getResource(id);

if (!resource) {
return httpResponse.notFound(res).send();
}
if (!resource) return httpResponse.notFound(res).send();

return httpResponse.ok(res).json(resource);
}


const getDocContent = async (req : Request, res : Response) => {
const {wid, rid} = req.params;
const content = await services.getDocContent(wid, rid);
return httpResponse.ok(res).json(content);
}

/**
* Update a resource
* @param req
* @param res
*/
const updateResource = async (req : Request, res : Response) => {
const resource = req.body as Partial<WorkspaceResource>
if (!resource.id) {
return httpResponse.badRequest(res).send('Resource id is required');
}

if (!resource.id) return httpResponse.badRequest(res).send('Resource id is required');

await services.updateResource(resource);
return httpResponse.noContent(res).send();
}

/**
* Delete a resource
* @param req
* @param res
*/
const deleteResource = async (req : Request, res : Response) => {
const id = req.params.resId;
await services.deleteResource(id);
return httpResponse.noContent(res).send();
}

router.post('/', createResource);
router.get('/:resId', getResource);
router.put('/:resId', updateResource);
router.delete('/:resId', deleteResource);
router.get('/:rid', getResource);
router.get('/:rid/content', getDocContent);

router.put('/:rid', updateResource);
router.delete('/:rid', deleteResource);

return router;
}
Expand Down
19 changes: 9 additions & 10 deletions code/server/src/ts/controllers/http/workspace/workspaceHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import PromiseRouter from 'express-promise-router';
import { NoteSpaceServices } from '@services/types';
import { httpResponse } from '@controllers/http/utils';

import { httpResponse } from '@controllers/http/httpResponse';
import { Request, Response } from 'express';
import resourcesHandlers from '@controllers/http/workspace/resourcesHandlers';
import documentHandlers from '@controllers/http/workspace/documentHandlers';
import { WorkspaceInfo } from '@notespace/shared/workspace/types/workspace';
import { NoteSpaceServices } from '@services/noteSpaceServices';

function workspaceHandlers(service : NoteSpaceServices){
const router = PromiseRouter();
Expand All @@ -16,7 +16,7 @@ function workspaceHandlers(service : NoteSpaceServices){
}

const getWorkspaceInfo = async (req : Request, res : Response) => {
const workspace = await service.workspace.getWorkspace(req.params.id);
const workspace = await service.workspace.getWorkspace(req.params.wid);
httpResponse.ok(res).json(workspace);
}

Expand All @@ -27,18 +27,17 @@ function workspaceHandlers(service : NoteSpaceServices){
}

const deleteWorkspace = async (req : Request, res : Response) => {
await service.workspace.deleteWorkspace(req.params.id);
await service.workspace.deleteWorkspace(req.params.wid);
httpResponse.noContent(res);
}

router.post('/', createWorkspace);
router.put('/', updateWorkspace);
router.get('/:id', getWorkspaceInfo);
router.delete('/:id', deleteWorkspace);

router.use('/:id/resources', resourcesHandlers(service.resources));
router.use('/:id/documents', documentHandlers(service.document));
router.get('/:wid', getWorkspaceInfo);
router.delete('/:wid', deleteWorkspace);

// Set sub-routes for resources and documents
router.use('/:wid/', resourcesHandlers(service.resources));
return router;
}

Expand Down
2 changes: 1 addition & 1 deletion code/server/src/ts/controllers/ws/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import cursor from '@controllers/ws/namespaces/document/cursor';
import title from '@controllers/ws/namespaces/document/title';
import join from '@controllers/ws/namespaces/document/join';
import leave from '@controllers/ws/namespaces/document/leave';
import { DocumentService } from '@services/types';
import { DocumentService } from '@services/documentService';

export default function events(service: DocumentService): Record<string, Record<string, SocketHandler>> {
if (!service) throw new Error('Service parameter is required');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Socket } from 'socket.io';
import { DocumentService } from '@services//types';

import { Operation } from '@notespace/shared/crdt/types/operations';
import { getRoomId } from '@controllers/ws/rooms/roomOperations';

import { ForbiddenError, InvalidParameterError } from '@domain/errors/errors';
import { DocumentService } from '@services/documentService';

function operation(service: DocumentService) {
return async (socket: Socket, operations: Operation[]) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Socket } from 'socket.io';
import { DocumentService } from '@services/types';

import rooms from '@controllers/ws/rooms/rooms';
import { DocumentService } from '@services/documentService';

function title(service: DocumentService) {
return async function (socket: Socket, title: string) {
Expand Down
10 changes: 0 additions & 10 deletions code/server/src/ts/database/firestore/firestoreDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ export default function DocumentFirestoreDB(): DocumentDatabase {

const db = getFirestore();

/**
* Create a new document in firestore - all metadata is stored in postgres
* @param workspace
* @param id - the id of the document - must correspond to the id in postgres
*/
async function createDocument(workspace: string, id: string) {
const documents = await getWorkspace(workspace);

Expand All @@ -29,11 +24,6 @@ export default function DocumentFirestoreDB(): DocumentDatabase {
return id;
}

/**
* Get a document from firestore
* @param workspace
* @param id
*/
async function getDocument(workspace: string, id: string): Promise<DocumentContent> {
const doc = await getDoc(workspace, id);
const snapshot = await doc.get();
Expand Down
15 changes: 15 additions & 0 deletions code/server/src/ts/database/pg/noteSpaceDB.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { DocumentDatabase, NoteSpaceDatabase, ResourceDatabase, WorkspaceDatabase } from '@database/types';
import { ResourcesDB } from '@database/pg/resourcesDB';
import { WorkspaceDB } from '@database/pg/workspaceDB';

export class NoteSpaceDB implements NoteSpaceDatabase{
readonly document: DocumentDatabase;
readonly resource: ResourceDatabase;
readonly workspace: WorkspaceDatabase;

constructor(document: DocumentDatabase) {
this.document = document;
this.resource = new ResourcesDB();
this.workspace = new WorkspaceDB();
}
}
47 changes: 38 additions & 9 deletions code/server/src/ts/database/pg/resourcesDB.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,44 @@
export class ResourcesDB {
import sql from '@database/pg/sql';
import { ResourceInputModel, WorkspaceResource } from '@notespace/shared/workspace/types/resource';
import { ResourceDatabase } from '@database/types';

async createResource(resource) {
return await this.database.createResource(resource);
export class ResourcesDB implements ResourceDatabase{


async createResource(resource : ResourceInputModel) : Promise<string> {
const results = await sql`
INSERT INTO resource ${sql(resource)}
RETURNING id
`;
if (results.length === 0) throw new Error('Resource not created');
return results[0].id;
}
async getResource(id) {
return await this.database.getResource(id);

async getResource(id : string) : Promise<WorkspaceResource> {
const results : WorkspaceResource[] = await sql`
SELECT * FROM resource WHERE id = ${id}
`;
if (results.length === 0) throw new Error('Resource not found');
if (results.length > 1) throw new Error('Multiple resources found');
return results[0];
}
async updateResource(resource) {
await this.database.updateResource(resource);

async updateResource(resource : Partial<WorkspaceResource>) : Promise<void> {
if (!resource.id) throw new Error('Resource id not provided');

const results = await sql`
UPDATE resource
SET ${sql(resource)}
WHERE id = ${resource.id}
`;
if (results.length === 0) throw new Error('Resource not updated');
}
async deleteResource(id) {
await this.database.deleteResource(id);

async deleteResource(id : string) {
const results = await sql`
DELETE FROM resource
WHERE id = ${id}
`;
if (results.length === 0) throw new Error('Resource not deleted');
}
}
File renamed without changes.
28 changes: 17 additions & 11 deletions code/server/src/ts/database/pg/workspaceDB.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
import { WorkspaceResource } from '@notespace/shared/workspace/types/resource';
import { WorkspaceInfo } from '@notespace/shared/workspace/types/workspace';
import sql from './database';
import sql from './sql';
import { NotFoundError } from '@domain/errors/errors';


export class WorkspaceDB {

async createWorkspace(title: string) : Promise<string> {
const results = await sql`INSERT INTO workspace (title) VALUES (${title}) RETURNING id`;
async createWorkspace(name: string) : Promise<string> {
const results = await sql`
INSERT INTO workspace (name)
VALUES (${name})
RETURNING id
`;
return results[0].id;
}

async getWorkspace(workspace: string) : Promise<WorkspaceInfo> {
const results : WorkspaceInfo[] = await sql`SELECT * FROM workspace WHERE id = ${workspace}`;

if (results.length === 0) {
throw new NotFoundError(`Workspace with id ${workspace} not found`);
}
if (results.length > 1) {
throw new Error(`Multiple workspaces with id ${workspace} found`);
}
if (results.length === 0) throw new NotFoundError(`Workspace with id ${workspace} not found`);
if (results.length > 1) throw new Error(`Multiple workspaces with id ${workspace} found`);
return results[0];
}

async updateWorkspace(id: string, name: string) : Promise<void> {

const results = await sql`
UPDATE workspace
SET name = ${name}
WHERE id = ${id}
`;
if (results.length === 0) throw new NotFoundError(`Workspace with id ${id} not found`);
}

async deleteWorkspace(id: string) : Promise<void> {

const results = await sql`DELETE FROM workspace WHERE id = ${id}`;
if (results.length === 0) throw new NotFoundError(`Workspace with id ${id} not found`);
}

async getWorkspaceResources(workspace: string) : Promise<WorkspaceResource[]> {
Expand Down
Loading

0 comments on commit daf6790

Please sign in to comment.