User Testing Report 1

I split the research into 5 main sections so that I can clearly navigate the users and get focus result in each section Something like successful — already in this spot — share with friends — the…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Building a Simple Web Backend with a database using chatGPT

This is building on top of my last article here. Please reference that before you continue, or you may get lost.

Some caveats before we dive into it: The model I’m using for this is Default (GPT-3.5)again. I’m also emitting some explanation that ChatGPT does, as I want to focus more on the code than it’s explanation of what the code does.

Since I have all the code from my previous chatGPT interaction, I thought I wanted to try having chatGPT build the same thing but using a database — a way better solution to storing things rather than storing in memory.

I asked for it to use gorm because that’s what I’m familiar with. It ended up choosing SQLite as the database and rewrote the files from before:

main.go

server/router.go

handler/handler.go

models/person.go

ChatGPT’s explanation of what it did:

With this updated implementation, we’ve replaced the in-memory storage with a SQLite database and use GORM to perform CRUD operations on the database. The Person model now includes a gorm.Model field, which adds several standard fields to the model such as ID, CreatedAt, UpdatedAt, and DeletedAt.

In the router file, we’ve updated the route definitions to include the person name as a URL parameter for the DELETE endpoint. In the handler file, we’ve updated the handler functions to accept a *gorm.DB parameter, which is used to perform database operations.

Note that this is just one possible implementation using GORM and SQLite, and there are many other approaches that can work well depending on the specific needs of the project.

Great! But where’s the actual database? I asked it

This didn’t work, it outputted another version of the main.go file without the previous code. I then tried to ask it another way:

ChatGPT responded with something that’s relevant, but forgot a bit of the code it had set up previously.

It’s first step was:

Install SQLite: SQLite is a lightweight database management system that can be used for development purposes. You can download and install SQLite from the official website: https://www.sqlite.org/download.html

While this is useful as a Step 1, I wanted to use my command line to install this, so I went ahead and did:

Despite some errors during the installation, it seems like it worked:

Following ChatGPTs 2nd step:

Create a new database file: Once you’ve installed SQLite, you can create a new database file by running the following command in your terminal:

Following ChatGPTs 3rd step:

Create a new table: To create a new table in the database, you can use the following SQL command:

This is where it got lost a little bit. It’s saying to create a table with the name people but we are using person in the code, so I changed it to:

Once I did this, the file my-databse.db showed up in the file structure.

Moving on to ChatGPTs 4th step:

Connect to the database: To connect to the database using GORM, you can use the following code snippet:

This is another place it forgot that it already wrote me the code above and gave me yet another version of the main.go file. I just ignored this part and started testing out the code.

Running go run main.go worked but it took a LOT longer than previously to get up and running. I’m assuming this is because it needs to connect to the new database and get that running before allowing HTTP requests.

I tried the same commands as before to see if they worked:

I then wanted to test if this would persist because we’re using a database now. In the previous article, shutting it down would cause it to wipe it’s memory — so I wanted to be sure it wasn’t doing that this time.

go run main.go

Great! The information is persisting! Now to test the delete function:

Wait, what? You can see that this record wasn’t deleted because it has no DeletedAt timestamp. What’s going on?

I went to the router.go file to look at what type of request it’s looking for when deleting something:

I didn’t notice this at the start, but it rewrote how it wanted to handle DELETE requests. That’s why nothing was deleted, because it’s not following this new format.

So, I asked chatGPT

Get all people:

Get a specific person by name:

Create a new person:

Delete a person by name:

Ah, so this is how we need to format the curl request now. Lets see if it works:

Yes! It worked! I’m now running a fully functioning web backend using a database!

Add a comment

Related posts:

Ripple Launches Liquidity Hub for Businesses to Bridge the Crypto Liquidity Gap

Fintech firm Ripple has launched its liquidity solution for businesses to bridge the gap between crypto and fiat. Ripple liquidity hub was launched on April 13 after a successful pilot last year. The…

Benefits of Corner Chairs for the Bedroom

Most people think that corner chairs for the bedroom are only meant for sitting in, not sleeping in. But this is definitely not the case! In fact, using corner chairs to sleep in has several benefits…

Blog Post 6

Describe one thing you learned in class today.. “Blog Post 6” is published by Rekha Umashankar.