Complete Go for Professional Developers

Testing CreateWorkout Endpoint with cURL

Melkey
Twitch
Complete Go for Professional Developers

Lesson Description

The "Testing CreateWorkout Endpoint with cURL" Lesson is part of the full, Complete Go for Professional Developers course featured in this preview video. Here's what you'd learn in this lesson:

Melkey uses a cURL command to test the CreateWorkout handler. The requests sends a workout along with two workout entries. psql is used to inspect the database and verify the rows were inserted. If there are any errors in the database migrations, delete the database and restart the Go application to rerun the migrations. The current code can be found on the 1.5 commit in the repo.

Preview
Close

Transcript from the "Testing CreateWorkout Endpoint with cURL" Lesson

[00:00:00]
>> Melkey: So we are missing one more thing and that's because we have declared the workout store in the handler and our constructor is expecting this workout store but we haven't actually called the workout handler with the new work out store. So if we go back to our app.go you can see here our lovely compile is already yelling at us, which is great.

[00:00:21]
And here we're gonna do declare our workout store. It's going to be store.NewPostgresWorkoutStore, and it expects one argument, which is our database, we can pass in our PGDB. And then to finalize the loop, we can go into the workout handler and pass in that workout store. So with all that kinda hooked up, I think it's a good time to try out if it works.

[00:00:45]
So what we can do is if you go to a different terminal, go ahead and do run main.go.
>> Melkey: Let's see, I almost forgot to turn my database. So if you go, docker, make sure the database's running, docker compose up. Okay, so yeah, make sure you have your database running.

[00:01:06]
That was an example of our application panicking because our database connection wasn't working. Now if we do go run main.go, if this doesn't work, I'll be very sad, but it does. All right, so we have our database connected, we're back and running. If you open up a business at fourth terminal, all right?

[00:01:25]
This is where we're gonna curl a request. Now, as at the beginning you can go ahead and use any sort of API cutlery runner, post man, post woman, Bruno, these are all viable options, I use curl. Now, curl is great, however typing all along takes quite a bit of time, it looks messy.

[00:01:42]
I have this file called post_notes.txt. Now, typically, I don't like doing this, I don't like how people just, here's some code, bring it in. I'm making the exception here cuz they're just pro requests with JSON bodies. Now, I just wanna quickly explain how these are set out. There's this little number here at the beginning, 1.5, and then the actual JSON request here, okay?

[00:02:04]
And you can see the following is 1.6, and if you keep scrolling down, there's 3.1, 3.2. These are related to the current submodule that we are working on. So right now we are on submodule 1.5. So when you go back, what we're doing here, and I'll add this to the commit message, 1.5 matches the 1.5 co request that we're creating here.

[00:02:24]
We have our Go server running, we have our Davis connection. And then in the fourth terminal, go ahead and connect via PSQL, so I use PSQL. We can do use Postgres, the host is local host, port 5432. Remember, if you are doing this on a different from port, not 5432, make sure that is declared here, okay?

[00:02:47]
And here now I can actually access the database, I can do slash DT to see all of the schemas we have through Goose migration. You can check, select all from workouts, this should be empty as it is, but we can see the schema definition here.
>> Melkey: So now with all that setup, let's go ahead and copy the very first curl command from the postnotes.txt right here, so that should be under 1.5.

[00:03:25]
And if we go into, I guess this would be a fifth terminal and we paste it, which should see the response in JSON, okay? So now we have essentially created a workout with the body of title, description duration minutes, calories burned, and entries, the cool part here is entries is a list of workout entries, each with the individual exercise name, such as squats and bench press.

[00:03:52]
And you can add on to this, there's no limit. This could be empty if you want, well, no, it has to have at least one workout entry, but this could be as large as we want it to. And the expected response is responding work to create a workout from our handler up here, okay?

[00:04:09]
And if we kind of go back to PSQL, and I just ran the command, select all from workouts before running this command, this was empty, if I run this now, I now have an actual entry into my database. You can see here morning strength training, description, full body strength training session, duration 60 minutes, I burned 500 calories.

[00:04:31]
And the created ad and the updated ad is the time at which this was inserted into our database. And we can even take this one step further, right? Because we have workout entries, if we do select all from workout_entries, we can see here we do in fact see the squat and the bench entries for that workout.

[00:04:55]
You can see in the second column here that the worker ID is one for both of these worker entries, which matches the workout ID from the previous table. Does anyone have any questions, anyone kind of seeing any errors, is it working for everyone?
>> Speaker 2: Michael in chat says he's getting an error, column exercise name of relation workout entries does not exist.

[00:05:20]
>> Melkey: That error is in the SQL under migrations work on entries, they don't have the exercise name column here. So make sure you have double-checked the repo I pushed, make sure your SQL tables are correct. If you ran your migration with an incorrect workout schema or you missed a field, what I recommend at this point is just new to database and rerun it, or you can skip ahead and figure out how to do the migration call.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now