1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00
wakapi/routes/summary.go

99 lines
3.2 KiB
Go
Raw Normal View History

package routes
import (
"github.com/gorilla/mux"
2020-10-09 22:37:16 +03:00
conf "github.com/muety/wakapi/config"
"github.com/muety/wakapi/helpers"
"github.com/muety/wakapi/middlewares"
"github.com/muety/wakapi/models"
2020-11-06 23:19:54 +03:00
"github.com/muety/wakapi/models/view"
su "github.com/muety/wakapi/routes/utils"
2020-03-31 13:22:17 +03:00
"github.com/muety/wakapi/services"
"github.com/muety/wakapi/utils"
"fmt"
"net/http"
2019-05-19 21:06:07 +03:00
)
type SummaryHandler struct {
2020-10-09 22:37:16 +03:00
config *conf.Config
userSrvc services.IUserService
summarySrvc services.ISummaryService
2019-05-21 22:46:23 +03:00
}
func NewSummaryHandler(summaryService services.ISummaryService, userService services.IUserService) *SummaryHandler {
return &SummaryHandler{
summarySrvc: summaryService,
userSrvc: userService,
2020-10-09 22:37:16 +03:00
config: conf.Get(),
}
}
func (h *SummaryHandler) RegisterRoutes(router *mux.Router) {
r1 := router.PathPrefix("/summary").Subrouter()
r1.Use(middlewares.NewAuthenticateMiddleware(h.userSrvc).WithRedirectTarget(defaultErrorRedirectTarget()).Handler)
r1.Methods(http.MethodGet).HandlerFunc(h.GetIndex)
r2 := router.PathPrefix("/summary").Subrouter()
r2.Use(middlewares.NewAuthenticateMiddleware(h.userSrvc).WithRedirectTarget(defaultErrorRedirectTarget()).Handler)
r2.Methods(http.MethodGet).HandlerFunc(h.GetIndex)
2020-02-20 16:28:55 +03:00
}
func (h *SummaryHandler) GetIndex(w http.ResponseWriter, r *http.Request) {
if h.config.IsDev() {
loadTemplates()
2020-02-20 17:39:56 +03:00
}
rawQuery := r.URL.RawQuery
2020-02-20 17:54:20 +03:00
q := r.URL.Query()
if q.Get("interval") == "" && q.Get("from") == "" {
// If the PersistentIntervalKey cookie is set, redirect to the correct summary page
if intervalCookie, _ := r.Cookie(models.PersistentIntervalKey); intervalCookie != nil {
redirectAddress := fmt.Sprintf("%s/summary?interval=%s", h.config.Server.BasePath, intervalCookie.Value)
http.Redirect(w, r, redirectAddress, http.StatusFound)
}
2020-02-20 17:54:20 +03:00
q.Set("interval", "today")
r.URL.RawQuery = q.Encode()
} else if q.Get("interval") != "" {
// Send a Set-Cookie header to persist the interval
headerValue := fmt.Sprintf("%s=%s", models.PersistentIntervalKey, q.Get("interval"))
w.Header().Add("Set-Cookie", headerValue)
2020-02-20 17:54:20 +03:00
}
summaryParams, _ := helpers.ParseSummaryParams(r)
summary, err, status := su.LoadUserSummary(h.summarySrvc, r)
2020-02-20 16:28:55 +03:00
if err != nil {
2020-11-06 23:19:54 +03:00
w.WriteHeader(status)
conf.Log().Request(r).Error("failed to load summary - %v", err)
2020-11-06 23:19:54 +03:00
templates[conf.SummaryTemplate].Execute(w, h.buildViewModel(r).WithError(err.Error()))
return
}
user := middlewares.GetPrincipal(r)
if user == nil {
2020-11-06 23:19:54 +03:00
w.WriteHeader(http.StatusUnauthorized)
templates[conf.SummaryTemplate].Execute(w, h.buildViewModel(r).WithError("unauthorized"))
2020-02-20 16:28:55 +03:00
return
}
2021-12-16 14:39:41 +03:00
vm := view.SummaryViewModel{
2020-03-31 12:24:44 +03:00
Summary: summary,
SummaryParams: summaryParams,
User: user,
EditorColors: utils.FilterColors(h.config.App.GetEditorColors(), summary.Editors),
LanguageColors: utils.FilterColors(h.config.App.GetLanguageColors(), summary.Languages),
OSColors: utils.FilterColors(h.config.App.GetOSColors(), summary.OperatingSystems),
ApiKey: user.ApiKey,
RawQuery: rawQuery,
2020-03-31 12:24:44 +03:00
}
2020-10-09 22:37:16 +03:00
templates[conf.SummaryTemplate].Execute(w, vm)
2020-02-20 16:28:55 +03:00
}
2020-11-06 23:19:54 +03:00
func (h *SummaryHandler) buildViewModel(r *http.Request) *view.SummaryViewModel {
return &view.SummaryViewModel{
Success: r.URL.Query().Get("success"),
Error: r.URL.Query().Get("error"),
}
}