You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
1.9 KiB

  1. package main
  2. import (
  3. "net/http"
  4. "github.com/gorilla/mux"
  5. "errors"
  6. "strconv"
  7. "git.wegmueller.it/fruitsteps/framework/router"
  8. "git.wegmueller.it/fruitsteps/framework/controller"
  9. "git.wegmueller.it/fruitsteps/framework/util"
  10. "git.wegmueller.it/fruitsteps/framework/models"
  11. "git.wegmueller.it/toasterson/glog"
  12. )
  13. type MainController struct {
  14. controller.ListController
  15. }
  16. func (c *MainController) Init() {
  17. c.SuperInit()
  18. c.TplName = "index"
  19. c.ItemName = "tiles"
  20. c.ItemList = &tiles
  21. c.Routes = []router.Route{
  22. {http.MethodGet, "/"},
  23. }
  24. }
  25. type TileListController struct {
  26. controller.ListController
  27. }
  28. func (c *TileListController) Init() {
  29. c.SuperInit()
  30. c.TplName = "tile_list"
  31. c.ItemName = "tiles"
  32. c.ItemList = &tiles
  33. c.Routes = []router.Route{
  34. {http.MethodGet, "/tiles"},
  35. }
  36. }
  37. type TileDetailController struct {
  38. controller.Controller
  39. }
  40. func (c *TileDetailController) Init() {
  41. c.SuperInit()
  42. c.TplName = "tile_view"
  43. c.Routes = []router.Route{
  44. {http.MethodGet, "/tiles/{id}"},
  45. }
  46. }
  47. func (c *TileDetailController) Get() error {
  48. vars := mux.Vars(c.R)
  49. id, err := strconv.Atoi(vars["id"])
  50. util.Must(err)
  51. //TODO Load from DB Here
  52. var ok bool
  53. c.Data["tile"], ok = tiles[id]
  54. if !ok {
  55. return errors.New("Does Not Exist yet Maybe create it?")
  56. }
  57. return c.Render()
  58. }
  59. type NewTileController struct {
  60. controller.Controller
  61. }
  62. func (c *NewTileController) Init() {
  63. c.SuperInit()
  64. c.TplName = "tile_form"
  65. c.EnableAuthProtection()
  66. c.Routes = []router.Route{
  67. {http.MethodGet, "/tiles/new"},
  68. {http.MethodPost, "/tiles/new"},
  69. }
  70. }
  71. func (c *NewTileController) Get() error {
  72. return c.Render()
  73. }
  74. func (c *NewTileController) Post() error {
  75. name := c.PostFormValue("name")
  76. p := Tile{
  77. name,
  78. c.PostFormValue("text"),
  79. []Tile{},
  80. []string{},
  81. []string{},
  82. models.User{},
  83. 0,
  84. Asessment{},
  85. }
  86. //TODO Database Saving Here
  87. key := len(tiles)
  88. tiles[key] = p
  89. glog.Tracef("%v", tiles)
  90. http.Redirect(c.W, c.R, strconv.Itoa(key), http.StatusSeeOther)
  91. return nil
  92. }