package user import ( "log" "net/http" "strconv" "fagott.pw/charakterin" "fagott.pw/grilist/grilist" "fagott.pw/grilist/modules/lists" "github.com/julienschmidt/httprouter" ) type Module struct { g *grilist.Grilist lists *lists.Module } func New() *Module { return &Module{} } func (m *Module) Name() string { return "User" } func (m *Module) Init(g *grilist.Grilist) { m.g = g gm, ok := g.Modules["Lists"] if !ok { log.Fatal("tags: lists module not found") } m.lists = gm.(*lists.Module) m.g.Router.GET("/user/:id", m.viewUser) m.g.Router.GET("/user/:id/*rest", m.viewUser) } func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.DashboardCategory { return make([]grilist.DashboardCategory, 0) } func (m *Module) viewUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) { currentUser, _ := m.g.Charakterin.GetUserFromRequest(r) userID, err := strconv.Atoi(p.ByName("id")) if err != nil { http.Redirect(w, r, "/", 302) return } user := currentUser if user == nil || currentUser.ID != userID { user, err = m.g.Charakterin.GetUserByID(userID) if err != nil { http.Redirect(w, r, "/", 302) return } } userLists := m.lists.GetUserLists(user) data := m.g.Renderer.DefaultData() data["user"] = currentUser data["DisplayUser"] = user data["Lists"] = lists.ListsToCards(userLists) m.g.Renderer.RenderPage("user", w, data) }