parent
8bf90bea0b
commit
4d797c8e7d
@ -0,0 +1,55 @@
|
|||||||
|
package list
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/mnrva-dev/owltier.com/server/auth"
|
||||||
|
"github.com/mnrva-dev/owltier.com/server/db"
|
||||||
|
"github.com/mnrva-dev/owltier.com/server/jsend"
|
||||||
|
)
|
||||||
|
|
||||||
|
func DeleteList(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// get user from session key
|
||||||
|
sessC, err := r.Cookie(auth.SESSION_COOKIE)
|
||||||
|
if err != nil {
|
||||||
|
jsend.Fail(w, 401, map[string]interface{}{
|
||||||
|
"session": "invalid session",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
session := sessC.Value
|
||||||
|
var user = &db.UserSchema{}
|
||||||
|
err = db.FetchByGsi(&db.UserSchema{
|
||||||
|
Session: session,
|
||||||
|
}, user)
|
||||||
|
if err != nil {
|
||||||
|
jsend.Fail(w, 401, map[string]interface{}{
|
||||||
|
"session": "invalid session",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// get list id
|
||||||
|
r.ParseForm()
|
||||||
|
listId := r.FormValue("id")
|
||||||
|
list := &List{Id: listId}
|
||||||
|
// make sure user owns list
|
||||||
|
err = db.Fetch(list, list)
|
||||||
|
if err != nil {
|
||||||
|
jsend.Fail(w, 404, map[string]interface{}{
|
||||||
|
"id": "list not found",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if list.CreatedBy != user.Username {
|
||||||
|
jsend.Fail(w, 401, map[string]interface{}{
|
||||||
|
"username": "user does not own this list",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// delete list
|
||||||
|
db.Delete(list)
|
||||||
|
|
||||||
|
jsend.Success(w, map[string]interface{}{
|
||||||
|
"id": list.Id,
|
||||||
|
})
|
||||||
|
}
|
||||||
@ -0,0 +1,28 @@
|
|||||||
|
package list
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
|
"github.com/mnrva-dev/owltier.com/server/db"
|
||||||
|
"github.com/mnrva-dev/owltier.com/server/jsend"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetList(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// get iist from db
|
||||||
|
listId := chi.URLParam(r, "id")
|
||||||
|
list := &List{}
|
||||||
|
list.Id = listId
|
||||||
|
err := db.Fetch(list, list)
|
||||||
|
if err != nil {
|
||||||
|
jsend.Fail(w, 404, map[string]interface{}{
|
||||||
|
"id": "list not found",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// return list
|
||||||
|
jsend.Success(w, map[string]interface{}{
|
||||||
|
"list": list,
|
||||||
|
})
|
||||||
|
}
|
||||||
@ -0,0 +1,62 @@
|
|||||||
|
package list
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"math/rand"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/mnrva-dev/owltier.com/server/auth"
|
||||||
|
"github.com/mnrva-dev/owltier.com/server/db"
|
||||||
|
"github.com/mnrva-dev/owltier.com/server/jsend"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ID_LENGTH = 10
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewList(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
// get user from session key
|
||||||
|
sessC, err := r.Cookie(auth.SESSION_COOKIE)
|
||||||
|
if err != nil {
|
||||||
|
jsend.Fail(w, 401, map[string]interface{}{
|
||||||
|
"session": "invalid session",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
session := sessC.Value
|
||||||
|
var user = &db.UserSchema{}
|
||||||
|
err = db.FetchByGsi(&db.UserSchema{
|
||||||
|
Session: session,
|
||||||
|
}, user)
|
||||||
|
if err != nil {
|
||||||
|
jsend.Fail(w, 401, map[string]interface{}{
|
||||||
|
"session": "invalid session",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// create list object from user and json data
|
||||||
|
list := &List{}
|
||||||
|
json.NewDecoder(r.Body).Decode(list)
|
||||||
|
list.CreatedAt = time.Now().Unix()
|
||||||
|
list.CreatedBy = user.Username
|
||||||
|
list.Id = makeId(ID_LENGTH)
|
||||||
|
|
||||||
|
// create list in db
|
||||||
|
db.Create(list)
|
||||||
|
|
||||||
|
jsend.Success(w, map[string]interface{}{
|
||||||
|
"slug": "/list/" + list.Id,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func makeId(n int) string {
|
||||||
|
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||||
|
b := make([]rune, n)
|
||||||
|
for i := range b {
|
||||||
|
b[i] = letters[rand.Intn(len(letters))]
|
||||||
|
}
|
||||||
|
return string(b)
|
||||||
|
}
|
||||||
@ -0,0 +1,13 @@
|
|||||||
|
package list
|
||||||
|
|
||||||
|
import "github.com/go-chi/chi/v5"
|
||||||
|
|
||||||
|
func BuildRouter() *chi.Mux {
|
||||||
|
r := chi.NewRouter()
|
||||||
|
|
||||||
|
r.Post("/new", NewList)
|
||||||
|
r.Post("/delete", DeleteList) // should this even exist?
|
||||||
|
r.Get("/{id}", GetList)
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
Loading…
Reference in new issue