diff --git a/football-cms-v3 b/football-cms-v3 deleted file mode 100644 index 2c7d80f0e13e83593069fbb0f88e4abdec5255e1..0000000000000000000000000000000000000000 Binary files a/football-cms-v3 and /dev/null differ diff --git a/handlers/add.go b/handlers/add.go new file mode 100644 index 0000000000000000000000000000000000000000..906c26301fd4464ddc48a6ff8e6b892771da6c49 --- /dev/null +++ b/handlers/add.go @@ -0,0 +1,22 @@ +package handlers + +func handler_add(ctx *macaron.Context) { + var match dbstr.Match + db.First(&match, "Active = ?", 1) + if match.Active != 0 { + ctx.Redirect("/error") + } else { + key := ctx.Req.Request.URL.Query() + if len(key) < 3{ + ctx.HTML(200, "add") + } else if key["team1"] == nil && key["team2"] == nil && key["stadium"] == nil { + ctx.HTML(200, "add") + } else { + team1 := strings.Join(key["team1"], " ") + team2 := strings.Join(key["team2"], " ") + stadium := strings.Join(key["stadium"], " ") + db.Create(&dbstr.Match{Team1: team1, Team2: team2, Stadium: stadium, Active: 1}) + ctx.Redirect("/") + } + } +} \ No newline at end of file diff --git a/handlers/edit.go b/handlers/edit.go new file mode 100644 index 0000000000000000000000000000000000000000..f058f4fc7e226c334b8226843a8742e395dc28ec --- /dev/null +++ b/handlers/edit.go @@ -0,0 +1,21 @@ +package handlers + +func handler_edit(ctx *macaron.Context) { + var match dbstr.Match + db.First(&match, "Active = ?", 1) + if match.Active != 0 { + r := ctx.Req.Request + r.ParseForm() // Parses the request body + arr1 := r.Form.Get("arr1") // x will be "" if parameter is not set + arr2 := r.Form.Get("arr2") + if len(arr1) > 0 && len(arr2) > 0 { + db.Model(&match).Update("Arr1", arr1) + db.Model(&match).Update("Arr2", arr2) + ctx.Redirect("/") + } else { + ctx.Redirect("/error") + } + } else { + ctx.Redirect("/error") + } +} \ No newline at end of file diff --git a/handlers/edit_main.go b/handlers/edit_main.go new file mode 100644 index 0000000000000000000000000000000000000000..14d14f3a111ea638f6b421a5b42ac61ed518ad36 --- /dev/null +++ b/handlers/edit_main.go @@ -0,0 +1,35 @@ +package handlers + +func handler_edit_main(ctx *macaron.Context) { + var match dbstr.Match + db.First(&match, "Active = ?", 1) + if match.Active != 0 { + key := ctx.Req.Request.URL.Query() + if len(key) < 5{ + ctx.Data["Matchinfo"] = match + ctx.HTML(200, "medit") + } else if key["team1"] == nil && key["team2"] == nil && key["stadium"] == nil && key["score1"] == nil && key["score2"] == nil { + ctx.Data["Matchinfo"] = match + ctx.HTML(200, "medit") + } else { + team1 := strings.Join(key["team1"], " ") + team2 := strings.Join(key["team2"], " ") + + stadium := strings.Join(key["stadium"], " ") + + score1, _ := strconv.Atoi(strings.Join(key["score1"], " ")) + score2, _ := strconv.Atoi(strings.Join(key["score2"], " ")) + + db.Model(&match).Update("Team1", team1) + db.Model(&match).Update("Team2", team2) + + db.Model(&match).Update("Stadium", stadium) + + db.Model(&match).Update("Score1", score1) + db.Model(&match).Update("Score2", score2) + ctx.Redirect("/") + } + } else { + ctx.Redirect("/error") + } +} \ No newline at end of file diff --git a/handlers/end_match.go b/handlers/end_match.go new file mode 100644 index 0000000000000000000000000000000000000000..7235d8a569b83c61b62fc1944a40782d17d29bbc --- /dev/null +++ b/handlers/end_match.go @@ -0,0 +1,12 @@ +package handlers + +func handler_end(ctx *macaron.Context) { + var match dbstr.Match + db.First(&match, "Active = ?", 1) + if match.Active != 0 { + db.Model(&match).Update("Active", 0) + ctx.Redirect("/") + } else { + ctx.Redirect("/error") + } +} \ No newline at end of file diff --git a/handlers/error.go b/handlers/error.go new file mode 100644 index 0000000000000000000000000000000000000000..c31ecf9160a8bd4649038a0e442bbe5c6618c3a1 --- /dev/null +++ b/handlers/error.go @@ -0,0 +1,5 @@ +package handlers + +func handler_error() string { + return "Произошла какая-то ошибка!" +} \ No newline at end of file diff --git a/handlers/get.go b/handlers/get.go new file mode 100644 index 0000000000000000000000000000000000000000..c4da7f4fb03448254af1489562a3593908b5b0a4 --- /dev/null +++ b/handlers/get.go @@ -0,0 +1,15 @@ +package handlers + +func handler_events_get(ctx *macaron.Context) { + var match dbstr.Match + db.First(&match, "Active = ?", 1) + if match.Active != 0 { + events := []Event {} + db.Find(&events, "Match_id = ?", match.ID) + ctx.Data["Events"] = events + ctx.Data["Matchinfo"] = match + ctx.HTML(200, "events") + } else { + ctx.Redirect("/error") + } +} \ No newline at end of file diff --git a/handlers/index.go b/handlers/index.go new file mode 100644 index 0000000000000000000000000000000000000000..39074da14dff495436448f2e8c081ce81aa5ebbf --- /dev/null +++ b/handlers/index.go @@ -0,0 +1,18 @@ +package handlers + +func handler_index(ctx *macaron.Context) { + var match dbstr.Match + var events_team1 dbstr.Match_events + var events_team2 dbstr.Match_events + db.First(&match, "Active = ?", 1) + if match.Active != 0 { + json.Unmarshal([]byte(match.Arr1), &events_team1) + json.Unmarshal([]byte(match.Arr2), &events_team2) + ctx.Data["Matchinfo"] = match + ctx.Data["Team1"] = events_team1 + ctx.Data["Team2"] = events_team2 + ctx.HTML(200, "active") + } else { + ctx.Redirect("/add") + } +} \ No newline at end of file diff --git a/handlers/main.go b/handlers/main.go new file mode 100644 index 0000000000000000000000000000000000000000..7d2f087452908078b9f57921d8b54d682ab2b008 --- /dev/null +++ b/handlers/main.go @@ -0,0 +1,14 @@ +package handlers + +import ( + "log" + "encoding/json" + "strings" + "gopkg.in/macaron.v1" + "strconv" + + "github.com/jinzhu/gorm" + _ "github.com/jinzhu/gorm/dialects/sqlite" + + dbstr "../structs" +) \ No newline at end of file diff --git a/handlers/post.go b/handlers/post.go new file mode 100644 index 0000000000000000000000000000000000000000..a879fbf831e29cf853b89b726908bb132eb54bc1 --- /dev/null +++ b/handlers/post.go @@ -0,0 +1,35 @@ +package handlers + +func handler_events_post(ctx *macaron.Context) { + var match dbstr.Match + db.First(&match, "Active = ?", 1) + if match.Active != 0 { + r := ctx.Req.Request + r.ParseForm() + + Match_idc := r.Form.Get("match_id") + Teamc := r.Form.Get("team") + Eventc := r.Form.Get("event") + Minutec := r.Form.Get("minute") + + if len(Match_idc)>0 && len(Teamc)>0 && len(Eventc)>0 && len(Minutec)>0 { + Comment := r.Form.Get("comment") + Midd, _ := strconv.Atoi(Match_idc) + Teamd, _ := strconv.Atoi(Teamc) + Eventd, _ := strconv.Atoi(Eventc) + Minuted, _ := strconv.Atoi(Minutec) + if len(Comment)>0 { + db.Create(&dbstr.Event{Match_id: Midd, Team: Teamd, Event: Eventd, Comment: Comment, Minute: Minuted}) + ctx.Redirect("/events") + } else { + db.Create(&dbstr.Event{Match_id: Midd, Team: Teamd, Event: Eventd, Minute: Minuted}) + ctx.Redirect("/events") + } + } else { + ctx.Redirect("/error") + } + } else { + ctx.Redirect("/error") + } + +} \ No newline at end of file diff --git a/main.go b/main.go index 4dd1bf9e6ddbb9cfbd40456ea5cd361d4cfbecd3..1c6cefc1cee677f0ccfb3fff4d90acbda293a5ef 100644 --- a/main.go +++ b/main.go @@ -4,65 +4,15 @@ import ( "log" "encoding/json" "strings" - "github.com/jinzhu/gorm" - _ "github.com/jinzhu/gorm/dialects/sqlite" "gopkg.in/macaron.v1" - "strconv" -) -// Удары по воротам -// Удары в створ -// Фолы -// Угловые -// Офсайды -// Предупрждения -// Удаления - -// Событие -// Гол -// Жёлтая карточка -// Красная карточка -// Удар по воротам -// Угловой -// Штрафной -// Опасный момент -// Замена -// Комментарий - -type Match struct { - gorm.Model - Team1 string - Team2 string - Arr1 string `sql:"DEFAULT:'{\"shoot1\": 0,\"shoot2\": 0,\"fols\": 0,\"ugls\": 0,\"offsides\": 0,\"yc\": 0,\"rc\": 0}'"` - Arr2 string `sql:"DEFAULT:'{\"shoot1\": 0,\"shoot2\": 0,\"fols\": 0,\"ugls\": 0,\"offsides\": 0,\"yc\": 0,\"rc\": 0}'"` - Score1 uint `sql:"DEFAULT:0"` - Score2 uint `sql:"DEFAULT:0"` - Stadium string - Active uint -} - -type errord struct { - Code string -} + "strconv" -type Match_events struct { - Shoot1 int `json:"shoot1"` - Shoot2 int `json:"shoot2"` - Fols int `json:"fols"` - Ugls int `json:"ugls"` - Offsides int `json:"offsides"` - Yc int `json:"yc"` - Rc int `json:"rc"` -} - - -type Event struct { - gorm.Model - Match_id int - Team int - Event int - Comment string `sql:"DEFAULT:''` - Minute int -} + "github.com/jinzhu/gorm" + _ "github.com/jinzhu/gorm/dialects/sqlite" + + dbstr "./structs" + handler "./handlers" +) var db, err_db = gorm.Open("sqlite3", "stats.db") @@ -79,176 +29,19 @@ func main() { } defer db.Close() - db.AutoMigrate(&Match{}) - db.AutoMigrate(&Event{}) + db.AutoMigrate(&dbstr.Match{}) + db.AutoMigrate(&dbstr.Event{}) - m.Get("/", handler_index) // / (GET) - основная страница, шаблон "active.tmpl" - m.Get("/add", handler_add) // /add(GET) - страница для создания матча, шаблон "add.tmpl" - m.Get("/error", handler_error) // /error(GET) - страница для ошибок, *разрабатывается* - m.Get("/end", handler_end) // /end(GET) - мгновенно окончить матч, шаблон отсутствует - m.Get("/medit", handler_edit_main) // /medit(GET) - редактор матча, шаблон "medit.tmpl" - m.Get("/events", handler_events_get) // /events(GET) - редактор событий, шаблон "events.tmpl" + m.Get("/", handler.handler_index) // / (GET) - основная страница, шаблон "active.tmpl" + m.Get("/add", handler.handler_add) // /add(GET) - страница для создания матча, шаблон "add.tmpl" + m.Get("/error", handler.handler_error) // /error(GET) - страница для ошибок, *разрабатывается* + m.Get("/end", handler.handler_end) // /end(GET) - мгновенно окончить матч, шаблон отсутствует + m.Get("/medit", handler.handler_edit_main) // /medit(GET) - редактор матча, шаблон "medit.tmpl" + m.Get("/events", handler.handler_events_get) // /events(GET) - редактор событий, шаблон "events.tmpl" - m.Post("/edit", handler_edit) // /edit(POST) - обновляет события в / - m.Post("/events", handler_events_post) // /events(POST) - добавляет события в /events(GET) + m.Post("/edit", handler.handler_edit) // /edit(POST) - обновляет события в / + m.Post("/events", handler.handler_events_post) // /events(POST) - добавляет события в /events(GET) m.Run("0.0.0.0", 8080) log.Print("Server started at *:8080") -} - -func handler_index(ctx *macaron.Context) { - var match Match - var events_team1 Match_events - var events_team2 Match_events - db.First(&match, "Active = ?", 1) - if match.Active != 0 { - json.Unmarshal([]byte(match.Arr1), &events_team1) - json.Unmarshal([]byte(match.Arr2), &events_team2) - ctx.Data["Matchinfo"] = match - ctx.Data["Team1"] = events_team1 - ctx.Data["Team2"] = events_team2 - ctx.HTML(200, "active") - } else { - ctx.Redirect("/add") - } -} - -func handler_error() string { - return "Произошла какая-то ошибка!" -} - -func handler_add(ctx *macaron.Context) { - var match Match - db.First(&match, "Active = ?", 1) - if match.Active != 0 { - ctx.Redirect("/error") - } else { - key := ctx.Req.Request.URL.Query() - if len(key) < 3{ - ctx.HTML(200, "add") - } else if key["team1"] == nil && key["team2"] == nil && key["stadium"] == nil { - ctx.HTML(200, "add") - } else { - team1 := strings.Join(key["team1"], " ") - team2 := strings.Join(key["team2"], " ") - stadium := strings.Join(key["stadium"], " ") - db.Create(&Match{Team1: team1, Team2: team2, Stadium: stadium, Active: 1}) - ctx.Redirect("/") - } - } -} - -func handler_edit_main(ctx *macaron.Context) { - var match Match - db.First(&match, "Active = ?", 1) - if match.Active != 0 { - key := ctx.Req.Request.URL.Query() - if len(key) < 5{ - ctx.Data["Matchinfo"] = match - ctx.HTML(200, "medit") - } else if key["team1"] == nil && key["team2"] == nil && key["stadium"] == nil && key["score1"] == nil && key["score2"] == nil { - ctx.Data["Matchinfo"] = match - ctx.HTML(200, "medit") - } else { - team1 := strings.Join(key["team1"], " ") - team2 := strings.Join(key["team2"], " ") - - stadium := strings.Join(key["stadium"], " ") - - score1, _ := strconv.Atoi(strings.Join(key["score1"], " ")) - score2, _ := strconv.Atoi(strings.Join(key["score2"], " ")) - - db.Model(&match).Update("Team1", team1) - db.Model(&match).Update("Team2", team2) - - db.Model(&match).Update("Stadium", stadium) - - db.Model(&match).Update("Score1", score1) - db.Model(&match).Update("Score2", score2) - ctx.Redirect("/") - } - } else { - ctx.Redirect("/error") - } -} - -func handler_edit(ctx *macaron.Context) { - var match Match - db.First(&match, "Active = ?", 1) - if match.Active != 0 { - r := ctx.Req.Request - r.ParseForm() // Parses the request body - arr1 := r.Form.Get("arr1") // x will be "" if parameter is not set - arr2 := r.Form.Get("arr2") - if len(arr1) > 0 && len(arr2) > 0 { - db.Model(&match).Update("Arr1", arr1) - db.Model(&match).Update("Arr2", arr2) - ctx.Redirect("/") - } else { - ctx.Redirect("/error") - } - } else { - ctx.Redirect("/error") - } -} - -func handler_end(ctx *macaron.Context) { - var match Match - db.First(&match, "Active = ?", 1) - if match.Active != 0 { - db.Model(&match).Update("Active", 0) - ctx.Redirect("/") - } else { - ctx.Redirect("/error") - } -} - - -func handler_events_get(ctx *macaron.Context) { - var match Match - db.First(&match, "Active = ?", 1) - if match.Active != 0 { - events := []Event {} - db.Find(&events, "Match_id = ?", match.ID) - ctx.Data["Events"] = events - ctx.Data["Matchinfo"] = match - ctx.HTML(200, "events") - } else { - ctx.Redirect("/error") - } -} - - -func handler_events_post(ctx *macaron.Context) { - var match Match - db.First(&match, "Active = ?", 1) - if match.Active != 0 { - r := ctx.Req.Request - r.ParseForm() - - Match_idc := r.Form.Get("match_id") - Teamc := r.Form.Get("team") - Eventc := r.Form.Get("event") - Minutec := r.Form.Get("minute") - - if len(Match_idc)>0 && len(Teamc)>0 && len(Eventc)>0 && len(Minutec)>0 { - Comment := r.Form.Get("comment") - Midd, _ := strconv.Atoi(Match_idc) - Teamd, _ := strconv.Atoi(Teamc) - Eventd, _ := strconv.Atoi(Eventc) - Minuted, _ := strconv.Atoi(Minutec) - if len(Comment)>0 { - db.Create(&Event{Match_id: Midd, Team: Teamd, Event: Eventd, Comment: Comment, Minute: Minuted}) - ctx.Redirect("/events") - } else { - db.Create(&Event{Match_id: Midd, Team: Teamd, Event: Eventd, Minute: Minuted}) - ctx.Redirect("/events") - } - } else { - ctx.Redirect("/error") - } - } else { - ctx.Redirect("/error") - } - } \ No newline at end of file diff --git a/structs/structs.go b/structs/structs.go new file mode 100644 index 0000000000000000000000000000000000000000..88045006299b1ddbc27558d6a4c809d2e81a7983 --- /dev/null +++ b/structs/structs.go @@ -0,0 +1,61 @@ +package structs + +// Удары по воротам +// Удары в створ +// Фолы +// Угловые +// Офсайды +// Предупрждения +// Удаления + +// Событие +// Гол +// Жёлтая карточка +// Красная карточка +// Удар по воротам +// Угловой +// Штрафной +// Опасный момент +// Замена +// Комментарий + +import ( + "github.com/jinzhu/gorm" + _ "github.com/jinzhu/gorm/dialects/sqlite" +) + +type Match struct { + gorm.Model + Team1 string + Team2 string + Arr1 string `sql:"DEFAULT:'{\"shoot1\": 0,\"shoot2\": 0,\"fols\": 0,\"ugls\": 0,\"offsides\": 0,\"yc\": 0,\"rc\": 0}'"` + Arr2 string `sql:"DEFAULT:'{\"shoot1\": 0,\"shoot2\": 0,\"fols\": 0,\"ugls\": 0,\"offsides\": 0,\"yc\": 0,\"rc\": 0}'"` + Score1 uint `sql:"DEFAULT:0"` + Score2 uint `sql:"DEFAULT:0"` + Stadium string + Active uint + } + + type errord struct { + Code string + } + + type Match_events struct { + Shoot1 int `json:"shoot1"` + Shoot2 int `json:"shoot2"` + Fols int `json:"fols"` + Ugls int `json:"ugls"` + Offsides int `json:"offsides"` + Yc int `json:"yc"` + Rc int `json:"rc"` + } + + + type Event struct { + gorm.Model + Match_id int + Team int + Event int + Comment string `sql:"DEFAULT:''` + Minute int + } \ No newline at end of file