Skip to content

Commit 229767d

Browse files
committed
sqlite added with user creation
1 parent 422cd95 commit 229767d

File tree

8 files changed

+86
-6
lines changed

8 files changed

+86
-6
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ go.work.sum
2424
# env file
2525
.env
2626

27-
storage
27+
/storage

cmd/api/main.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,23 @@ import (
1313

1414
"github.com/krishkumar84/golang-project/pkg/config"
1515
"github.com/krishkumar84/golang-project/pkg/http/handler/users"
16+
"github.com/krishkumar84/golang-project/pkg/storage/sqlite"
17+
_ "github.com/mattn/go-sqlite3"
1618
)
1719

1820
func main() {
21+
1922
// load config
2023

2124
cfg := config.MustLoad()
2225

23-
//database
2426

27+
//database
28+
storage,err := sqlite.New(cfg)
29+
if err != nil {
30+
log.Fatal(err)
31+
}
32+
slog.Info("Database connected",cfg.Env)
2533

2634
//setup router
2735

@@ -31,7 +39,7 @@ func main() {
3139
w.Write([]byte("Hello from krish and cicd and server grace full kill is running"))
3240
})
3341

34-
router.HandleFunc("POST /api/users",users.New())
42+
router.HandleFunc("POST /api/users",users.New(storage))
3543

3644
//start server
3745

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require (
1616
github.com/leodido/go-urn v1.4.0 // indirect
1717
github.com/mattn/go-colorable v0.1.13 // indirect
1818
github.com/mattn/go-isatty v0.0.20 // indirect
19+
github.com/mattn/go-sqlite3 v1.14.24 // indirect
1920
github.com/radovskyb/watcher v1.0.7 // indirect
2021
golang.org/x/crypto v0.19.0 // indirect
2122
golang.org/x/net v0.21.0 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOA
3131
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
3232
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
3333
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
34+
github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
35+
github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
3436
github.com/radovskyb/watcher v1.0.7 h1:AYePLih6dpmS32vlHfhCeli8127LzkIgwJGcwwe8tUE=
3537
github.com/radovskyb/watcher v1.0.7/go.mod h1:78okwvY5wPdzcb1UYnip1pvrZNIVEIh/Cm+ZuvsUYIg=
3638
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=

pkg/http/handler/users/users.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ import (
99
"net/http"
1010

1111
"github.com/go-playground/validator/v10"
12+
"github.com/krishkumar84/golang-project/pkg/storage"
1213
"github.com/krishkumar84/golang-project/pkg/types"
1314
"github.com/krishkumar84/golang-project/pkg/utils/response"
1415
)
1516

1617

1718

18-
func New() http.HandlerFunc{
19+
func New(storage storage.Storage) http.HandlerFunc{
1920
return func(w http.ResponseWriter, r *http.Request) {
2021
slog.Info("New User Handler")
2122
var user types.User
@@ -37,7 +38,20 @@ func New() http.HandlerFunc{
3738
return
3839
}
3940

40-
response.WriteJson(w, http.StatusCreated, map[string]string{"status":"ok"})
41+
lastId, err := storage.CreateUser(
42+
user.Name,
43+
user.Email,
44+
user.Age,
45+
)
46+
47+
slog.Info("User created sucessfully",slog.String("userId",fmt.Sprint(lastId)))
48+
49+
if err != nil {
50+
response.WriteJson(w, http.StatusInternalServerError, response.GeneralError(err))
51+
return
52+
}
53+
54+
response.WriteJson(w, http.StatusCreated, map[string]int64{"id":lastId})
4155
}
4256
}
4357

pkg/storage/sqlite/sqlite.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package sqlite
2+
3+
import (
4+
"database/sql"
5+
6+
"github.com/krishkumar84/golang-project/pkg/config"
7+
_"github.com/mattn/go-sqlite3"
8+
9+
)
10+
11+
type Sqlite struct {
12+
Db *sql.DB
13+
}
14+
15+
func New(cfg *config.Config) (*Sqlite, error) {
16+
db, err := sql.Open("sqlite3", cfg.StoragePath)
17+
if err != nil {
18+
return nil, err
19+
}
20+
21+
_,err =db.Exec(`CREATE TABLE IF NOT EXISTS users (
22+
id INTEGER PRIMARY KEY AUTOINCREMENT,
23+
name TEXT NOT NULL UNIQUE,
24+
email TEXT NOT NULL UNIQUE,
25+
age INTEGER NOT NULL
26+
)`)
27+
if err != nil {
28+
return nil, err
29+
}
30+
31+
return &Sqlite{Db: db}, nil
32+
}
33+
34+
func (s *Sqlite) CreateUser(name string, email string, age int) (int64, error) {
35+
stmt, err := s.Db.Prepare("INSERT INTO users (name, email, age) VALUES (?, ?, ?)")
36+
if err != nil {
37+
return 0, err
38+
}
39+
defer stmt.Close()
40+
41+
result, err := stmt.Exec(name, email, age)
42+
if err != nil {
43+
return 0, err
44+
}
45+
lastId, err := result.LastInsertId()
46+
if err != nil {
47+
return 0, err
48+
}
49+
return lastId, nil
50+
}

pkg/storage/storage.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package storage
2+
3+
type Storage interface {
4+
CreateUser(name string, email string, age int)(int64, error)
5+
}

pkg/types/types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package types
22

33
type User struct {
4-
Id int
4+
Id int64
55
Name string `validate:"required"`
66
Email string `validate:"required"`
77
Age int `validate:"required"`

0 commit comments

Comments
 (0)