-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cf409d7
commit fe674a8
Showing
19 changed files
with
1,562 additions
and
177 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
@@ -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{} | ||
|
@@ -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) { | ||
|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.