Skip to content

Commit

Permalink
Bug Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
R1c4rdCo5t4 committed Jun 5, 2024
1 parent c7d01f9 commit 55e6c29
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 39 deletions.
2 changes: 1 addition & 1 deletion code/server/docker/Dockerfile-db
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ FROM postgres
USER postgres
WORKDIR /app

COPY ./postgres/create_tables.sql /docker-entrypoint-initdb.d/1_create-schema.sql
COPY ./sql/create_tables.sql /docker-entrypoint-initdb.d/1_create-schema.sql

COPY --chown=postgres:postgres ./docker/scripts/wait-for-postgres.sh ./bin/wait-for-postgres.sh
RUN chmod +x ./bin/wait-for-postgres.sh
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ function resourcesHandlers(service: ResourcesService, io: Server) {
createdAt: now,
updatedAt: now,
};

io.in(wid).emit('createdResource', createdResource);
httpResponse.created(res).json({ id });
};
Expand Down
7 changes: 3 additions & 4 deletions code/server/src/controllers/ws/initSocketEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ export default function initSocketEvents(events: Record<string, SocketHandler>)
return async (socket: Socket) => {
logger.logInfo('Client connected:' + socket.id);

// Add listeners for each event
for (const event in Object.entries(events)) {
const handler = events[event];
// add listeners for each event
Object.entries(events).forEach(([event, handler]) => {
socket.on(event, async data => {
try {
logger.logInfo('Event: ' + event + ': ' + JSON.stringify(data));
Expand All @@ -21,7 +20,7 @@ export default function initSocketEvents(events: Record<string, SocketHandler>)
socket.emit('error', e.message);
}
});
}
});

socket.on('disconnect', reason => {
logger.logInfo('Client disconnected: ' + reason);
Expand Down
54 changes: 21 additions & 33 deletions code/server/src/databases/memory/MemoryResourcesDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,13 @@ export class MemoryResourcesDB implements ResourcesRepository {
Memory.reset();
}

async createResource(wid: string, name: string, type: ResourceType, parent?: string): Promise<string> {
return this._createResource(wid, name, type, parent);
}

private _createResource(wid: string, name: string, type: ResourceType, parent?: string): string {
async createResource(wid: string, name: string, type: ResourceType, parent?: string) {
const workspace = Memory.workspaces[wid];
if (!workspace) throw new NotFoundError(`Workspace not found`);

// create resource
const id = uuid();
// Create resource
Object.assign(workspace.resources[id], {
workspace.resources[id] = {
id,
name,
workspace: wid,
Expand All @@ -28,51 +24,35 @@ export class MemoryResourcesDB implements ResourcesRepository {
children: [],
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
});
};

// update parent
if (parent) {
const parentResource = this._getResource(parent);
const parentResource = this.getResourceById(parent);
parentResource.children.push(id);
}
return id;
}

async getResource(id: string): Promise<Resource> {
return this._getResource(id);
}

private _getResource(id: string): Resource {
for (const workspace of Object.values(Memory.workspaces)) {
const resource = workspace.resources[id];
if (resource) return resource;
}
throw new NotFoundError(`Resource not found`);
}

async updateResource(id: string, newProps: Partial<Resource>): Promise<void> {
return this._updateResource(id, newProps);
return this.getResourceById(id);
}

private _updateResource(id: string, newProps: Partial<Resource>): void {
const resource = this._getResource(id);
async updateResource(id: string, newProps: Partial<Resource>) {
const resource = this.getResourceById(id);
Object.assign(resource, newProps);

if (newProps.parent) {
const prevParent = this._getResource(resource.parent);
const prevParent = this.getResourceById(resource.parent);
prevParent.children = prevParent.children.filter((childId: string) => childId !== id);
const newParent = this._getResource(newProps.parent);
const newParent = this.getResourceById(newProps.parent);
newParent.children.push(id);
}
}

async deleteResource(id: string): Promise<void> {
return this._deleteResource(id);
}

private _deleteResource(id: string): void {
const resource = this._getResource(id);
const parentResource = this._getResource(resource.parent);
async deleteResource(id: string) {
const resource = await this.getResource(id);
const parentResource = await this.getResource(resource.parent);

// remove from parent
parentResource.children = parentResource.children.filter((childId: string) => childId !== id);
Expand All @@ -83,4 +63,12 @@ export class MemoryResourcesDB implements ResourcesRepository {
// delete resource
delete Memory.workspaces[resource.workspace].resources[id];
}

private getResourceById(id: string) {
for (const workspace of Object.values(Memory.workspaces)) {
const resource = workspace.resources[id];
if (resource) return resource;
}
throw new NotFoundError(`Resource not found`);
}
}
File renamed without changes.

0 comments on commit 55e6c29

Please sign in to comment.