-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscriber_model.go
104 lines (92 loc) · 3.44 KB
/
subscriber_model.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Copyright (c) 2021.
* Marc Concepcion
* marcanthonyconcepcion@gmail.com
*/
package MarcGoRESTAPIDemo
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
"strconv"
"strings"
)
var settings = readConfiguration("resources/MarcGoRESTAPIDemo.yaml")
type Records struct {
database *sql.DB
}
type Subscriber struct {
Index uint8 `json:"index,omitempty"`
EmailAddress string `json:"email_address,omitempty"`
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
ActivationFlag bool `json:"activation_flag,omitempty"`
}
func MakeDatabaseRecords() Records {
database, dbInstanceFail := sql.Open("mysql", settings.Database.User+":"+settings.Database.Password+
"@tcp("+settings.Database.Host+":"+strconv.Itoa(int(settings.Database.Port))+")/"+settings.Database.DBName)
if dbInstanceFail != nil {
panic(dbInstanceFail.Error())
}
return Records{database}
}
func (records Records) create(subscriber Subscriber) (sql.Result, error) {
result, fault := records.database.Exec(
"insert into `subscribers` (`email_address`, `last_name`, `first_name`) values (?, ?, ?)",
subscriber.EmailAddress, subscriber.LastName, subscriber.FirstName)
return result, fault
}
func (records Records) retrieve(index uint8) (*Subscriber, error) {
var subscriber Subscriber
record := records.database.QueryRow("select * from `subscribers` where `index`=?", index)
recordModelError := record.Scan(&subscriber.Index, &subscriber.EmailAddress, &subscriber.LastName, &subscriber.FirstName,
&subscriber.ActivationFlag)
return &subscriber, recordModelError
}
func (records Records) update(subscriber Subscriber) (sql.Result, error) {
var parametersToUpdate []string
if "" != subscriber.EmailAddress {
parametersToUpdate = append(parametersToUpdate, "`email_address` = "+"\""+subscriber.EmailAddress+"\"")
}
if "" != subscriber.LastName {
parametersToUpdate = append(parametersToUpdate, "`last_name` = "+"\""+subscriber.LastName+"\"")
}
if "" != subscriber.FirstName {
parametersToUpdate = append(parametersToUpdate, "`first_name` = "+"\""+subscriber.FirstName+"\"")
}
result, updateFail := records.database.Exec("update `subscribers` set "+
strings.Join(parametersToUpdate, ",")+" where `index`=?", subscriber.Index)
return result, updateFail
}
func (records Records) activate(index uint8, activate bool) (sql.Result, error) {
activationFlag := 0
if activate == true {
activationFlag = 1
}
result, updateFail := records.database.Exec(
"update `subscribers` set activation_flag=? where `index`=?", activationFlag, index)
return result, updateFail
}
func (records Records) delete(index uint8) (sql.Result, error) {
result, deleteError := records.database.Exec("delete from `subscribers` where `index`=?", index)
return result, deleteError
}
func (records Records) list() ([]Subscriber, error) {
rows, dbQueryError := records.database.Query("select * from `subscribers`")
if dbQueryError != nil {
return nil, dbQueryError
}
subscribers := make([]Subscriber, 0)
for rows.Next() {
var subscriber Subscriber
if recordModelError := rows.Scan(&subscriber.Index, &subscriber.EmailAddress, &subscriber.LastName, &subscriber.FirstName,
&subscriber.ActivationFlag); recordModelError != nil {
return subscribers, recordModelError
}
subscribers = append(subscribers, subscriber)
}
rowsCloseError := rows.Close()
if rowsCloseError != nil {
return subscribers, rowsCloseError
}
return subscribers, nil
}