Skip to content

Commit

Permalink
feat: Store iconUrl in workspace.data
Browse files Browse the repository at this point in the history
  • Loading branch information
orgrimarr committed Feb 14, 2024
1 parent 100a6d0 commit bc6eb86
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
53 changes: 38 additions & 15 deletions app/Controllers/Http/WorkspaceController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext';
import { validator, schema } from '@ioc:Adonis/Core/Validator';
import Workspace from 'App/Models/Workspace';
import Workspace, { type CustomData } from 'App/Models/Workspace';
import { v4 as uuid } from 'uuid';

const createSchema = schema.create({
Expand Down Expand Up @@ -89,13 +89,24 @@ export default class WorkspaceController {
const data = request.all();
const { id } = params;

const currentWorkspace = await Workspace.query()
.where('workspaceId', id)
.where('userId', user.id)
.firstOrFail();
const customData: CustomData = JSON.parse(currentWorkspace.data || '{}');
Object.assign(customData, {
name: data.name,
iconUrl: data.iconUrl,
});

// Update data in database
await Workspace.query()
.where('workspaceId', id)
.where('userId', user.id)
.update({
name: data.name,
services: JSON.stringify(data.services),
data: JSON.stringify(customData),
});

// Get updated row
Expand All @@ -106,10 +117,11 @@ export default class WorkspaceController {

return response.send({
id: workspace.workspaceId,
name: data.name,
name: workspace.name,
order: workspace.order,
services: data.services,
services: workspace.services,
userId: user.id,
iconUrl: customData.iconUrl,
});
}

Expand Down Expand Up @@ -165,19 +177,30 @@ export default class WorkspaceController {

const workspaces = await user.related('workspaces').query();
// Convert to array with all data Franz wants
let workspacesArray: object[] = [];
let workspacesArray: Workspace[] = [];
if (workspaces) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
workspacesArray = workspaces.map((workspace: any) => ({
id: workspace.workspaceId,
name: workspace.name,
order: workspace.order,
services:
typeof workspace.services === 'string'
? JSON.parse(workspace.services)
: workspace.services,
userId: user.id,
}));
workspacesArray = workspaces.map((workspace: Workspace) => {
let data: CustomData = {};
try {
data = JSON.parse(workspace.data);
} catch (error) {
console.warn(
`[WorkspaceController] list ${workspace.workspaceId}. Error parsing data JSON`,
error,
);
}
return {
id: workspace.workspaceId,
name: workspace.name,
order: workspace.order,
services:
typeof workspace.services === 'string'
? JSON.parse(workspace.services)
: workspace.services,
userId: user.id,
iconUrl: data.iconUrl ?? '',
};
});
}

return response.send(workspacesArray);
Expand Down
7 changes: 6 additions & 1 deletion app/Models/Workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,16 @@ export default class Workspace extends BaseModel {
public services: string;

@column()
public data: string;
public data: string; // JSON string that match type CustomData

@column.dateTime({ autoCreate: true })
public createdAt: DateTime;

@column.dateTime({ autoCreate: true, autoUpdate: true })
public updatedAt: DateTime;
}

export interface CustomData {
name?: string;
iconUrl?: string;
}

0 comments on commit bc6eb86

Please sign in to comment.