go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/projects/chirp/pkg/controller/home.go (about) 1 /* 2 3 Copyright (c) 2023 - Present. Will Charczuk. All rights reserved. 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository. 5 6 */ 7 8 package controller 9 10 import ( 11 "go.charczuk.com/sdk/apputil" 12 "go.charczuk.com/sdk/uuid" 13 "go.charczuk.com/sdk/web" 14 15 "go.charczuk.com/projects/chirp/pkg/config" 16 "go.charczuk.com/projects/chirp/pkg/model" 17 ) 18 19 type Home struct { 20 apputil.BaseController 21 22 Config config.Config 23 Model model.Manager 24 } 25 26 func (h Home) Register(app *web.App) { 27 app.Get("/home", web.NestMiddleware(h.home, web.SessionRequired)) 28 app.Get("/home/:after", web.NestMiddleware(h.homeAfter, web.SessionRequired)) 29 } 30 31 const DEFAULT_HOME_FEED_LENGTH = 50 32 33 func (h Home) home(r web.Context) web.Result { 34 userID := h.GetUserID(r) 35 feed, err := h.Model.FeedForUserID(r, userID, DEFAULT_HOME_FEED_LENGTH, nil) 36 if err != nil { 37 return r.Views().InternalError(err) 38 } 39 return r.Views().View("home", feed) 40 } 41 42 func (h Home) homeAfter(r web.Context) web.Result { 43 userID := h.GetUserID(r) 44 after, err := web.RouteValue[uuid.UUID](r, "after") 45 if err != nil { 46 return r.Views().BadRequest(err) 47 } 48 feed, err := h.Model.FeedForUserID(r, userID, DEFAULT_HOME_FEED_LENGTH, &after) 49 if err != nil { 50 return r.Views().InternalError(err) 51 } 52 return r.Views().View("home", feed) 53 }