Browse Source

CLI: add commands for categories deletion

iorveth 4 years ago
parent
commit
19f64ed8b0

+ 1 - 1
cli/examples/content/CreateCategory.json

@@ -1,5 +1,5 @@
 {
   "meta": {
-    "name": "Nature",
+    "name": "Nature"
   }
 }

+ 1 - 1
cli/examples/content/UpdateCategory.json

@@ -1,5 +1,5 @@
 {
   "meta": {
-    "name": "Science",
+    "name": "Science"
   }
 }

+ 19 - 1
cli/src/Api.ts

@@ -48,7 +48,15 @@ import { RewardRelationship, RewardRelationshipId } from '@joystream/types/recur
 import { Stake, StakeId } from '@joystream/types/stake'
 
 import { InputValidationLengthConstraint, ChannelId, Url } from '@joystream/types/common'
-import { CuratorGroup, CuratorGroupId, Channel, Video, VideoId } from '@joystream/types/content'
+import {
+  CuratorGroup,
+  CuratorGroupId,
+  Channel,
+  Video,
+  VideoId,
+  ChannelCategory,
+  VideoCategory,
+} from '@joystream/types/content'
 import { ContentId, DataObject } from '@joystream/types/storage'
 import { ServiceProviderRecord } from '@joystream/types/discovery'
 import _ from 'lodash'
@@ -538,6 +546,16 @@ export default class Api {
     return exists ? await this._api.query.content.videoById<Video>(videoId) : null
   }
 
+  async channelCategoryById(channelCategoryId: number): Promise<ChannelCategory | null> {
+    const exists = !!(await this._api.query.content.channelCategoryById.size(channelCategoryId)).toNumber()
+    return exists ? await this._api.query.content.channelCategoryById<ChannelCategory>(channelCategoryId) : null
+  }
+
+  async videoCategoryById(videoCategoryId: number): Promise<VideoCategory | null> {
+    const exists = !!(await this._api.query.content.videoCategoryById.size(videoCategoryId)).toNumber()
+    return exists ? await this._api.query.content.videoCategoryById<VideoCategory>(videoCategoryId) : null
+  }
+
   async dataByContentId(contentId: ContentId): Promise<DataObject | null> {
     const dataObject = await this._api.query.dataDirectory.dataByContentId<DataObject>(contentId)
     return dataObject.isEmpty ? null : dataObject

+ 39 - 0
cli/src/commands/content/deleteChannelCategory.ts

@@ -0,0 +1,39 @@
+import ContentDirectoryCommandBase from '../../base/ContentDirectoryCommandBase'
+
+export default class DeleteChannelCategoryCommand extends ContentDirectoryCommandBase {
+  static description = 'Delete channel category.'
+  static flags = {
+    context: ContentDirectoryCommandBase.categoriesContextFlag,
+  }
+
+  static args = [
+    {
+      name: 'channelCategoryId',
+      required: true,
+      description: 'ID of the Channel Category',
+    },
+  ]
+
+  async run() {
+    let { context } = this.parse(DeleteChannelCategoryCommand).flags
+
+    const { channelCategoryId } = this.parse(DeleteChannelCategoryCommand).args
+
+    const channelCategory = await this.getApi().channelCategoryById(channelCategoryId)
+
+    if (channelCategory) {
+      if (!context) {
+        context = await this.promptForCategoriesContext()
+      }
+
+      const currentAccount = await this.getRequiredSelectedAccount()
+      await this.requestAccountDecoding(currentAccount)
+
+      const actor = await this.getActor(context)
+
+      await this.sendAndFollowNamedTx(currentAccount, 'content', 'deleteChannelCategory', [actor, channelCategoryId])
+    } else {
+      this.error('Channel category under given id does not exist...')
+    }
+  }
+}

+ 39 - 0
cli/src/commands/content/deleteVideoCategory.ts

@@ -0,0 +1,39 @@
+import ContentDirectoryCommandBase from '../../base/ContentDirectoryCommandBase'
+
+export default class DeleteVideoCategoryCommand extends ContentDirectoryCommandBase {
+  static description = 'Delete video category.'
+  static flags = {
+    context: ContentDirectoryCommandBase.categoriesContextFlag,
+  }
+
+  static args = [
+    {
+      name: 'videoCategoryId',
+      required: true,
+      description: 'ID of the Video Category',
+    },
+  ]
+
+  async run() {
+    let { context } = this.parse(DeleteVideoCategoryCommand).flags
+
+    const { videoCategoryId } = this.parse(DeleteVideoCategoryCommand).args
+
+    const videoCategory = await this.getApi().videoCategoryById(videoCategoryId)
+
+    if (videoCategory) {
+      if (!context) {
+        context = await this.promptForCategoriesContext()
+      }
+
+      const currentAccount = await this.getRequiredSelectedAccount()
+      await this.requestAccountDecoding(currentAccount)
+
+      const actor = await this.getActor(context)
+
+      await this.sendAndFollowNamedTx(currentAccount, 'content', 'deleteVideoCategory', [actor, videoCategoryId])
+    } else {
+      this.error('Video category under given id does not exist...')
+    }
+  }
+}

+ 13 - 2
cli/src/commands/content/updateChannelCategory.ts

@@ -3,15 +3,25 @@ import { IOFlags, getInputJson } from '../../helpers/InputOutput'
 import { ChannelCategoryUpdateParameters, ChannelCategoryUpdateParametersInput } from '../../Types'
 import { channelCategoryMetadataFromInput } from '../../helpers/serialization'
 
-export default class CreateChannelCategoryCommand extends ContentDirectoryCommandBase {
+export default class UpdateChannelCategoryCommand extends ContentDirectoryCommandBase {
   static description = 'Update channel category inside content directory.'
   static flags = {
     context: ContentDirectoryCommandBase.categoriesContextFlag,
     input: IOFlags.input,
   }
 
+  static args = [
+    {
+      name: 'channelCategoryId',
+      required: true,
+      description: 'ID of the Channel Category',
+    },
+  ]
+
   async run() {
-    let { context, input } = this.parse(CreateChannelCategoryCommand).flags
+    let { context, input } = this.parse(UpdateChannelCategoryCommand).flags
+
+    const { channelCategoryId } = this.parse(UpdateChannelCategoryCommand).args
 
     if (!context) {
       context = await this.promptForCategoriesContext()
@@ -44,6 +54,7 @@ export default class CreateChannelCategoryCommand extends ContentDirectoryComman
 
         await this.sendAndFollowNamedTx(currentAccount, 'content', 'updateChannelCategory', [
           actor,
+          channelCategoryId,
           channelCategoryUpdateParameters,
         ])
       }

+ 11 - 0
cli/src/commands/content/updateVideoCategory.ts

@@ -10,9 +10,19 @@ export default class UpdateVideoCategoryCommand extends ContentDirectoryCommandB
     input: IOFlags.input,
   }
 
+  static args = [
+    {
+      name: 'videoCategoryId',
+      required: true,
+      description: 'ID of the Video Category',
+    },
+  ]
+
   async run() {
     let { context, input } = this.parse(UpdateVideoCategoryCommand).flags
 
+    const { videoCategoryId } = this.parse(UpdateVideoCategoryCommand).args
+
     if (!context) {
       context = await this.promptForCategoriesContext()
     }
@@ -44,6 +54,7 @@ export default class UpdateVideoCategoryCommand extends ContentDirectoryCommandB
 
         await this.sendAndFollowNamedTx(currentAccount, 'content', 'updateVideoCategory', [
           actor,
+          videoCategoryId,
           videoCategoryUpdateParameters,
         ])
       }