summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrailFinder <wohilas@gmail.com>2024-06-02 15:00:41 +0300
committerGrailFinder <wohilas@gmail.com>2024-06-02 15:00:41 +0300
commit60fc1773241195f4988ed8066e019bbc42267085 (patch)
tree91dec5a4e298a8b3eb0870649ed8d5031a685001
parentd0ec9f38b17dadf4c244904e28e064d6ad7c5674 (diff)
Enha: add db method to list recs
-rw-r--r--cmd/start.go4
-rw-r--r--internal/database/repos/action.go12
-rw-r--r--internal/handlers/auth.go9
-rw-r--r--internal/handlers/elements.go15
-rw-r--r--internal/server/router.go2
5 files changed, 30 insertions, 12 deletions
diff --git a/cmd/start.go b/cmd/start.go
index 6bb16a3..fa00786 100644
--- a/cmd/start.go
+++ b/cmd/start.go
@@ -22,9 +22,8 @@ var startCmd = &cobra.Command{
Long: `Start data server`,
Run: func(cmd *cobra.Command, args []string) {
log := slog.New(slog.NewJSONHandler(os.Stdout,
- &slog.HandlerOptions{Level: slog.LevelDebug}))
+ &slog.HandlerOptions{Level: slog.LevelDebug, AddSource: true}))
// load server configuration from server
- log.Debug("Loading server configuration")
if viper.ConfigFileUsed() != "" {
log.Debug("Configuration file loaded", "section", "init",
"path", viper.ConfigFileUsed())
@@ -37,7 +36,6 @@ var startCmd = &cobra.Command{
}
srv := server.NewServer(cfg, log, db.Conn)
// listen for new messages
- log.Info("Listening for incoming events")
srv.Listen()
},
}
diff --git a/internal/database/repos/action.go b/internal/database/repos/action.go
index 49ad95e..ab1f559 100644
--- a/internal/database/repos/action.go
+++ b/internal/database/repos/action.go
@@ -8,6 +8,18 @@ type ActionRepo interface {
DBActionGetByName(name string) (*models.Action, error)
DBActionDone(name string) error
DBActionsToReset() error
+ DBActionRecommend(username string, at models.ActionType) ([]models.Action, error)
+}
+
+func (p *Provider) DBActionRecommend(
+ username string, at models.ActionType,
+) ([]models.Action, error) {
+ resp := []models.Action{}
+ query := `SELECT * FROM action WHERE username!=$1 AND type=$2 GROUP BY name LIMIT 5;`
+ if err := p.db.Select(&resp, query, username, at); err != nil {
+ return nil, err
+ }
+ return resp, nil
}
func (p *Provider) DBActionCreate(req *models.Action) error {
diff --git a/internal/handlers/auth.go b/internal/handlers/auth.go
index e147efc..b470c57 100644
--- a/internal/handlers/auth.go
+++ b/internal/handlers/auth.go
@@ -49,7 +49,7 @@ func (h *Handlers) HandleSignup(w http.ResponseWriter, r *http.Request) {
}
if err := h.repo.DBUserScoreCreate(newUser); err != nil {
msg := "failed to create user"
- h.log.Error(msg, "user", newUser)
+ h.log.Error(msg, "user", newUser, "error", err)
abortWithError(w, msg)
return
}
@@ -98,13 +98,6 @@ func (h *Handlers) HandleLogin(w http.ResponseWriter, r *http.Request) {
abortWithError(w, err.Error())
return
}
- // check password
- // hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), 8)
- // if err != nil {
- // h.log.Warn("failed to gen pass hash", "err", err)
- // abortWithError(w, err.Error())
- // return
- // }
if err := bcrypt.CompareHashAndPassword(
[]byte(userScore.Password), []byte(password),
); err != nil {
diff --git a/internal/handlers/elements.go b/internal/handlers/elements.go
index 965281a..7fcd9ce 100644
--- a/internal/handlers/elements.go
+++ b/internal/handlers/elements.go
@@ -1,6 +1,7 @@
package handlers
import (
+ "apjournal/internal/models"
"html/template"
"net/http"
)
@@ -24,6 +25,20 @@ func (h *Handlers) ServeHideForm(w http.ResponseWriter, r *http.Request) {
}
func (h *Handlers) ShowRecommended(w http.ResponseWriter, r *http.Request) {
+ var username string
+ // TODO: getusername
+ // TODO: get recommendations for user
+ plusA, err := h.repo.DBActionRecommend(username, models.ActionTypePlus)
+ if err != nil {
+ abortWithError(w, err.Error())
+ return
+ }
+ minusA, err := h.repo.DBActionRecommend(username, models.ActionTypeMinus)
+ if err != nil {
+ abortWithError(w, err.Error())
+ return
+ }
+ h.log.Debug("got actions", "plus#", len(plusA), "minus#", len(minusA))
tmpl, err := template.ParseGlob("components/*.html")
if err != nil {
abortWithError(w, err.Error())
diff --git a/internal/server/router.go b/internal/server/router.go
index f36e55f..670f750 100644
--- a/internal/server/router.go
+++ b/internal/server/router.go
@@ -32,6 +32,6 @@ func (srv *server) ListenToRequests() {
mux.HandleFunc("GET /recommendation", h.ShowRecommended)
mux.HandleFunc("GET /recommendation-hide", h.HideRecommended)
- fmt.Printf("Listening on %v\n", server.Addr)
+ fmt.Println("Listening", "addr", server.Addr)
server.ListenAndServe()
}