feat: add err check for single row

This commit is contained in:
Yanislav Igonin 2021-09-03 02:34:45 +03:00
parent 0717a89bc7
commit 107dd0fa0e
7 changed files with 21 additions and 11 deletions

2
.env
View File

@ -1,3 +1,3 @@
ENV=debug
PORT=3000
POSTGRES_URL=postgresql://localhost/micrach
POSTGRES_URL=postgres://localhost/micrach

View File

@ -1,3 +1,3 @@
ENV=debug
PORT=3000
POSTGRES_URL=postgresql://localhost/micrach
POSTGRES_URL=postgres://localhost/micrach

4
.gitignore vendored
View File

@ -20,4 +20,6 @@ __debug_bin
micrach
uploads/
uploads/
.env

View File

@ -9,16 +9,15 @@ import (
Config "micrach/config"
Controllers "micrach/controllers"
// Db "micrach/db"
Db "micrach/db"
Repositories "micrach/repositories"
// Utils "micrach/utils"
)
func main() {
Config.Init()
// Db.Init()
// defer Db.Pool.Close()
Db.Init()
defer Db.Pool.Close()
gin.SetMode(Config.App.Env)
Repositories.Seed()

View File

@ -5,6 +5,7 @@ import (
Db "micrach/db"
"github.com/jackc/pgtype"
"github.com/jackc/pgx/v4"
)
type FilesRepository struct{}
@ -23,10 +24,15 @@ func (r *FilesRepository) Create(f File) error {
VALUES ($1, $2, $3, $4)
`
conn.QueryRow(
row := conn.QueryRow(
context.TODO(), sql, f.PostID, f.Name, f.Ext, f.Size,
)
err = row.Scan()
if err != nil && err != pgx.ErrNoRows {
return err
}
return nil
}

View File

@ -93,7 +93,10 @@ func (r *PostsRepository) Create(p Post) (int, error) {
}
createdPost := new(Post)
row.Scan(&createdPost.ID)
err = row.Scan(&createdPost.ID)
if err != nil {
return 0, err
}
return createdPost.ID, nil
}

View File

@ -75,7 +75,7 @@ func seedLocal() {
func seedDb() {
// preparing seed data with parent posts with files
var parentPosts []Post
for i := 1; i < 10; i++ {
for i := 1; i < 100; i++ {
post := getPost(i, nil)
parentPosts = append(parentPosts, post)
}
@ -97,7 +97,7 @@ func seedDb() {
}
// making child posts
for i := 0; i < 10; i++ {
for i := 0; i < 100; i++ {
// getting child post with files
childPost := getPost(0, &parentPostID)
childPostID, err := Posts.Create(childPost)