Skip to content

Commit

Permalink
add finders
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanfaerman committed Feb 8, 2024
1 parent cf409d7 commit fe674a8
Show file tree
Hide file tree
Showing 19 changed files with 1,562 additions and 177 deletions.
72 changes: 72 additions & 0 deletions internal/dao/search.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions internal/handlers/account.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package handlers

import (
"fmt"
"net/http"
"strings"

"github.com/davecgh/go-spew/spew"
"github.com/go-chi/chi"

validator "github.com/go-playground/validator/v10"
Expand All @@ -12,6 +14,8 @@ import (
"github.com/ryanfaerman/netctl/internal/services"
"github.com/ryanfaerman/netctl/internal/views"
"github.com/ryanfaerman/netctl/web/named"

. "github.com/ryanfaerman/netctl/internal/models/finders"
)

type account struct{}
Expand Down Expand Up @@ -41,6 +45,8 @@ func (h account) Routes(r chi.Router) {
// r.Get(named.Route("settings-sessions", "/settings/sessions"), h.Edit)
r.Get(named.Route("settings", "/settings/{namespace}"), h.Settings)
r.Post(named.Route("settings-save", "/settings/{namespace}/-/save"), h.SettingsSave)

r.Get("/-/find-accounts", h.Find)
}

func (h account) Setup(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -277,3 +283,54 @@ func (h account) Update(w http.ResponseWriter, r *http.Request) {

v.EditForm().Render(ctx, w)
}

func (h account) Find(w http.ResponseWriter, r *http.Request) {
fmt.Println("ByEmail:")
results, err := FindOne[models.Account](r.Context(), ByEmail("[email protected]"))
if err != nil {
ErrorHandler(err)(w, r)
return
}
spew.Dump(results)

fmt.Println("ByCallsign:")
results, err = FindOne[models.Account](r.Context(), ByCallsign("kq4jxi"))
if err != nil {
ErrorHandler(err)(w, r)
return
}
spew.Dump(results)

fmt.Println("ByID:")
results, err = FindOne[models.Account](r.Context(), ByID(1))
if err != nil {
ErrorHandler(err)(w, r)
return
}
spew.Dump(results)

fmt.Println("ByDistance:")
account := services.Session.GetAccount(r.Context())
if account.IsAnonymous() {
ErrorHandler(fmt.Errorf("you must be logged in to use this feature"))(w, r)
return
}
lat, lon, err := services.Account.Geolocation(r.Context(), account)
if err != nil {
ErrorHandler(err)(w, r)
return
}
found, err := Find[models.Account](r.Context(), ByDistance(lat, lon, 10*1.609344), ByKind(int(models.AccountKindClub)))
if err != nil {
ErrorHandler(err)(w, r)
return
}
spew.Dump(found)
// fmt.Println("ByNothing:")
// results, err = FindOne[models.Account](r.Context())
// if err != nil {
// ErrorHandler(err)(w, r)
// return
// }
// spew.Dump(results)
}
84 changes: 84 additions & 0 deletions internal/handlers/search.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package handlers

import (
"fmt"
"net/http"
"strconv"
"strings"

"github.com/go-chi/chi"
"github.com/ryanfaerman/netctl/internal/models"
"github.com/ryanfaerman/netctl/internal/services"
"github.com/ryanfaerman/netctl/internal/views"
"github.com/ryanfaerman/netctl/web/named"
)

type Search struct{}

func init() {
global.handlers = append(global.handlers, Search{})
}

func (h Search) Routes(r chi.Router) {
r.Use(services.Session.Middleware)
r.Get(named.Route("find-local", "/find/local/{kind}"), h.Search)
}

func (h Search) Search(w http.ResponseWriter, r *http.Request) {
distanceStr := r.URL.Query().Get("distance")
unit := r.URL.Query().Get("unit")

kind := models.ParseAccountKind(chi.URLParam(r, "kind"))

if distanceStr == "" {
distanceStr = "5"
}
if unit == "" {
unit = "miles"
}

v := views.Search{
Distance: distanceStr,
Unit: unit,
Kind: kind,
}

distance, err := strconv.ParseFloat(distanceStr, 64)
if err != nil {
ErrorHandler(err)(w, r)
return
}
if strings.ToLower(unit) == "miles" {
distance = distance * 1.609344
}

account := services.Session.GetAccount(r.Context())
if account.IsAnonymous() {
ErrorHandler(fmt.Errorf("you must be logged in to use this feature"))(w, r)
return
}
lat, lon, err := services.Account.Geolocation(r.Context(), account)
if err != nil {
ErrorHandler(err)(w, r)
return
}

input := services.CallsignsWithinRangeParams{
Latitude: lat,
Longitude: lon,
Distance: distance,
Kind: int(kind),
}

results, err := services.Search.CallsignsWithinRange(r.Context(), input)
if err != nil {
ErrorHandler(err)(w, r)
return
}
v.Found = results
if r.Header.Get("HX-Request") == "true" {
v.ResultList().Render(r.Context(), w)
return
}
v.Results().Render(r.Context(), w)
}
105 changes: 36 additions & 69 deletions internal/models/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package models
import (
"context"
"encoding/gob"
"encoding/json"
"errors"
"fmt"
"runtime"
"strings"
"time"

"dario.cat/mergo"
"github.com/ryanfaerman/netctl/internal/dao"

. "github.com/ryanfaerman/netctl/internal/models/finders"
)

type AccountKind int
Expand All @@ -20,6 +21,7 @@ const (
AccountKindUser AccountKind = iota // user
AccountKindClub // club
AccountKindOrganization // organization
AccoundKindAny // any
)

func ParseAccountKind(s string) AccountKind {
Expand Down Expand Up @@ -52,6 +54,8 @@ type Account struct {
CreatedAt time.Time
DeletedAt time.Time
Deleted bool

Distance float64
}

func init() {
Expand Down Expand Up @@ -176,79 +180,42 @@ func (m *Account) Organizations(ctx context.Context) ([]*Membership, error) {
}

func FindAccountByID(ctx context.Context, id int64) (*Account, error) {
raw, err := global.dao.GetAccount(ctx, id)
if err != nil {
return nil, err
}
u := Account{
ID: raw.ID,
Name: raw.Name,
About: raw.About,
Kind: AccountKind(raw.Kind),
CreatedAt: raw.Createdat,
}
if raw.Deletedat.Valid {
u.DeletedAt = raw.Deletedat.Time
u.Deleted = true
}

if err := json.Unmarshal([]byte(raw.Settings), &u.Settings); err != nil {
fmt.Println("error unmarshalling settings", err)
return &u, err
}

if err := mergo.Merge(&u.Settings, DefaultSettings); err != nil {
panic(err.Error())
return &u, err
}

return &u, nil
_, file, line, _ := runtime.Caller(1)
global.log.Warn(
"DEPRECATED FUNCTION",
"func", "FindAccountByID",
"use", "FindOne[Account](ctx, ByID(id))",
"file", file,
"line", line,
)

return FindOne[Account](ctx, ByID(id))
}

func FindAccountByEmail(ctx context.Context, email string) (*Account, error) {
raw, err := global.dao.FindAccountByEmail(ctx, email)
if err != nil {
return nil, err
}
u := Account{
ID: raw.ID,
Name: raw.Name,
About: raw.About,
Kind: AccountKind(raw.Kind),
CreatedAt: raw.Createdat,
Settings: DefaultSettings,
}
if raw.Deletedat.Valid {
u.DeletedAt = raw.Deletedat.Time
u.Deleted = true
}
if err := json.Unmarshal([]byte(raw.Settings), &u.Settings); err != nil {
return &u, err
}
return &u, nil
_, file, line, _ := runtime.Caller(1)
global.log.Warn(
"DEPRECATED FUNCTION",
"func", "FindAccountByEmail",
"use", "FindOne[Account](ctx, ByEmail(email))",
"file", file,
"line", line,
)

return FindOne[Account](ctx, ByEmail(email))
}

func FindAccountByCallsign(ctx context.Context, callsign string) (*Account, error) {
raw, err := global.dao.FindAccountByCallsign(ctx, callsign)
if err != nil {
return nil, err
}
u := Account{
ID: raw.ID,
Name: raw.Name,
About: raw.About,
Kind: AccountKind(raw.Kind),
CreatedAt: raw.Createdat,
Settings: DefaultSettings,
}
if raw.Deletedat.Valid {
u.DeletedAt = raw.Deletedat.Time
u.Deleted = true
}
if err := json.Unmarshal([]byte(raw.Settings), &u.Settings); err != nil {
return &u, err
}
return &u, nil
_, file, line, _ := runtime.Caller(1)
global.log.Warn(
"DEPRECATED FUNCTION",
"func", "FindAccountByCallsign",
"use", "FindOne[Account](ctx, ByCallsign(email))",
"file", file,
"line", line,
)

return FindOne[Account](ctx, ByCallsign(callsign))
}

func (u *Account) Callsigns() ([]Callsign, error) {
Expand Down
Loading

0 comments on commit fe674a8

Please sign in to comment.