-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignup.go
More file actions
236 lines (203 loc) · 8.73 KB
/
signup.go
File metadata and controls
236 lines (203 loc) · 8.73 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package main
import (
"database/sql"
"encoding/json"
"fmt"
"net/http"
"os"
_ "github.com/mattn/go-sqlite3"
"golang.org/x/crypto/bcrypt"
)
var db *sql.DB
func staffRegister(res http.ResponseWriter, req *http.Request) {
if req.Method != "POST" {
http.ServeFile(res, req, "staffRegister.html")
return
}
username := req.FormValue("username")
firstname := req.FormValue("firstname")
lastname := req.FormValue("lastname")
email := req.FormValue("email")
birthdate := req.FormValue("birthdate")
gender := req.FormValue("gender")
password := req.FormValue("password")
course1 := req.FormValue("course1")
course2 := req.FormValue("course2")
course3 := req.FormValue("course3")
var user string
err := db.QueryRow("SELECT username FROM staff WHERE username=?", username).Scan(&user)
switch {
case err == sql.ErrNoRows:
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
fmt.Println(err)
http.Error(res, "Server error, unable to create your account.", 500)
return
}
_, err = db.Exec("INSERT INTO staff(username ,firstname ,lastname ,email ,birthdate ,gender ,password ,course1 ,course2 ,course3) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", username, firstname, lastname, email, birthdate, gender, hashedPassword, course1, course2, course3)
if err != nil {
fmt.Println(err)
http.Error(res, "Server error, unable to create your account.", 500)
return
}
res.Write([]byte("User created!"))
return
case err != nil:
fmt.Println(err)
http.Error(res, "Server error, unable to create your account.", 500)
return
default:
http.Error(res, "User Already Exists.", 500)
}
}
func courseRegister(res http.ResponseWriter, req *http.Request) {
if req.Method != "POST" {
http.ServeFile(res, req, "courseRegister.html")
return
}
coursename := req.FormValue("coursename")
coursecode := req.FormValue("coursecode")
var course string
err := db.QueryRow("SELECT coursename FROM courses WHERE coursename=?", coursename).Scan(&course)
switch {
case err == sql.ErrNoRows:
_, err = db.Exec("INSERT INTO courses(coursename ,coursecode) VALUES(?, ?)", coursename, coursecode)
if err != nil {
http.Error(res, "Server error, unable to create your course.", 500)
return
}
res.Write([]byte("Course created!"))
return
case err != nil:
http.Error(res, "Server error, unable to create your Course.", 500)
return
default:
http.Error(res, "Course Already Exists.", 500)
}
}
func studentRegister(res http.ResponseWriter, req *http.Request) {
if req.Method != "POST" {
http.ServeFile(res, req, "studentRegister.html")
return
}
username := req.FormValue("username")
firstname := req.FormValue("firstname")
lastname := req.FormValue("lastname")
email := req.FormValue("email")
birthdate := req.FormValue("birthdate")
gender := req.FormValue("gender")
password := req.FormValue("password")
course1 := req.FormValue("course1")
course2 := req.FormValue("course2")
course3 := req.FormValue("course3")
course4 := req.FormValue("course4")
course5 := req.FormValue("course5")
course6 := req.FormValue("course6")
var user string
err := db.QueryRow("SELECT username FROM students WHERE username=?", username).Scan(&user)
switch {
case err == sql.ErrNoRows:
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
fmt.Println(err)
http.Error(res, "Server error, unable to create your account.", 500)
return
}
_, err = db.Exec("INSERT INTO students(username ,firstname ,lastname ,email ,birthdate ,gender ,password ,course1 ,course2 ,course3, course4, course5, course6) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", username, firstname, lastname, email, birthdate, gender, hashedPassword, course1, course2, course3, course4, course5, course6)
if err != nil {
fmt.Println(err)
http.Error(res, "Server error, unable to create your account.", 500)
return
}
res.Write([]byte("User created!"))
return
case err != nil:
fmt.Println(err)
http.Error(res, "Server error, unable to create your account.", 500)
return
default:
http.Error(res, "User Already Exists.", 500)
}
}
func loginPage(res http.ResponseWriter, req *http.Request) {
if req.Method != "POST" {
http.ServeFile(res, req, "login.html")
return
}
username := req.FormValue("username")
password := req.FormValue("password")
var databaseUsername string
var databaseFirstname string
var databaseLastname string
var databaseEmail string
var databaseBirthdate string
var databaseGender string
var databasePassword string
var databaseCourse1 string
var databaseCourse2 string
var databaseCourse3 string
var databaseCourse4 string
var databaseCourse5 string
var databaseCourse6 string
var student bool
err := db.QueryRow("SELECT username FROM students WHERE username=?", username).Scan(&databaseUsername)
if err == nil {
student = true
db.QueryRow("SELECT username ,firstname ,lastname ,email ,birthdate ,gender ,password ,course1 ,course2 ,course3 ,course4 ,course5 ,course6 FROM students WHERE username=?", username).Scan(&databaseUsername, &databaseFirstname, &databaseLastname, &databaseEmail, &databaseBirthdate, &databaseGender, &databasePassword, &databaseCourse1, &databaseCourse2, &databaseCourse3, &databaseCourse4, &databaseCourse5, &databaseCourse6)
} else {
err := db.QueryRow("SELECT username FROM staff WHERE username=?", username).Scan(&databaseUsername)
if err != nil {
http.Error(res, "User Doesn't Exist.", 500)
return
}
db.QueryRow("SELECT username ,firstname ,lastname ,email ,birthdate ,gender ,password ,course1 ,course2 ,course3 FROM staff WHERE username=?", username).Scan(&databaseUsername, &databaseFirstname, &databaseLastname, &databaseEmail, &databaseBirthdate, &databaseGender, &databasePassword, &databaseCourse1, &databaseCourse2, &databaseCourse3)
}
errP := bcrypt.CompareHashAndPassword([]byte(databasePassword), []byte(password))
if errP != nil {
http.Error(res, "Wrong Password.", 500)
return
}
if student {
res.Write([]byte("Hello Student " + databaseFirstname + " " + databaseLastname + "!\nHow are you doing today\nYour data is as Following\nUsername : " + databaseUsername + "\nEmail : " + databaseEmail + "\nBirthdate : " + databaseBirthdate + "\nGender : " + databaseGender + "\nYou Have The Following Courses" + "\n\t" + databaseCourse1 + "\n\t" + databaseCourse2 + "\n\t" + databaseCourse3 + "\n\t" + databaseCourse4 + "\n\t" + databaseCourse5 + "\n\t" + databaseCourse6))
} else {
res.Write([]byte("Hello Dr. " + databaseFirstname + " " + databaseLastname + "!\nHow are you doing today\nYour data is as Following\nUsername : " + databaseUsername + "\nEmail : " + databaseEmail + "\nBirthdate : " + databaseBirthdate + "\nGender : " + databaseGender + "\nYou Teach The Following Courses" + "\n\t" + databaseCourse1 + "\n\t" + databaseCourse2 + "\n\t" + databaseCourse3))
}
}
type person struct {
name string
age int
}
func getCourses(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "application/json")
res.WriteHeader(http.StatusOK)
var courses []string
var course string
rows, _ := db.Query("select coursename from courses")
for rows.Next() {
rows.Scan(&course)
courses = append(courses, course)
}
json.NewEncoder(res).Encode(courses)
}
func homePage(res http.ResponseWriter, req *http.Request) {
http.ServeFile(res, req, "index.html")
}
func main() {
if _, err := os.Stat("database.db"); err != nil {
file, _ := os.Create("database.db")
file.Close()
}
db, _ = sql.Open("sqlite3", "database.db")
db.Exec("CREATE TABLE IF NOT EXISTS staff (id INTEGER PRIMARY KEY AUTOINCREMENT ,username VARCHAR(50) NOT NULL ,firstname VARCHAR(50) NOT NULL ,lastname VARCHAR(50) NOT NULL ,email VARCHAR(50) NOT NULL ,birthdate VARCHAR(50) NOT NULL ,gender VARCHAR(50) NOT NULL ,password VARCHAR(120) NOT NULL, course1 VARCHAR(50), course2 VARCHAR(50), course3 VARCHAR(50))")
db.Exec("CREATE TABLE IF NOT EXISTS students (id INTEGER PRIMARY KEY AUTOINCREMENT ,username VARCHAR(50) NOT NULL ,firstname VARCHAR(50) NOT NULL ,lastname VARCHAR(50) NOT NULL ,email VARCHAR(50) NOT NULL ,birthdate VARCHAR(50) NOT NULL ,gender VARCHAR(50) NOT NULL ,password VARCHAR(120) NOT NULL, course1 VARCHAR(50), course2 VARCHAR(50), course3 VARCHAR(50), course4 VARCHAR(50), course5 VARCHAR(50), course6 VARCHAR(50))")
db.Exec("CREATE TABLE IF NOT EXISTS courses (id INTEGER PRIMARY KEY AUTOINCREMENT ,coursecode VARCHAR(50) NOT NULL ,coursename VARCHAR(50) NOT NULL)")
defer db.Close()
fmt.Println("http://localhost:5000/")
http.HandleFunc("/staffRegister", staffRegister)
http.HandleFunc("/studentRegister", studentRegister)
http.HandleFunc("/courseRegister", courseRegister)
http.HandleFunc("/login", loginPage)
http.HandleFunc("/", homePage)
http.HandleFunc("/get", getCourses)
http.ListenAndServe(":5001", nil)
}