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 ENV=debug
PORT=3000 PORT=3000
POSTGRES_URL=postgresql://localhost/micrach POSTGRES_URL=postgres://localhost/micrach

View File

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

2
.gitignore vendored
View File

@ -21,3 +21,5 @@ __debug_bin
micrach micrach
uploads/ uploads/
.env

View File

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

View File

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

View File

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

View File

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