Ready to get started?

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

robot mascot smiling

Editing & Deleting Course Lessons

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.

Published
Dec 13, 24
Duration
5m 9s

Developer, dog lover, and burrito eater. Currently teaching AdonisJS, a fully featured NodeJS framework, and running Adocasts where I post new lessons weekly. Professionally, I work with JavaScript, .Net C#, and SQL Server.

Adocasts

Burlington, KY

Get the Code

Download or explore the source code for this lesson on GitHub

Repository

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

Ready to get started?

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

Join The Discussion! (5 Comments)

Please sign in or sign up for free to join in on the dicussion.

  1. Commented 26 days ago

    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

    Please sign in or sign up for free to reply

    1. Commented 26 days ago

      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

      Please sign in or sign up for free to reply

      1. Commented 26 days ago

        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

        Please sign in or sign up for free to reply

        1. Commented 23 days ago

          Awesome! Thank you!

          1

          Please sign in or sign up for free to reply

          1. Commented 23 days ago

            Sure thing, Aaron!!

            0

            Please sign in or sign up for free to reply