-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
21 changed files
with
272 additions
and
191 deletions.
There are no files selected for viewing
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
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
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
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
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,33 @@ | ||
package typeaheadapi | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/PaluMacil/dwn/api" | ||
"github.com/PaluMacil/dwn/database" | ||
) | ||
|
||
type Module struct { | ||
Db *database.Database | ||
} | ||
|
||
func New(db *database.Database) *Module { | ||
return &Module{ | ||
Db: db, | ||
} | ||
} | ||
|
||
type TypeaheadRoute api.Route | ||
|
||
func (rt TypeaheadRoute) API() api.Route { | ||
return api.Route(rt) | ||
} | ||
|
||
// api/typeahead/... | ||
func (mod Module) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
route := TypeaheadRoute(api.GetRoute(w, r, mod.Db)) | ||
switch route.Endpoint { | ||
case "user": | ||
route.handleUser() | ||
} | ||
} |
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,31 @@ | ||
package typeaheadapi | ||
|
||
import ( | ||
"encoding/json" | ||
"net/url" | ||
) | ||
|
||
// api/typeahead/user?query=searchstring | ||
func (rt *TypeaheadRoute) handleUser() { | ||
switch rt.R.Method { | ||
case "GET": | ||
qry, err := url.QueryUnescape(rt.R.URL.Query().Get("query")) | ||
if len(qry) < 2 || err != nil { | ||
rt.API().ServeBadRequest() | ||
return | ||
} | ||
//TODO: only allow logged in users to search | ||
user, err := rt.Db.Users.CompletionSuggestions(qry) //TODO: check for absence of @ and search by username | ||
if err != nil { | ||
rt.API().ServeInternalServerError(err) | ||
return | ||
} | ||
if err := json.NewEncoder(rt.W).Encode(user); err != nil { | ||
rt.API().ServeInternalServerError(err) | ||
return | ||
} | ||
default: | ||
rt.API().ServeMethodNotAllowed() | ||
return | ||
} | ||
} |
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
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
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.