From f6351f4f4e5151498c6c2495f23fd92c2144f741 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Wed, 23 Mar 2022 22:41:22 +0300 Subject: [PATCH 1/5] added Font model and migration --- altrpnjs/app/Models/Font.ts | 16 +++++++++++++++ .../migrations/1648057987363_fonts.ts | 20 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 altrpnjs/app/Models/Font.ts create mode 100644 altrpnjs/database/migrations/1648057987363_fonts.ts diff --git a/altrpnjs/app/Models/Font.ts b/altrpnjs/app/Models/Font.ts new file mode 100644 index 000000000..45e46e77a --- /dev/null +++ b/altrpnjs/app/Models/Font.ts @@ -0,0 +1,16 @@ +import { BaseModel, column } from '@ioc:Adonis/Lucid/Orm' + +export default class Font extends BaseModel { + @column({ isPrimary: true }) + public id: number + + @column() + public font_family: string + + @column() + public description: string + + @column() + public guid: string + +} diff --git a/altrpnjs/database/migrations/1648057987363_fonts.ts b/altrpnjs/database/migrations/1648057987363_fonts.ts new file mode 100644 index 000000000..fac959227 --- /dev/null +++ b/altrpnjs/database/migrations/1648057987363_fonts.ts @@ -0,0 +1,20 @@ +import BaseSchema from '@ioc:Adonis/Lucid/Schema' + +export default class Fonts extends BaseSchema { + protected tableName = 'altrp_fonts' + + public async up () { + this.schema.createTable(this.tableName, (table) => { + table.increments('id') + table.string("font_family").notNullable() + table.text("description").nullable() + table.string("guid").notNullable() + table.unique(["font_family"]) + table.unique(["guid"]) + }) + } + + public async down () { + this.schema.dropTable(this.tableName) + } +} From 934516f85d9d8285fecf5397a068fe38b13be35f Mon Sep 17 00:00:00 2001 From: Dmitry Date: Wed, 23 Mar 2022 22:42:43 +0300 Subject: [PATCH 2/5] bug fix when media migration rollback --- .../1645090117365_add_variations_to_media_tables.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/altrpnjs/database/migrations/1645090117365_add_variations_to_media_tables.ts b/altrpnjs/database/migrations/1645090117365_add_variations_to_media_tables.ts index 52c531815..cac41ff9a 100644 --- a/altrpnjs/database/migrations/1645090117365_add_variations_to_media_tables.ts +++ b/altrpnjs/database/migrations/1645090117365_add_variations_to_media_tables.ts @@ -10,6 +10,8 @@ export default class AddVariationsToMediaTables extends BaseSchema { } public async down () { - this.schema.dropTable(this.tableName) + this.schema.alterTable(this.tableName, (table) => { + table.dropColumn("media_variation") + }) } } From 76d3f4d5e99aac4767108051e4d23fb4efcf0925 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Fri, 25 Mar 2022 10:41:04 +0300 Subject: [PATCH 3/5] CRUD Fonts --- .../Controllers/Http/admin/FontsController.ts | 145 ++++++++++++++++++ altrpnjs/app/Models/Font.ts | 12 +- 2 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 altrpnjs/app/Controllers/Http/admin/FontsController.ts diff --git a/altrpnjs/app/Controllers/Http/admin/FontsController.ts b/altrpnjs/app/Controllers/Http/admin/FontsController.ts new file mode 100644 index 000000000..a5dd9c0b9 --- /dev/null +++ b/altrpnjs/app/Controllers/Http/admin/FontsController.ts @@ -0,0 +1,145 @@ +import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' +import Font from 'App/Models/Font'; +import validGuid from '../../../../helpers/validGuid'; +import guid from "../../../../helpers/guid"; + +export default class FontsController { + + public async store({request, response}: HttpContextContract) { + + let font = new Font() + font.fill(request.all()) + font.guid = guid() + + try { + await font.save() + } catch (e) { + return response.json({ + 'success': + false, + 'message': + 'Customizer don\'t saved', + 'throw message': e.message, + 'trace': e.stack.split('\n'), + }, + ) + } + return response.json({ + 'success': true, + 'data': font + }, + ) + } + + public async show({params, response}: HttpContextContract) { + let font + if (validGuid(params.id)) { + font = await font.query().where('guid', params.id).first() + } else { + font = await Font.find(params.id) + } + if (!font) { + return response.json({ + 'success': false, + 'message': 'Font not found' + }, + ) + } + return response.json({ + 'success': true, + 'data': font + },) + } + + public async update({params, request, response}: HttpContextContract) { + let font = await Font.find(params.id) + if (!font) { + return response.json({ + 'success': false, + 'message': 'Font not found' + }, + ) + } + font.merge(request.all()) + try { + await font.save() + } catch (e) { + console.trace(e); + response.status(500) + return response.json({ + 'success': false, + 'message': 'Font could not be saved', + 'throw message': e.message, + 'trace': e.stack.split('\n'), + }, + ) + } + return response.json({ + 'success': true, + 'data': font.serialize() + }) + } + + public async index({request, response}: HttpContextContract) { + + let search = request.qs().s || '' + let categories = request.qs() || '' + let orderColumn = request.qs().order_by || 'font_family' + let orderType: 'asc' | 'desc' = request.qs()?.order ? request.qs().order.toLowerCase() : 'asc' + + let fonts = Font.query().preload('categories') + if (fonts && _.isString(categories)) { + categories = categories.split(',') + fonts.leftJoin('altrp_category_objects', + 'altrp_category_objects.object_guid', + '=', + 'altrp_fonts.guid') + // @ts-ignore + fonts.whereIn('altrp_category_objects.category_guid', categories) + } + + if (search) { + fonts.where(function (query) { + query.where('altrp_fonts.font_family', 'like', '%' + search + '%') + .orWhere('altrp_fonts.id', 'like', '%' + search + '%') + }) + } + fonts.orderBy(orderColumn, orderType) + const result = await fonts.select('altrp_fonts.*') + + return response.json({ + 'success': + true, + 'data': + result, + }) + } + + public async destroy({params, response}: HttpContextContract) { + let font + if (validGuid(params.id)) { + font = await Font.query().where('guid', params.id).first() + } else { + font = await Font.find(params.id) + } + if (!font) { + return response.json({ + 'success': false, + 'message': 'Font not found' + },) + } + try { + await font.delete() + } catch (e) { + return response.json({ + 'success': false, + 'throw message': e.message, + 'trace': e.stack.split('\n'), + 'message': 'Font could not be deleted' + }, + ) + } + return response.json({'success': true,},) + } + +} diff --git a/altrpnjs/app/Models/Font.ts b/altrpnjs/app/Models/Font.ts index 45e46e77a..12d336eac 100644 --- a/altrpnjs/app/Models/Font.ts +++ b/altrpnjs/app/Models/Font.ts @@ -1,4 +1,5 @@ -import { BaseModel, column } from '@ioc:Adonis/Lucid/Orm' +import {BaseModel, column, ManyToMany, manyToMany} from "@ioc:Adonis/Lucid/Orm"; +import Category from "App/Models/Category"; export default class Font extends BaseModel { @column({ isPrimary: true }) @@ -13,4 +14,13 @@ export default class Font extends BaseModel { @column() public guid: string + @manyToMany(() => Category, { + pivotTable: 'altrp_category_objects', + relatedKey: 'guid', + localKey: 'guid', + pivotForeignKey: 'object_guid', + pivotRelatedForeignKey: 'category_guid', + }) + public categories: ManyToMany + } From ddf4de1dad0c28db2424ca8f7f671dc627c6b19d Mon Sep 17 00:00:00 2001 From: Dmitry Date: Sun, 27 Mar 2022 23:20:51 +0300 Subject: [PATCH 4/5] CRUD Font Settings --- .../Http/admin/FontSettingsController.ts | 106 ++++++++++++++++++ .../Controllers/Http/admin/FontsController.ts | 3 +- altrpnjs/app/Models/Font.ts | 12 +- altrpnjs/app/Models/FontSetting.ts | 34 ++++++ .../migrations/1648399487179_font_settings.ts | 23 ++++ 5 files changed, 176 insertions(+), 2 deletions(-) create mode 100644 altrpnjs/app/Controllers/Http/admin/FontSettingsController.ts create mode 100644 altrpnjs/app/Models/FontSetting.ts create mode 100644 altrpnjs/database/migrations/1648399487179_font_settings.ts diff --git a/altrpnjs/app/Controllers/Http/admin/FontSettingsController.ts b/altrpnjs/app/Controllers/Http/admin/FontSettingsController.ts new file mode 100644 index 000000000..b137c909e --- /dev/null +++ b/altrpnjs/app/Controllers/Http/admin/FontSettingsController.ts @@ -0,0 +1,106 @@ +import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' +import FontSetting from 'App/Models/FontSetting'; + +export default class FontSettingSettingsController { + + public async store({request, response}: HttpContextContract) { + + let fontSetting = new FontSetting() + fontSetting.fill(request.all()) + + try { + await fontSetting.save() + } catch (e) { + return response.json({ + 'success': + false, + 'message': + 'Font Setting don\'t saved', + 'throw message': e.message, + 'trace': e.stack.split('\n'), + }, + ) + } + return response.json({ + 'success': true, + 'data': fontSetting + }, + ) + } + + public async show({params, response}: HttpContextContract) { + let fontSetting = await FontSetting.find(params.id) + if (!fontSetting) { + return response.json({ + 'success': false, + 'message': 'Font Setting not found' + }, + ) + } + return response.json({ + 'success': true, + 'data': fontSetting + },) + } + + public async update({params, request, response}: HttpContextContract) { + let fontSetting = await FontSetting.find(params.id) + if (!fontSetting) { + return response.json({ + 'success': false, + 'message': 'Font Setting not found' + }, + ) + } + fontSetting.merge(request.all()) + try { + await fontSetting.save() + } catch (e) { + response.status(500) + return response.json({ + 'success': false, + 'message': 'Font Setting could not be saved', + 'throw message': e.message, + 'trace': e.stack.split('\n'), + }, + ) + } + return response.json({ + 'success': true, + 'data': fontSetting.serialize() + }) + } + + public async index({params, response}: HttpContextContract) { + + let fontSettings = FontSetting.query().where('font_guid', params.guid) + + return response.json({ + 'success': true, + 'data': fontSettings + }) + } + + public async destroy({params, response}: HttpContextContract) { + let fontSetting = await FontSetting.find(params.id) + if (!fontSetting) { + return response.json({ + 'success': false, + 'message': 'FontSetting not found' + },) + } + try { + await fontSetting.delete() + } catch (e) { + return response.json({ + 'success': false, + 'throw message': e.message, + 'trace': e.stack.split('\n'), + 'message': 'Font Setting could not be deleted' + }, + ) + } + return response.json({'success': true,}) + } + +} diff --git a/altrpnjs/app/Controllers/Http/admin/FontsController.ts b/altrpnjs/app/Controllers/Http/admin/FontsController.ts index a5dd9c0b9..ee31ff64b 100644 --- a/altrpnjs/app/Controllers/Http/admin/FontsController.ts +++ b/altrpnjs/app/Controllers/Http/admin/FontsController.ts @@ -18,7 +18,7 @@ export default class FontsController { 'success': false, 'message': - 'Customizer don\'t saved', + 'Font don\'t saved', 'throw message': e.message, 'trace': e.stack.split('\n'), }, @@ -38,6 +38,7 @@ export default class FontsController { } else { font = await Font.find(params.id) } + await font.load('fontSettings') if (!font) { return response.json({ 'success': false, diff --git a/altrpnjs/app/Models/Font.ts b/altrpnjs/app/Models/Font.ts index 12d336eac..a48256b38 100644 --- a/altrpnjs/app/Models/Font.ts +++ b/altrpnjs/app/Models/Font.ts @@ -1,7 +1,11 @@ -import {BaseModel, column, ManyToMany, manyToMany} from "@ioc:Adonis/Lucid/Orm"; +import {BaseModel, column, HasMany, hasMany, ManyToMany, manyToMany} from "@ioc:Adonis/Lucid/Orm"; import Category from "App/Models/Category"; +import FontSetting from "App/Models/FontSetting"; export default class Font extends BaseModel { + + public static table = 'altrp_fonts' + @column({ isPrimary: true }) public id: number @@ -23,4 +27,10 @@ export default class Font extends BaseModel { }) public categories: ManyToMany + @hasMany(() => FontSetting, { + localKey: 'guid', + foreignKey: 'font_guid' + }) + public fontSettings: HasMany + } diff --git a/altrpnjs/app/Models/FontSetting.ts b/altrpnjs/app/Models/FontSetting.ts new file mode 100644 index 000000000..5ef570c39 --- /dev/null +++ b/altrpnjs/app/Models/FontSetting.ts @@ -0,0 +1,34 @@ +import { BaseModel, BelongsTo, belongsTo, column } from '@ioc:Adonis/Lucid/Orm' +import Font from "App/Models/Font"; + +export default class FontSetting extends BaseModel { + + public static table = 'altrp_font_settings' + + @column({ isPrimary: true }) + public id: number + + @column() + public src: string + + @column() + public font_weight: string + + @column() + public font_style: string + + @column() + public font_variant: string + + @column() + public font_stretch: string + + @column() + public font_guid: string + + @belongsTo(() => Font, { + foreignKey: 'font_guid' + }) + public altrp_font: BelongsTo + +} diff --git a/altrpnjs/database/migrations/1648399487179_font_settings.ts b/altrpnjs/database/migrations/1648399487179_font_settings.ts new file mode 100644 index 000000000..c1c6b94a0 --- /dev/null +++ b/altrpnjs/database/migrations/1648399487179_font_settings.ts @@ -0,0 +1,23 @@ +import BaseSchema from '@ioc:Adonis/Lucid/Schema' + +export default class FontSettings extends BaseSchema { + protected tableName = 'altrp_font_settings' + + public async up () { + this.schema.createTable(this.tableName, (table) => { + table.increments('id') + table.json("src").notNullable() + table.string("font_weight").nullable() + table.string("font_style").nullable() + table.string("font_variant").nullable() + table.string("font_stretch").nullable() + table.string("font_guid").notNullable() + + table.foreign('font_guid').references('altrp_fonts.guid') + }) + } + + public async down () { + this.schema.dropTable(this.tableName) + } +} From c25648ea4e86cbe14648055db9da5f551ecc1971 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Fri, 1 Apr 2022 23:27:45 +0300 Subject: [PATCH 5/5] bug fix for creating foreign key --- .../Http/admin/RelationshipsController.ts | 105 +++++++++++++----- 1 file changed, 77 insertions(+), 28 deletions(-) diff --git a/altrpnjs/app/Controllers/Http/admin/RelationshipsController.ts b/altrpnjs/app/Controllers/Http/admin/RelationshipsController.ts index 9a7f19d08..00535a104 100644 --- a/altrpnjs/app/Controllers/Http/admin/RelationshipsController.ts +++ b/altrpnjs/app/Controllers/Http/admin/RelationshipsController.ts @@ -56,14 +56,25 @@ export default class RelationshipsController { if (targetModel) { await targetModel.load('table') - let query = `ALTER TABLE ${model.table.name} ADD CONSTRAINT - ${relationshipData.name} - FOREIGN KEY (${relationshipData.local_key}) - REFERENCES ${targetModel.table.name}(${relationshipData.foreign_key}) - ON DELETE ${relationshipData.onDelete} - ON UPDATE ${relationshipData.onUpdate}` - - await Database.rawQuery(query) + let query + + if (relationshipData.type == "belongsTo") { + query = `ALTER TABLE ${model.table.name} ADD CONSTRAINT + ${model.table.name}_${relationshipData.local_key}_foreign + FOREIGN KEY (${relationshipData.local_key}) + REFERENCES ${targetModel.table.name}(${relationshipData.foreign_key}) + ON DELETE ${relationshipData.onDelete} + ON UPDATE ${relationshipData.onUpdate}` + } else if(relationshipData.type == "hasMany" || relationshipData.type == "hasOne") { + query = `ALTER TABLE ${targetModel.table.name} ADD CONSTRAINT + ${targetModel.table.name}_${relationshipData.foreign_key}_foreign + FOREIGN KEY (${relationshipData.foreign_key}) + REFERENCES ${model.table.name}(${relationshipData.local_key}) + ON DELETE ${relationshipData.onDelete} + ON UPDATE ${relationshipData.onUpdate}` + } + + await Database.rawQuery(query) } return response.json({success: true, data: relationship}) @@ -80,8 +91,11 @@ export default class RelationshipsController { }) } + + let relationshipData = request.all() + //const relationship = await Relationship.find(params.field_id) - const relationship = await Relationship.find(params.id) + const relationship = await Relationship.find(relationshipData.id) if (!relationship) { response.status(404) return response.json({ @@ -90,7 +104,22 @@ export default class RelationshipsController { }) } - let relationshipData = request.all() + + let targetModel = await Model.find(relationship.target_model_id) + if (targetModel) { + await model.load('table') + await targetModel.load('table') + + let deleteQuery + if (relationship.type == "belongsTo") { + deleteQuery = `ALTER TABLE ${model.table.name} DROP FOREIGN KEY ${model.table.name}_${relationship.local_key}_foreign` + } else if(relationship.type == "hasMany" || relationship.type == "hasOne") { + deleteQuery = `ALTER TABLE ${targetModel.table.name} DROP FOREIGN KEY ${targetModel.table.name}_${relationship.foreign_key}_foreign` + } + await Database.rawQuery(deleteQuery) + } + + relationship.merge({ title: relationshipData.title, description: relationshipData.description, @@ -111,22 +140,33 @@ export default class RelationshipsController { await relationship.save() Event.emit('model:updated', model) - let targetModel = await Model.find(relationshipData.target_model_id) - if (targetModel) { - await model.load('table') - let createQuery = `ALTER TABLE ${model.table.name} DROP FOREIGN KEY ${relationship.name}` - await Database.rawQuery(createQuery) + let newTargetModel = await Model.find(relationshipData.target_model_id) + if (newTargetModel) { - await targetModel.load('table') + await newTargetModel.load('table') + + let query + if (relationshipData.type == "belongsTo") { + + query = `ALTER TABLE ${model.table.name} ADD CONSTRAINT + ${model.table.name}_${relationshipData.local_key}_foreign + FOREIGN KEY (${relationshipData.local_key}) + REFERENCES ${newTargetModel.table.name}(${relationshipData.foreign_key}) + ON DELETE ${relationshipData.onDelete} + ON UPDATE ${relationshipData.onUpdate}` + + } else if(relationshipData.type == "hasMany" || relationshipData.type == "hasOne") { - let deleteQuery = `ALTER TABLE ${model.table.name} ADD CONSTRAINT - ${relationshipData.name} - FOREIGN KEY (${relationshipData.local_key}) - REFERENCES ${targetModel.table.name}(${relationshipData.foreign_key}) - ON DELETE ${relationshipData.onDelete} - ON UPDATE ${relationshipData.onUpdate}` + query = `ALTER TABLE ${newTargetModel.table.name} ADD CONSTRAINT + ${newTargetModel.table.name}_${relationshipData.foreign_key}_foreign + FOREIGN KEY (${relationshipData.foreign_key}) + REFERENCES ${model.table.name}(${relationshipData.local_key}) + ON DELETE ${relationshipData.onDelete} + ON UPDATE ${relationshipData.onUpdate}` + + } + await Database.rawQuery(query) - await Database.rawQuery(deleteQuery) } return response.json({success: true, data: relationship}) @@ -176,18 +216,27 @@ export default class RelationshipsController { }) } + + await model.load('table') + let targetModel = await Model.find(relationship.target_model_id) if(targetModel){ Event.emit('model:updated', targetModel) + + await targetModel.load('table') + + let deleteQuery + if (relationship.type == "belongsTo") { + deleteQuery = `ALTER TABLE ${model.table.name} DROP FOREIGN KEY ${model.table.name}_${relationship.local_key}_foreign` + } else if(relationship.type == "hasMany" || relationship.type == "hasOne") { + deleteQuery = `ALTER TABLE ${targetModel.table.name} DROP FOREIGN KEY ${targetModel.table.name}_${relationship.foreign_key}_foreign` + } + await Database.rawQuery(deleteQuery) } + await relationship.delete() Event.emit('model:updated', model) - await model.load('table') - - let query = `ALTER TABLE ${model.table.name} DROP FOREIGN KEY ${relationship.name}` - await Database.rawQuery(query) - return response.json({success: true,}) }