62 lines
1.3 KiB
Go
62 lines
1.3 KiB
Go
|
package server
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
func (server *Server) addTask(w http.ResponseWriter, r *http.Request) {
|
||
|
var valid bool = true
|
||
|
|
||
|
if r.Method != http.MethodPost {
|
||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||
|
return
|
||
|
}
|
||
|
if err := r.ParseForm(); err != nil {
|
||
|
fmt.Fprintf(w, "ERROR: Failed ParseForm() err: %v", err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
//fmt.Println(r.PostForm)
|
||
|
// Validate r.PostForm["rows"]
|
||
|
// Look at flags.validateRow()
|
||
|
// 1. Make sure the rows is 9 in length
|
||
|
if len(r.PostForm["rows"]) != 9 {
|
||
|
fmt.Fprintf(w, "ERROR: There aren't 9 rows")
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// 2. Validate the row
|
||
|
for _, value := range r.PostForm["rows"] {
|
||
|
if !server.validateRow(value) {
|
||
|
valid = false
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Go/No-Go moment
|
||
|
if !valid {
|
||
|
w.Write([]byte("ERROR found"))
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// Add the task
|
||
|
var puzzle [9]string
|
||
|
puzzle[0] = r.PostForm["rows"][0]
|
||
|
puzzle[1] = r.PostForm["rows"][1]
|
||
|
puzzle[2] = r.PostForm["rows"][2]
|
||
|
puzzle[3] = r.PostForm["rows"][3]
|
||
|
puzzle[4] = r.PostForm["rows"][4]
|
||
|
puzzle[5] = r.PostForm["rows"][5]
|
||
|
puzzle[6] = r.PostForm["rows"][6]
|
||
|
puzzle[7] = r.PostForm["rows"][7]
|
||
|
puzzle[8] = r.PostForm["rows"][8]
|
||
|
|
||
|
// Create task and chuck it in the server struct
|
||
|
task := Task{Puzzle: puzzle}
|
||
|
server.Tasks = append(server.Tasks, &task)
|
||
|
|
||
|
// Calling it
|
||
|
w.Write([]byte("Ok"))
|
||
|
|
||
|
}
|