-
Notifications
You must be signed in to change notification settings - Fork 0
/
homeworkAdd.go
103 lines (86 loc) · 2.65 KB
/
homeworkAdd.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
package main
import (
"fmt"
"os"
"strconv"
"time"
_ "github.com/lib/pq"
)
// insertData はデータベースに入れる内容を格納する構造体
type insertData struct {
HomeworkID string
Subject string
SubjectOmitted string
Title string
DueTime time.Time
AlarmTime time.Time
}
func homeworkAdd(homeworkID string, subject string, subjectOmitted string, homework string, dueTime time.Time) {
homeworkInfo := insertData{
HomeworkID: homeworkID,
Subject: subject,
SubjectOmitted: subjectOmitted,
Title: homework,
DueTime: dueTime,
AlarmTime: alarmTimeAdd(homeworkID, dueTime, subjectOmitted),
}
dbAdd(&homeworkInfo)
}
// dbAdd はDBテーブルに同じデータがなければ追加する関数
func dbAdd(homeworkInfo *insertData) {
var sqlStatement string = fmt.Sprintf("SELECT id FROM %s", tableName)
rows, err := db.Query(sqlStatement)
if err != nil {
panic(nil)
}
defer rows.Close()
var dbSubjectIDs map[string]int = make(map[string]int, 0)
for rows.Next() {
var temp string
rows.Scan(&temp)
dbSubjectIDs[temp] = 1
}
// 同じものがなければ書き込み
if _, exist := dbSubjectIDs[homeworkInfo.HomeworkID]; !exist {
dbWrite(homeworkInfo)
} else {
fmt.Println("同じものがありました…")
}
}
// dbWrite はdbテーブルに課題情報を書き込む関数
func dbWrite(homeworkInfo *insertData) {
var sqlStatement string = fmt.Sprintf("INSERT INTO %s (id, subject, omitted, title, duetime, alarmtime) VALUES ($1, $2, $3, $4, $5, $6)", tableName)
_, err := db.Exec(
sqlStatement,
homeworkInfo.HomeworkID,
homeworkInfo.Subject,
homeworkInfo.SubjectOmitted,
homeworkInfo.Title,
homeworkInfo.DueTime,
homeworkInfo.AlarmTime,
)
if err != nil {
panic(err)
}
}
// dbDelete はテーブル内のデータをすべて削除する関数
func dbDelete() {
var sqlStatement string = "DELETE FROM " + tableName
_, err := db.Exec(sqlStatement)
if err != nil {
panic(err)
}
}
// alarmTimeAdd は課題アラームを追加し、アラーム時刻を返す
func alarmTimeAdd(id string, dueTime time.Time, name string) time.Time {
// 提出期限の何時間前かを取得し、その時刻を求める
hourBeforeDep, _ := strconv.Atoi(os.Getenv("HOUR_BEFORE_DEPARTURE"))
var alarmTime time.Time = dueTime.Add(-time.Duration(hourBeforeDep) * time.Hour)
// fmt.Println("予定追加:", alarmTime, name)
// アラーム一覧になければ課題アラームを追加
if _, exist := alarmTimes[id]; !exist {
alarmTimes[id] = []interface{}{alarmTime, name}
}
// fmt.Println("現在の課題アラーム:", alarmTimes)
return alarmTime
}