2020-03-31 07:38:45 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"suah.dev/cromp/db"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewUser is the handler for /user/new
|
|
|
|
func NewUser(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var u db.CreateUserParams
|
|
|
|
json.NewDecoder(r.Body).Decode(&u)
|
|
|
|
|
|
|
|
user, err := base.CreateUser(ctx, u)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
json.NewEncoder(w).Encode(user)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Auth is the handler for /usr/auth
|
|
|
|
func Auth(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var p db.AuthUserParams
|
|
|
|
json.NewDecoder(r.Body).Decode(&p)
|
|
|
|
|
|
|
|
user, err := base.AuthUser(ctx, p)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if user.Authed {
|
|
|
|
// TODO respond with token
|
|
|
|
json.NewEncoder(w).Encode(user)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-02 06:28:40 -06:00
|
|
|
// ListEntries handles requests to /entries/list
|
|
|
|
func ListEntries(w http.ResponseWriter, r *http.Request) {
|
|
|
|
user, err := getUser(r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(user.UserID)
|
|
|
|
entries, err := base.GetEntries(ctx, user.UserID)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
json.NewEncoder(w).Encode(entries)
|
|
|
|
}
|
|
|
|
|
2020-03-31 07:38:45 -06:00
|
|
|
// AddEntry handles requests to /entries/add
|
|
|
|
func AddEntry(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var e db.CreateEntryParams
|
|
|
|
|
2020-04-02 06:28:40 -06:00
|
|
|
err := json.NewDecoder(r.Body).Decode(&e)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := getUser(r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
e.UserID = user.UserID
|
2020-03-31 07:38:45 -06:00
|
|
|
|
|
|
|
entry, err := base.CreateEntry(ctx, e)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
json.NewEncoder(w).Encode(entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Entries handles requests to /entries
|
|
|
|
func Entries(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Print secret message
|
|
|
|
fmt.Fprintln(w, "The cake is a lie!")
|
|
|
|
}
|
|
|
|
|
2020-04-02 06:28:40 -06:00
|
|
|
// SimilarEntries are entries that match some text
|
2020-03-31 07:38:45 -06:00
|
|
|
func SimilarEntries(w http.ResponseWriter, r *http.Request) {
|
2020-04-02 06:28:40 -06:00
|
|
|
var e db.SimilarEntriesParams
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&e)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := getUser(r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
e.UserID = user.UserID
|
|
|
|
entries, err := base.SimilarEntries(ctx, e)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
json.NewEncoder(w).Encode(entries)
|
2020-03-31 07:38:45 -06:00
|
|
|
}
|