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 {
|
2020-04-30 06:16:50 -06:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
2020-03-31 07:38:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-04-15 07:39:08 -06:00
|
|
|
// UpdateEntries handles requests to /etries/update
|
|
|
|
func UpdateEntries(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var params db.UpdateEntryParams
|
|
|
|
|
|
|
|
err := json.NewDecoder(r.Body).Decode(¶ms)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
params.UserID = user.UserID
|
|
|
|
|
|
|
|
entry, err := base.UpdateEntry(ctx, params)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
json.NewEncoder(w).Encode(entry)
|
|
|
|
}
|
|
|
|
|
2020-03-31 07:38:45 -06:00
|
|
|
// Entries handles requests to /entries
|
|
|
|
func Entries(w http.ResponseWriter, r *http.Request) {
|
2020-04-15 07:39:08 -06:00
|
|
|
var params db.GetEntryParams
|
|
|
|
|
|
|
|
err := json.NewDecoder(r.Body).Decode(¶ms)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
params.UserID = user.UserID
|
|
|
|
|
|
|
|
entry, err := base.GetEntry(ctx, params)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
json.NewEncoder(w).Encode(entry)
|
2020-03-31 07:38:45 -06:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|