2019-05-19 20:49:27 +03:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
2021-01-30 12:34:52 +03:00
|
|
|
"github.com/gorilla/mux"
|
2020-10-09 22:37:16 +03:00
|
|
|
conf "github.com/muety/wakapi/config"
|
2020-03-31 13:22:17 +03:00
|
|
|
"github.com/muety/wakapi/models"
|
2020-11-06 23:19:54 +03:00
|
|
|
"github.com/muety/wakapi/models/view"
|
2020-03-31 13:22:17 +03:00
|
|
|
"github.com/muety/wakapi/services"
|
|
|
|
"github.com/muety/wakapi/utils"
|
2020-09-06 13:15:46 +03:00
|
|
|
"net/http"
|
2019-05-19 21:06:07 +03:00
|
|
|
)
|
|
|
|
|
2019-05-19 20:49:27 +03:00
|
|
|
type SummaryHandler struct {
|
2020-10-09 22:37:16 +03:00
|
|
|
config *conf.Config
|
2020-11-08 12:12:49 +03:00
|
|
|
summarySrvc services.ISummaryService
|
2019-05-21 22:46:23 +03:00
|
|
|
}
|
|
|
|
|
2020-11-08 12:12:49 +03:00
|
|
|
func NewSummaryHandler(summaryService services.ISummaryService) *SummaryHandler {
|
2020-05-24 14:41:19 +03:00
|
|
|
return &SummaryHandler{
|
2020-05-24 22:19:05 +03:00
|
|
|
summarySrvc: summaryService,
|
2020-10-09 22:37:16 +03:00
|
|
|
config: conf.Get(),
|
2020-05-24 11:37:31 +03:00
|
|
|
}
|
2019-05-19 20:49:27 +03:00
|
|
|
}
|
|
|
|
|
2021-01-30 12:34:52 +03:00
|
|
|
func (h *SummaryHandler) RegisterRoutes(router *mux.Router) {
|
|
|
|
router.Methods(http.MethodGet).HandlerFunc(h.GetIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *SummaryHandler) RegisterAPIRoutes(router *mux.Router) {
|
|
|
|
router.Methods(http.MethodGet).HandlerFunc(h.ApiGet)
|
|
|
|
}
|
|
|
|
|
2020-03-31 12:24:44 +03:00
|
|
|
func (h *SummaryHandler) ApiGet(w http.ResponseWriter, r *http.Request) {
|
2020-09-06 13:15:46 +03:00
|
|
|
summary, err, status := h.loadUserSummary(r)
|
2020-02-20 16:28:55 +03:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(status)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
utils.RespondJSON(w, http.StatusOK, summary)
|
|
|
|
}
|
|
|
|
|
2020-06-07 20:28:32 +03:00
|
|
|
func (h *SummaryHandler) GetIndex(w http.ResponseWriter, r *http.Request) {
|
2020-05-24 14:41:19 +03:00
|
|
|
if h.config.IsDev() {
|
|
|
|
loadTemplates()
|
2020-02-20 17:39:56 +03:00
|
|
|
}
|
|
|
|
|
2020-02-20 17:54:20 +03:00
|
|
|
q := r.URL.Query()
|
|
|
|
if q.Get("interval") == "" && q.Get("from") == "" {
|
|
|
|
q.Set("interval", "today")
|
|
|
|
r.URL.RawQuery = q.Encode()
|
|
|
|
}
|
|
|
|
|
2020-09-06 13:15:46 +03:00
|
|
|
summary, err, status := h.loadUserSummary(r)
|
2020-02-20 16:28:55 +03:00
|
|
|
if err != nil {
|
2020-11-06 23:19:54 +03:00
|
|
|
w.WriteHeader(status)
|
|
|
|
templates[conf.SummaryTemplate].Execute(w, h.buildViewModel(r).WithError(err.Error()))
|
2020-05-24 22:19:05 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user := r.Context().Value(models.UserKey).(*models.User)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-03-31 12:24:44 +03:00
|
|
|
vm := models.SummaryViewModel{
|
|
|
|
Summary: summary,
|
2021-01-30 11:51:36 +03:00
|
|
|
LanguageColors: utils.FilterColors(h.config.App.GetLanguageColors(), summary.Languages),
|
|
|
|
EditorColors: utils.FilterColors(h.config.App.GetEditorColors(), summary.Editors),
|
|
|
|
OSColors: utils.FilterColors(h.config.App.GetOSColors(), summary.OperatingSystems),
|
2020-05-24 22:19:05 +03:00
|
|
|
ApiKey: user.ApiKey,
|
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-09-06 13:15:46 +03:00
|
|
|
func (h *SummaryHandler) loadUserSummary(r *http.Request) (*models.Summary, error, int) {
|
|
|
|
summaryParams, err := utils.ParseSummaryParams(r)
|
2019-05-19 20:49:27 +03:00
|
|
|
if err != nil {
|
2020-09-06 13:15:46 +03:00
|
|
|
return nil, err, http.StatusBadRequest
|
2019-05-19 21:34:26 +03:00
|
|
|
}
|
|
|
|
|
2020-11-07 14:01:35 +03:00
|
|
|
var retrieveSummary services.SummaryRetriever = h.summarySrvc.Retrieve
|
|
|
|
if summaryParams.Recompute {
|
|
|
|
retrieveSummary = h.summarySrvc.Summarize
|
|
|
|
}
|
|
|
|
|
|
|
|
summary, err := h.summarySrvc.Aliased(summaryParams.From, summaryParams.To, summaryParams.User, retrieveSummary)
|
2020-02-20 16:28:55 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err, http.StatusInternalServerError
|
2019-05-19 20:49:27 +03:00
|
|
|
}
|
|
|
|
|
2020-02-20 16:28:55 +03:00
|
|
|
return summary, nil, http.StatusOK
|
2019-05-19 21:14:57 +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"),
|
|
|
|
}
|
|
|
|
}
|