Ready to get started?

Join Adocasts Plus for $8/mo, or sign into an existing Adocasts Plus account, to get access to all of our lessons.

robot mascot smiling

Editing & Deleting Course Lessons

@tomgobich
Published by
@tomgobich
In This Lesson

We'll add the ability to edit and delete lessons from a course's module. When editing, we'll also decrement the order field for all lessons within the module after the lesson being deleted.

Chapters

00:00 - Creating our Update & Destroy Actions
00:29 - Updating Lessons with the Update Lesson Action
01:10 - Deleting Lessons with the Destroy Lesson Action
02:46 - Adding the Lesson Controller's Update & Destroy Route Handlers
03:46 - Defining the Update & Destroy Lesson Routes
04:08 - Testing our Update & Delete Operation Flows

Join the Discussion 5 comments

Create a free account to join in on the discussion
  1. @aaron-ford

    Sorry for all the questions. I am getting a lock timeout error when I try to delete. I copied your destroy_lesson code from the repo to make sure our files matched and still get it. I get it trying to delete any lesson. I built my DB fresh and reseeded, just in case. If I remove the await decrement lesson I can delete lessons just fine, just for some reason the lessons are locked when trying to increment:

    ERROR (5078): update lessons set order = order - 1 where (`module_id` = 1 and order > 1) and (`organization_id` = 1) - Lock wait timeout exceeded; try restarting transaction.

    The query looks right, it seems to be trying to update the right data. Any ideas?

    1
    1. Responding to aaron-ford
      @tomgobich

      No worries at all! It's been a long while since I've used MySQL, so my platform-specific knowledge here will be limited. But, if it works fine without the decrement then I'd imagine you most likely have a stuck lock or transaction on one of your lessons. Something not directly related to the decrement, but rather blocking the decrement from succeeding.

      This may or may not be code-related. It could be a lock added by a client application or CLI using your MySQL table. It could also be another transaction touching one of the lesson rows that was never committed or rolled back. Here is a StackOverflow thread that discussed some query options to dig into what might be locking things up.

      I can try and get MySQL set up and test it out this evening after work to ensure it isn't a code issue specific to MySQL.

      0
      1. Responding to tomgobich
        @tomgobich

        Alrighty, so I took a look into it. I was also getting the error with MySQL, and it was indeed a code issue! I accidentally missed binding the organization to the transaction, which caused the decrement to run outside the confinement of the transaction - hence the lock block.

        Below is the updated code that fixes this issue! Terribly sorry about the miss here! I'll get a note about this added into the lesson.

        export default class DestroyLesson {
          static async handle({ organization, id }: Params) {
            const lesson = await organization.related('lessons').query().where({ id }).firstOrFail()
        
            await db.transaction(async (trx) => {
              lesson.useTransaction(trx)
              organization.useTransaction(trx) // 👈
        
              await lesson.delete()
              await organization
                .related('lessons')
                .query()
                .where('moduleId', lesson.moduleId)
                .where('order', '>', lesson.order)
                .decrement('order')
            })
        
            return lesson
          }
        }
        Copied!
        0
        1. Responding to tomgobich
          @aaron-ford

          Awesome! Thank you!

          1
          1. Responding to aaron-ford
            @tomgobich

            Sure thing, Aaron!!

            0