addTask.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package server
  2. import (
  3. "fmt"
  4. "net/http"
  5. )
  6. func (server *Server) addTask(w http.ResponseWriter, r *http.Request) {
  7. var valid bool = true
  8. if r.Method != http.MethodPost {
  9. http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
  10. return
  11. }
  12. if err := r.ParseForm(); err != nil {
  13. fmt.Fprintf(w, "ERROR: Failed ParseForm() err: %v", err)
  14. return
  15. }
  16. //fmt.Println(r.PostForm)
  17. // Validate r.PostForm["rows"]
  18. // Look at flags.validateRow()
  19. // 1. Make sure the rows is 9 in length
  20. if len(r.PostForm["rows"]) != 9 {
  21. fmt.Fprintf(w, "ERROR: There aren't 9 rows")
  22. return
  23. }
  24. // 2. Validate the row
  25. for _, value := range r.PostForm["rows"] {
  26. if !server.validateRow(value) {
  27. valid = false
  28. }
  29. }
  30. // Go/No-Go moment
  31. if !valid {
  32. w.Write([]byte("ERROR found"))
  33. return
  34. }
  35. // Add the task
  36. var puzzle [9]string
  37. puzzle[0] = r.PostForm["rows"][0]
  38. puzzle[1] = r.PostForm["rows"][1]
  39. puzzle[2] = r.PostForm["rows"][2]
  40. puzzle[3] = r.PostForm["rows"][3]
  41. puzzle[4] = r.PostForm["rows"][4]
  42. puzzle[5] = r.PostForm["rows"][5]
  43. puzzle[6] = r.PostForm["rows"][6]
  44. puzzle[7] = r.PostForm["rows"][7]
  45. puzzle[8] = r.PostForm["rows"][8]
  46. // Create task and chuck it in the server struct
  47. task := Task{Puzzle: puzzle}
  48. server.Tasks = append(server.Tasks, &task)
  49. // Calling it
  50. w.Write([]byte("Ok"))
  51. }