Skip to content

Commit 23cfbad

Browse files
committed
Add iconUrl logic
1 parent 4f91906 commit 23cfbad

File tree

3 files changed

+38
-44
lines changed

3 files changed

+38
-44
lines changed

app/Controllers/Http/WorkspaceController.ts

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext';
22
import { validator, schema } from '@ioc:Adonis/Core/Validator';
3-
import Workspace, { type CustomData } from 'App/Models/Workspace';
3+
import Workspace from 'App/Models/Workspace';
44
import { v4 as uuid } from 'uuid';
55

66
const createSchema = schema.create({
@@ -89,24 +89,14 @@ export default class WorkspaceController {
8989
const data = request.all();
9090
const { id } = params;
9191

92-
const currentWorkspace = await Workspace.query()
93-
.where('workspaceId', id)
94-
.where('userId', user.id)
95-
.firstOrFail();
96-
const customData: CustomData = JSON.parse(currentWorkspace.data || '{}');
97-
Object.assign(customData, {
98-
name: data.name,
99-
iconUrl: data.iconUrl,
100-
});
101-
10292
// Update data in database
10393
await Workspace.query()
10494
.where('workspaceId', id)
10595
.where('userId', user.id)
10696
.update({
10797
name: data.name,
10898
services: JSON.stringify(data.services),
109-
data: JSON.stringify(customData),
99+
iconUrl: data.iconUrl,
110100
});
111101

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

118108
return response.send({
119109
id: workspace.workspaceId,
120-
name: workspace.name,
110+
name: data.name,
121111
order: workspace.order,
122-
services: workspace.services,
112+
services: data.services,
123113
userId: user.id,
124-
iconUrl: customData.iconUrl,
125114
});
126115
}
127116

@@ -177,30 +166,20 @@ export default class WorkspaceController {
177166

178167
const workspaces = await user.related('workspaces').query();
179168
// Convert to array with all data Franz wants
180-
let workspacesArray: Workspace[] = [];
169+
let workspacesArray: object[] = [];
181170
if (workspaces) {
182-
workspacesArray = workspaces.map((workspace: Workspace) => {
183-
let data: CustomData = {};
184-
try {
185-
data = JSON.parse(workspace.data);
186-
} catch (error) {
187-
console.warn(
188-
`[WorkspaceController] list ${workspace.workspaceId}. Error parsing data JSON`,
189-
error,
190-
);
191-
}
192-
return {
193-
id: workspace.workspaceId,
194-
name: workspace.name,
195-
order: workspace.order,
196-
services:
197-
typeof workspace.services === 'string'
198-
? JSON.parse(workspace.services)
199-
: workspace.services,
200-
userId: user.id,
201-
iconUrl: data.iconUrl ?? '',
202-
};
203-
});
171+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
172+
workspacesArray = workspaces.map((workspace: any) => ({
173+
id: workspace.workspaceId,
174+
name: workspace.name,
175+
iconUrl: workspace.iconUrl,
176+
order: workspace.order,
177+
services:
178+
typeof workspace.services === 'string'
179+
? JSON.parse(workspace.services)
180+
: workspace.services,
181+
userId: user.id,
182+
}));
204183
}
205184

206185
return response.send(workspacesArray);

app/Models/Workspace.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,14 @@ export default class Workspace extends BaseModel {
3131
public services: string;
3232

3333
@column()
34-
public data: string; // JSON string that match type CustomData
34+
public data: string;
35+
36+
@column()
37+
public iconUrl: string;
3538

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

3942
@column.dateTime({ autoCreate: true, autoUpdate: true })
4043
public updatedAt: DateTime;
4144
}
42-
43-
export interface CustomData {
44-
name?: string;
45-
iconUrl?: string;
46-
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import BaseSchema from '@ioc:Adonis/Lucid/Schema'
2+
3+
export default class extends BaseSchema {
4+
protected tableName = 'workspaces'
5+
6+
public async up () {
7+
this.schema.alterTable(this.tableName, (table) => {
8+
table.string('icon_url').nullable()
9+
})
10+
}
11+
12+
public async down () {
13+
this.schema.alterTable(this.tableName, (table) => {
14+
table.dropColumn('icon_url')
15+
})
16+
}
17+
}

0 commit comments

Comments
 (0)