mirror of
https://github.com/yanislav-igonin/micrach
synced 2024-12-22 14:22:33 +03:00
feat: add err check for single row
This commit is contained in:
parent
0717a89bc7
commit
107dd0fa0e
2
.env
2
.env
@ -1,3 +1,3 @@
|
||||
ENV=debug
|
||||
PORT=3000
|
||||
POSTGRES_URL=postgresql://localhost/micrach
|
||||
POSTGRES_URL=postgres://localhost/micrach
|
@ -1,3 +1,3 @@
|
||||
ENV=debug
|
||||
PORT=3000
|
||||
POSTGRES_URL=postgresql://localhost/micrach
|
||||
POSTGRES_URL=postgres://localhost/micrach
|
4
.gitignore
vendored
4
.gitignore
vendored
@ -20,4 +20,6 @@ __debug_bin
|
||||
|
||||
micrach
|
||||
|
||||
uploads/
|
||||
uploads/
|
||||
|
||||
.env
|
7
main.go
7
main.go
@ -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()
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user