-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivity.go
80 lines (63 loc) · 1.87 KB
/
activity.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
package app
import (
"fmt"
"log"
"net/http"
"time"
"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
var DBClient *sqlx.DB
var PostNumber Post
func ConnectToMySQL() {
db, err := sqlx.Connect("mysql", "doctor:miyamoto@tcp(localhost:3306)/test_db?parseTime=true")
if err != nil {
log.Fatalln(err)
}
fmt.Println("connected", db)
DBClient = db
}
func GetInt() error {
tx := DBClient.MustBegin()
tx.Get(&PostNumber, `SELECT id, test_numbers, created_at FROM numbers WHERE id=1`)
tx.Commit()
fmt.Println("ID: ", PostNumber.ID)
fmt.Println("Number: ", PostNumber.TestNumbers)
return nil
}
func UpdateInt() error {
PostNumber.TestNumbers++
td := time.Now()
DBClient.Exec("UPDATE numbers SET test_numbers=? WHERE id=1;", PostNumber.TestNumbers)
fmt.Println("Updated number:", PostNumber.TestNumbers, "Created: ", PostNumber.CreatedAt, "Updated: ", td)
return nil
}
// The sunction below are for testing the functions without a Temporal workflow
// with the main.go in the sql folder
func GetInt2(c *gin.Context) {
td := time.Now()
tx := DBClient.MustBegin()
tx.Get(&PostNumber, `SELECT id, test_numbers, created_at FROM numbers WHERE id=1`)
tx.Commit()
fmt.Println("ID: ", PostNumber.ID)
fmt.Println("Number: ", PostNumber.TestNumbers)
c.JSON(http.StatusOK, gin.H{
//"error": false,
"id": PostNumber.ID,
"Number: ": PostNumber.TestNumbers,
"Updated number": PostNumber.TestNumbers,
"Created ": PostNumber.CreatedAt,
"Updated ": td,
})
//return nil
}
func UpdateInt2(c *gin.Context) {
PostNumber.TestNumbers++
td := time.Now()
DBClient.Exec("UPDATE numbers SET test_numbers=? WHERE id=1;", PostNumber.TestNumbers)
fmt.Println("Updated number:", PostNumber.TestNumbers, "Created: ", PostNumber.CreatedAt, "Updated: ", td)
c.JSON(http.StatusCreated, gin.H{
"error": false,
})
}