Skip to content

Commit 2713c77

Browse files
committed
fix: resolve apartment image upload issue and implement image generation
1 parent 553b7de commit 2713c77

File tree

5 files changed

+30
-12
lines changed

5 files changed

+30
-12
lines changed

live-demo/app/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import usersResource from "./resources/users";
66
import apartmentsResource from "./resources/apartments";
77
import auditLogsResource from "./resources/auditLogs"
88
import translations from "./resources/translations";
9-
9+
import { randomUUID } from 'crypto';
1010
try { fs.mkdirSync('db') } catch (e) {}
1111

1212

@@ -113,7 +113,7 @@ async function seedDatabase() {
113113
}
114114
for (let i = 0; i <= 100; i++) {
115115
await admin.resource('aparts').create({
116-
id: `${i}`,
116+
id: randomUUID(),
117117
title: `Apartment ${i}`,
118118
square_meter: (Math.random() * 100).toFixed(1),
119119
price: (Math.random() * 10000).toFixed(2),

live-demo/app/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
"start": "tsx --env-file=.env --watch index.ts",
88
"test": "echo \"Error: no test specified\" && exit 1",
99
"bundleNow": "tsx bundleNow.ts",
10-
"startLive": "prisma migrate deploy && tsx index.ts"
10+
"startLive": "prisma migrate deploy && tsx index.ts",
11+
"migrate": "prisma migrate deploy",
12+
"makemigration": "prisma migrate dev"
1113
},
1214
"keywords": [],
1315
"license": "ISC",

live-demo/app/resources/apartments.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ import importExport from '@adminforth/import-export';
33
import RichEditorPlugin from '@adminforth/rich-editor';
44
import ChatGptPlugin from '@adminforth/chat-gpt';
55
import UploadPlugin from '@adminforth/upload';
6-
7-
8-
6+
import { randomUUID } from 'crypto';
97

108

119
const blockDemoUsers = async ({ record, adminUser, resource }) => {
@@ -37,9 +35,9 @@ export default {
3735
{
3836
name: 'id',
3937
label: 'Identifier', // if you wish you can redefine label, defaulted to uppercased name
40-
showIn: ['filter', 'show'], // show column in filter and in show page
38+
showIn: {all: false, show: true, filter: true}, // show column in filter and in show page
4139
primaryKey: true,
42-
fillOnCreate: ({initialRecord, adminUser}) => Math.random().toString(36).substring(7), // called during creation to generate content of field, initialRecord is values user entered, adminUser object of user who creates record
40+
fillOnCreate: ({initialRecord, adminUser}) => randomUUID(), // called during creation to generate content of field, initialRecord is values user entered, adminUser object of user who creates record
4341
},
4442
{
4543
name: 'title',
@@ -90,7 +88,7 @@ export default {
9088
},
9189
{
9290
name: 'apartment_image',
93-
showIn: [], // You can set to ['list', 'show'] if you wish to show path column in list and show views
91+
showIn: {list: true, show: true, edit: true, create: true}, // You can set to ['list', 'show'] if you wish to show path column in list and show views
9492
},
9593
{
9694
name: 'created_at',
@@ -170,8 +168,23 @@ export default {
170168
allowedFileExtensions: ['jpg', 'jpeg', 'png', 'gif', 'webm', 'webp'],
171169
maxFileSize: 1024 * 1024 * 20, // 5MB
172170
s3Path: ({originalFilename, originalExtension, contentType}) =>
173-
`aparts/${new Date().getFullYear()}/${uuid()}-${originalFilename}.${originalExtension}`,
171+
`aparts/${new Date().getFullYear()}/${randomUUID()}-${originalFilename}.${originalExtension}`,
174172
// You can use next to change preview URLs (if it is image) in list and show views
173+
generation: {
174+
provider: "openai-dall-e",
175+
countToGenerate: 2,
176+
openAiOptions: {
177+
model: "dall-e-3",
178+
size: "1792x1024",
179+
apiKey: process.env.OPENAI_API_KEY as string,
180+
},
181+
fieldsForContext: ["title"],
182+
rateLimit: {
183+
limit: "2/1m",
184+
errorMessage:
185+
"For demo purposes, you can generate only 2 images per minute",
186+
},
187+
},
175188
preview: {
176189
showInList: true,
177190
previewUrl: ({s3Path}) => `https://demo-static.adminforth.dev/${s3Path}`,

live-demo/app/resources/translations.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export default {
8585
{
8686
name: "created_at",
8787
fillOnCreate: ({ initialRecord, adminUser }: any) => new Date().toISOString(),
88+
showIn: {show: true, filter: true, list: true, edit: false, create: false},
8889
},
8990
{
9091
name: "uk_string",

live-demo/app/resources/users.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import AdminForth, { AdminForthDataTypes, AdminForthResourceInput } from 'adminforth';
1+
import AdminForth, { AdminForthDataTypes, AdminForthResourceColumn, AdminForthResourceInput } from 'adminforth';
22
import importExport from '@adminforth/import-export';
33
import ForeignInlineListPlugin from '@adminforth/foreign-inline-list';
4+
import { randomUUID } from 'crypto';
45

56
const blockDemoUsers = async ({ record, adminUser, resource }) => {
67
if (adminUser.dbUser && adminUser.dbUser.role !== 'superadmin') {
@@ -32,8 +33,9 @@ export default {
3233
columns: [
3334
{
3435
name: 'id',
36+
label: 'Identifier',
3537
primaryKey: true,
36-
fillOnCreate: ({initialRecord, adminUser}) => Math.random().toString(36).substring(7),
38+
fillOnCreate: ({ initialRecord, adminUser }: any) => randomUUID(),
3739
showIn: ['list', 'filter', 'show'],
3840
},
3941
{

0 commit comments

Comments
 (0)