Skip to content

Commit

Permalink
Add iconUrl logic
Browse files Browse the repository at this point in the history
  • Loading branch information
SpecialAro committed Feb 15, 2024
1 parent 4f91906 commit 23cfbad
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 44 deletions.
55 changes: 17 additions & 38 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, { type CustomData } from 'App/Models/Workspace';
import Workspace from 'App/Models/Workspace';
import { v4 as uuid } from 'uuid';

const createSchema = schema.create({
Expand Down Expand Up @@ -89,24 +89,14 @@ 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),
iconUrl: data.iconUrl,
});

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

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

Expand Down Expand Up @@ -177,30 +166,20 @@ export default class WorkspaceController {

const workspaces = await user.related('workspaces').query();
// Convert to array with all data Franz wants
let workspacesArray: Workspace[] = [];
let workspacesArray: object[] = [];
if (workspaces) {
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 ?? '',
};
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
workspacesArray = workspaces.map((workspace: any) => ({
id: workspace.workspaceId,
name: workspace.name,
iconUrl: workspace.iconUrl,
order: workspace.order,
services:
typeof workspace.services === 'string'
? JSON.parse(workspace.services)
: workspace.services,
userId: user.id,
}));
}

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

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

@column()
public iconUrl: string;

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

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

export interface CustomData {
name?: string;
iconUrl?: string;
}
17 changes: 17 additions & 0 deletions database/migrations/1707993515903_add_workspace_icon_urls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import BaseSchema from '@ioc:Adonis/Lucid/Schema'

export default class extends BaseSchema {
protected tableName = 'workspaces'

public async up () {
this.schema.alterTable(this.tableName, (table) => {
table.string('icon_url').nullable()
})
}

public async down () {
this.schema.alterTable(this.tableName, (table) => {
table.dropColumn('icon_url')
})
}
}

0 comments on commit 23cfbad

Please sign in to comment.