Complete the demo procedure
We already have a working application using the repository implementation and calling the demo procedure. However, it would be good for the demo procedure to do more than just migrate data. So complete the app/demo.go
file, and let’s trace what the newly added operations do.
MIGRATE REPOSITORY
- As you already know, at the beginning, we executed theMigrate()
function, which created thewebsites
database table.CREATE RECORDS OF REPOSITORY
- Next, in lines18
to43
, we create 2 newWebsite
objects and add them to the repository using theCreate()
method. Then we print out the inserted records to the standard output. Note how we handle errors from theCreate()
method. In the case of anErrDuplicate
error, we only log that fact; in the case of a non-standard, unexpected error, we abort the application execution by logging the error using thelog.Fatal()
function.GET RECORD BY NAME
- After inserting the records to the database, we check if we can pull them from there. In lines45-53
, we get a record namedGOSAMPLES
using theGetByName()
method. If such a row does not exist, we log anErrNotExist
error and move on. Other unknown errors are logged, and the application exits.UPDATE RECORD
- In lines55-65
, we update a single record with a new ranking value. Because theUpdate()
method may returnErrDuplicate
orErrUpdateFailed
errors, we check for them and handle them by printing on the standard output.GET ALL
- Lines67
through75
use theAll()
method to get a list of all records and then print them to the standard output, so we can check that the update actually was made in the database.DELETE RECORD
- In lines77-84
, we delete one of the inserted records and handle errors if they occur. In the case ofErrDeleteFailed
, we log the error; in the case of others, we exit the application.GET ALL
- As the last thing, in lines86-93
, we again get the list of all records using theAll()
method and write them out to the standard output to check if the row was actually deleted.
Now you can run our cmd/classic/main.go
application using
go run main.go
and check the output:
1. MIGRATE REPOSITORY
2. CREATE RECORDS OF REPOSITORY
&{ID:1 Name:GOSAMPLES URL:https://gosamples.dev Rank:2}
&{ID:2 Name:Golang official website URL:https://golang.org Rank:1}
3. GET RECORD BY NAME
&{ID:1 Name:GOSAMPLES URL:https://gosamples.dev Rank:2}
4. UPDATE RECORD
5. GET ALL
{ID:2 Name:Golang official website URL:https://golang.org Rank:1}
{ID:1 Name:GOSAMPLES URL:https://gosamples.dev Rank:1}
6. DELETE RECORD
7. GET ALL
{ID:1 Name:GOSAMPLES URL:https://gosamples.dev Rank:1}
Feel free to experiment on your own. If this is your first application connecting to PostgreSQL, change function input arguments, change data, run the application multiple times and see what happens, check what errors appear, and try to fix them. The code we have created is just an introduction, which you should use as a base to create something more advanced.
Remember that when you run the application for the second time, you have already inserted data into the
websites
table. If you want to clean the database, just close the running PostgreSQL Docker container and restart it usingdocker run
as in the beginning.
This way, we finished building the application that performs CRUD operations using the repository based on the database/sql
package. In the following pages, we will show you how to create a repository based on the pgx
client package, and using an ORM called GORM.
app/demo.go
|
|