2021-05-01 14:52:03 +03:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2023-03-03 22:40:50 +03:00
|
|
|
"github.com/go-chi/chi/v5"
|
2022-12-01 12:57:07 +03:00
|
|
|
"github.com/muety/wakapi/helpers"
|
2022-01-02 03:22:58 +03:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2021-05-01 14:52:03 +03:00
|
|
|
conf "github.com/muety/wakapi/config"
|
|
|
|
"github.com/muety/wakapi/middlewares"
|
|
|
|
"github.com/muety/wakapi/models"
|
|
|
|
v1 "github.com/muety/wakapi/models/compat/wakatime/v1"
|
2021-05-19 11:18:18 +03:00
|
|
|
routeutils "github.com/muety/wakapi/routes/utils"
|
2021-05-01 14:52:03 +03:00
|
|
|
"github.com/muety/wakapi/services"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ProjectsHandler struct {
|
|
|
|
config *conf.Config
|
|
|
|
userSrvc services.IUserService
|
|
|
|
heartbeatSrvc services.IHeartbeatService
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProjectsHandler(userService services.IUserService, heartbeatsService services.IHeartbeatService) *ProjectsHandler {
|
|
|
|
return &ProjectsHandler{
|
|
|
|
userSrvc: userService,
|
|
|
|
heartbeatSrvc: heartbeatsService,
|
|
|
|
config: conf.Get(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-03 22:40:50 +03:00
|
|
|
func (h *ProjectsHandler) RegisterRoutes(router chi.Router) {
|
|
|
|
router.Group(func(r chi.Router) {
|
|
|
|
r.Use(middlewares.NewAuthenticateMiddleware(h.userSrvc).Handler)
|
|
|
|
r.Get("/compat/wakatime/v1/users/{user}/projects", h.Get)
|
|
|
|
})
|
2021-05-01 14:52:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// @Summary Retrieve and fitler the user's projects
|
|
|
|
// @Description Mimics https://wakatime.com/developers#projects
|
|
|
|
// @ID get-wakatime-projects
|
|
|
|
// @Tags wakatime
|
|
|
|
// @Produce json
|
|
|
|
// @Param user path string true "User ID to fetch data for (or 'current')"
|
|
|
|
// @Param q query string true "Query to filter projects by"
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Success 200 {object} v1.ProjectsViewModel
|
2022-01-02 03:22:58 +03:00
|
|
|
// @Router /compat/wakatime/v1/users/{user}/projects [get]
|
2021-05-01 14:52:03 +03:00
|
|
|
func (h *ProjectsHandler) Get(w http.ResponseWriter, r *http.Request) {
|
2021-05-19 11:18:18 +03:00
|
|
|
user, err := routeutils.CheckEffectiveUser(w, r, h.userSrvc, "current")
|
|
|
|
if err != nil {
|
|
|
|
return // response was already sent by util function
|
2021-05-01 14:52:03 +03:00
|
|
|
}
|
|
|
|
|
2021-05-19 11:18:18 +03:00
|
|
|
results, err := h.heartbeatSrvc.GetEntitySetByUser(models.SummaryProject, user)
|
2021-05-01 14:52:03 +03:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
w.Write([]byte("something went wrong"))
|
|
|
|
conf.Log().Request(r).Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
q := r.URL.Query().Get("q")
|
|
|
|
|
|
|
|
projects := make([]*v1.Project, 0, len(results))
|
|
|
|
for _, p := range results {
|
|
|
|
if strings.HasPrefix(p, q) {
|
|
|
|
projects = append(projects, &v1.Project{ID: p, Name: p})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vm := &v1.ProjectsViewModel{Data: projects}
|
2022-12-01 12:57:07 +03:00
|
|
|
helpers.RespondJSON(w, r, http.StatusOK, vm)
|
2021-05-01 14:52:03 +03:00
|
|
|
}
|