-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
159 lines (140 loc) · 3.31 KB
/
main.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"database/sql"
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"time"
_ "github.com/go-sql-driver/mysql"
)
type kelas struct {
Id_ruangan string
Id_time int
Mata_kuliah string
}
type ruangan struct {
Id_ruangan string
Nama string
Kapasitas int
Fasilitas string
}
type timeslot struct {
Id_time int
Hari string
Jam string
}
func main() {
port := 8181
http.HandleFunc("/kelas_kosong/", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
time := getTimeslot()
if time != 999 {
GetHariJamNow(w, r, time)
GetKelasKosong(w, r, time)
} else {
log.Println("Saat ini tidak ada jam kuliah!")
}
default:
http.Error(w, "Invalid request method", 405)
}
})
log.Printf("Server berjalan pada port %v\n", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", port), nil))
}
func GetHariJamNow(w http.ResponseWriter, r *http.Request, t int) {
db, err := sql.Open("mysql",
"root:@tcp(167.205.67.251:3306)/habib_labtekv")
if err != nil {
log.Fatal(err)
}
defer db.Close()
hariJamNow := timeslot{}
ts := strconv.Itoa(t) //convert int to string
rows, err := db.Query("select hari, jam from timeslot where id_time = ?;", ts)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
err := rows.Scan(&hariJamNow.Hari, &hariJamNow.Jam)
if err != nil {
log.Fatal(err)
}
json.NewEncoder(w).Encode(&hariJamNow)
}
err = rows.Err()
if err != nil {
log.Fatal(err)
}
}
func GetKelasKosong(w http.ResponseWriter, r *http.Request, t int) {
db, err := sql.Open("mysql",
"root:@tcp(167.205.67.251:3306)/habib_labtekv")
if err != nil {
log.Fatal(err)
}
defer db.Close()
ruangan_kosong := ruangan{}
ts := strconv.Itoa(t) //convert int to string
rows, err := db.Query("select * from ruangan where id_ruangan = any ( select id_ruangan from kelas where mata_kuliah = '' and id_time = ?);", ts)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
err := rows.Scan(&ruangan_kosong.Id_ruangan, &ruangan_kosong.Nama, &ruangan_kosong.Kapasitas, &ruangan_kosong.Fasilitas)
if err != nil {
log.Fatal(err)
}
json.NewEncoder(w).Encode(&ruangan_kosong)
}
err = rows.Err()
if err != nil {
log.Fatal(err)
}
}
func getTimeslot() int {
t := time.Now()
waktuInit := time.Date(2017, 12, 10, 0, 0, 0, 0, t.Location()) //acuan hari untuk mendapatkan nama hari dari waktuNow
waktuNow := time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), t.Location())
var waktuSelisih time.Duration = waktuNow.Sub(waktuInit) //hitung selisih hariInit dan hariNow
var i int = (int(waktuSelisih.Hours()/24) % 7) //cari modulus selisih hariInit dan hariNow utk dapat index hari
var j int = (int(t.Hour())) //cari jam sekarang
switch i {
case 1: //senin
if (j > 6) && (j < 18) {
return j + 94
} else {
return 999
}
case 2: //selasa
if (j > 6) && (j < 18) {
return j + 194
} else {
return 999
}
case 3: //rabu
if (j > 6) && (j < 18) {
return j + 294
} else {
return 999
}
case 4: //kamis
if (j > 6) && (j < 18) {
return j + 394
} else {
return 999
}
case 5: //jumat
if (j > 6) && (j < 18) {
return j + 494
} else {
return 999
}
default: //sabtu minggu
return 999
}
}