Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

212 did someone wanted themes #241

Merged
merged 4 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions db/migrations/1681393317164-Data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = class Data1681393317164 {
name = 'Data1681393317164'

async up(db) {
await db.query(`CREATE TABLE "theme" ("id" character varying NOT NULL, "name" text NOT NULL, "theme_color1" text, "theme_color2" text, "theme_color3" text, "theme_color4" text, "base_id" character varying, CONSTRAINT "PK_c1934d0b4403bf10c1ab0c18166" PRIMARY KEY ("id"))`)
await db.query(`CREATE INDEX "IDX_52c219cbfa98d6011518283433" ON "theme" ("base_id") `)
await db.query(`ALTER TABLE "theme" ADD CONSTRAINT "FK_52c219cbfa98d6011518283433d" FOREIGN KEY ("base_id") REFERENCES "base"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`)
}

async down(db) {
await db.query(`DROP TABLE "theme"`)
await db.query(`DROP INDEX "public"."IDX_52c219cbfa98d6011518283433"`)
await db.query(`ALTER TABLE "theme" DROP CONSTRAINT "FK_52c219cbfa98d6011518283433d"`)
}
}
12 changes: 11 additions & 1 deletion schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,22 @@ type Base @entity {
meta: MetadataEntity
metadata: String
# parts: [BasePart!] @derivedFrom(field: "base")
# themes: [BaseTheme!] @derivedFrom(field: "base")
themes: [Theme!] @derivedFrom(field: "base")
# attachedToResources: [Resource!] @derivedFrom (field: "base")
# logs: [Call!] @derivedFrom (field: "base") // Rename to event
}


type Theme @entity {
id: ID!
name: String!
base: Base!
themeColor1: String
themeColor2: String
themeColor3: String
themeColor4: String
}

# https://github.com/rmrk-team/rmrk-spec/blob/master/standards/rmrk2.0.0/entities/nft.md#resources-and-resource
type Resource @entity {
id: ID!
Expand Down
32 changes: 17 additions & 15 deletions src/mappings/v2/addTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import { Optional } from '@kodadot1/metasquid/types'
import { ThemeAdd, toThemeId } from '@kodadot1/minimark/v2'

import { getOrFail as get } from '@kodadot1/metasquid/entity'
import { Base } from '../../model'
import { Base, Theme } from '../../model'
import { unwrap } from '../utils'
import logger, { logError } from '../utils/logger'
import { isOwnerOrElseError } from '../utils/consolidator'
import { camelCase } from '../utils/helper'
import logger, { logError, success } from '../utils/logger'
import { Action, Context } from '../utils/types'
import { createUnlessNotExist } from '../utils/verbose'
import { getThemeAdd } from './getters'

const OPERATION = Action.THEMEADD
Expand All @@ -18,29 +21,28 @@ export async function addTheme(context: Context) {
const { value: interaction, caller, timestamp, blockNumber, version } = unwrap(context, getter)

const base = await get<Base>(context.store, Base, interaction.base_id)
isOwnerOrElseError(base, caller)

const id = toThemeId(interaction.base_id, interaction.name)
// const final = await createUnlessNotExist(id, Base, context)
const final = await createUnlessNotExist(id, Theme, context)



// theme_color_1: String
// theme_color_2: String
// theme_color_3: String
// theme_color_4: String
const keys = [ 'theme_color_1', 'theme_color_2', 'theme_color_3', 'theme_color_4' ]
final.name = interaction.name
final.base = base

const keys = ['theme_color_1', 'theme_color_2', 'theme_color_3', 'theme_color_4']

for (const key of keys) {
const value = interaction.value[key]
// base[key] = interaction[key]
if (value && key !== 'base') {
const camelKey = camelCase(key) as keyof Omit<Theme, 'base'>
final[camelKey] = value
}

}
// logger.success(`[${OPERATION}] NEW PRIORITY ${interaction.value} for ${nft.id} from ${caller}`)

// TODO: add logic for accepting resource

logger.info(`[${OPERATION}] < ${interaction.name}, ${interaction.value} > for ${interaction.base_id} from ${caller}`)
// await context.store.save(nft)
success(OPERATION, `< ${interaction.name}, ${interaction.value} > for ${interaction.base_id} from ${caller}`)
await context.store.save(final)
// await createEvent(nft, OPERATION, { blockNumber, caller, timestamp, version }, `${interaction.value.at(0)}`, context.store)
} catch (e) {
logError(e, (e) => logger.warn(`[${OPERATION}] ${e.message} ${JSON.stringify(interaction)}`))
Expand Down
5 changes: 4 additions & 1 deletion src/mappings/v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { lockCollection } from './lock'
import { mintItem } from './mint'
import { send } from './send'
import { setPriority } from './setpriority'
import { addTheme } from './addTheme'

export async function mainFrame(remark: string, context: Context): Promise<void> {
const base = unwrap(context, (_: Context) => ({ value: remark }))
Expand Down Expand Up @@ -63,8 +64,10 @@ export async function mainFrame(remark: string, context: Context): Promise<void>
case Interaction.LOCK:
await lockCollection(context)
break
case Interaction.DESTROY:
case Interaction.THEMEADD:
await addTheme(context)
break
case Interaction.DESTROY:
case Interaction.EQUIPPABLE:
case Interaction.SETPROPERTY:
case Interaction.EQUIP:
Expand Down
6 changes: 5 additions & 1 deletion src/model/generated/base.model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, ManyToOne as ManyToOne_, Index as Index_} from "typeorm"
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, ManyToOne as ManyToOne_, Index as Index_, OneToMany as OneToMany_} from "typeorm"
import {BaseType} from "./_baseType"
import {MetadataEntity} from "./metadataEntity.model"
import {Theme} from "./theme.model"

@Entity_()
export class Base {
Expand Down Expand Up @@ -29,4 +30,7 @@ export class Base {

@Column_("text", {nullable: true})
metadata!: string | undefined | null

@OneToMany_(() => Theme, e => e.base)
themes!: Theme[]
}
1 change: 1 addition & 0 deletions src/model/generated/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * from "./_interaction"
export * from "./emote.model"
export * from "./base.model"
export * from "./_baseType"
export * from "./theme.model"
export * from "./resource.model"
export * from "./series.model"
export * from "./spotlight.model"
Expand Down
31 changes: 31 additions & 0 deletions src/model/generated/theme.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, ManyToOne as ManyToOne_, Index as Index_} from "typeorm"
import {Base} from "./base.model"

@Entity_()
export class Theme {
constructor(props?: Partial<Theme>) {
Object.assign(this, props)
}

@PrimaryColumn_()
id!: string

@Column_("text", {nullable: false})
name!: string

@Index_()
@ManyToOne_(() => Base, {nullable: true})
base!: Base

@Column_("text", {nullable: true})
themeColor1!: string | undefined | null

@Column_("text", {nullable: true})
themeColor2!: string | undefined | null

@Column_("text", {nullable: true})
themeColor3!: string | undefined | null

@Column_("text", {nullable: true})
themeColor4!: string | undefined | null
}