-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstorage.go
68 lines (56 loc) · 1.36 KB
/
storage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"database/sql"
"log"
_ "github.com/mattn/go-sqlite3"
)
type UserStorage interface {
ReadUsers() ([]User, error)
AddUser(name string, surname string) error
DeleteUser(id string) error
}
type StorageImpl struct {
connection *sql.DB
}
func NewStorage(dbType, dbName string) (StorageImpl, error) {
connection, err := sql.Open(dbType, dbName)
if err != nil {
return StorageImpl{}, err
}
log.Printf("Connected to database %v", connection)
return StorageImpl{
connection: connection,
}, nil
}
func (s StorageImpl) ReadUsers() ([]User, error) {
rows, err := s.connection.Query("SELECT id, name, surname FROM users ORDER BY id")
if err != nil {
return []User{}, err
}
defer rows.Close()
var users []User
for rows.Next() {
var id int
var name string
var surname string
if err := rows.Scan(&id, &name, &surname); err != nil {
return users, err
}
users = append(users, User{
ID: id,
Name: name,
Surname: surname,
})
}
return users, nil
}
func (s StorageImpl) AddUser(name string, surname string) error {
statement, err := s.connection.Prepare("INSERT INTO users(name, surname) VALUES (?, ?)")
_, err = statement.Exec(name, surname)
return err
}
func (s StorageImpl) DeleteUser(id string) error {
statement, err := s.connection.Prepare("DELETE FROM users WHERE id=?")
_, err = statement.Exec(id)
return err
}