Fixed lint + fmt

This commit is contained in:
konrad 2018-07-10 14:02:23 +02:00 committed by kolaente
parent c8622b0029
commit 237874eda6
Signed by untrusted user: konrad
GPG Key ID: F40E70337AB24C9B
25 changed files with 94 additions and 70 deletions

View File

@ -1,7 +1,8 @@
package models package models
// CRUDable defines the crud methods
type CRUDable interface { type CRUDable interface {
Create(*User) (error) Create(*User) error
ReadOne(int64) error ReadOne(int64) error
ReadAll(*User) (interface{}, error) ReadAll(*User) (interface{}, error)
Update(int64, *User) error Update(int64, *User) error

View File

@ -149,7 +149,7 @@ type ErrNeedToBeListAdmin struct {
UserID int64 UserID int64
} }
// IsErrListDoesNotExist checks if an error is a ErrListDoesNotExist. // IsErrNeedToBeListAdmin checks if an error is a ErrListDoesNotExist.
func IsErrNeedToBeListAdmin(err error) bool { func IsErrNeedToBeListAdmin(err error) bool {
_, ok := err.(ErrNeedToBeListAdmin) _, ok := err.(ErrNeedToBeListAdmin)
return ok return ok
@ -176,12 +176,12 @@ func (err ErrListItemCannotBeEmpty) Error() string {
return fmt.Sprintf("List item text cannot be empty.") return fmt.Sprintf("List item text cannot be empty.")
} }
// ErrListItemCannotBeEmpty represents a "ErrListDoesNotExist" kind of error. Used if the list does not exist. // ErrListItemDoesNotExist represents a "ErrListDoesNotExist" kind of error. Used if the list does not exist.
type ErrListItemDoesNotExist struct { type ErrListItemDoesNotExist struct {
ID int64 ID int64
} }
// IsErrListItemCannotBeEmpty checks if an error is a ErrListDoesNotExist. // IsErrListItemDoesNotExist checks if an error is a ErrListDoesNotExist.
func IsErrListItemDoesNotExist(err error) bool { func IsErrListItemDoesNotExist(err error) bool {
_, ok := err.(ErrListItemDoesNotExist) _, ok := err.(ErrListItemDoesNotExist)
return ok return ok
@ -232,7 +232,7 @@ type ErrNeedToBeNamespaceOwner struct {
UserID int64 UserID int64
} }
// IsErrNamespaceDoesNotExist checks if an error is a ErrNamespaceDoesNotExist. // IsErrNeedToBeNamespaceOwner checks if an error is a ErrNamespaceDoesNotExist.
func IsErrNeedToBeNamespaceOwner(err error) bool { func IsErrNeedToBeNamespaceOwner(err error) bool {
_, ok := err.(ErrNeedToBeNamespaceOwner) _, ok := err.(ErrNeedToBeNamespaceOwner)
return ok return ok
@ -289,4 +289,3 @@ func IsErrUserDoesNotHaveWriteAccessToNamespace(err error) bool {
func (err ErrUserDoesNotHaveWriteAccessToNamespace) Error() string { func (err ErrUserDoesNotHaveWriteAccessToNamespace) Error() string {
return fmt.Sprintf("You need to have write access to this namespace to do that [NamespaceID: %d, UserID: %d]", err.NamespaceID, err.UserID) return fmt.Sprintf("You need to have write access to this namespace to do that [NamespaceID: %d, UserID: %d]", err.NamespaceID, err.UserID)
} }

View File

@ -1,11 +1,11 @@
package models package models
import ( import (
"fmt"
"github.com/labstack/echo" "github.com/labstack/echo"
"strconv" "strconv"
) )
// GetIntURLParam is a helper method which returns an int from an url param
func GetIntURLParam(param string, c echo.Context) (intParam int64, err error) { func GetIntURLParam(param string, c echo.Context) (intParam int64, err error) {
id := c.Param(param) id := c.Param(param)
@ -16,6 +16,7 @@ func GetIntURLParam(param string, c echo.Context) (intParam int64, err error) {
return intParam, err return intParam, err
} }
// GetByID gets an object by its ID
func GetByID(id int64, result interface{}) (err error) { func GetByID(id int64, result interface{}) (err error) {
exists, err := x.ID(id).Get(result) exists, err := x.ID(id).Get(result)
if err != nil { if err != nil {
@ -28,9 +29,3 @@ func GetByID(id int64, result interface{}) (err error) {
return return
} }
func GetAllByUser(user *User, result interface{}) (err error) {
fmt.Println(result)
err = x.Where("owner_id = ", user.ID).Find(result)
return
}

View File

@ -19,7 +19,8 @@ func CreateOrUpdateList(list *List) (err error) {
} }
func (l *List) Update(id int64, doer *User) (err error) { // Update implements the update method of CRUDable
func (l *List) Update(id int64, doer *User) (err error) {
l.ID = id l.ID = id
// Check if it exists // Check if it exists
@ -35,12 +36,13 @@ func (l *List) Update(id int64, doer *User) (err error) {
} }
if !oldList.IsAdmin(&user) { if !oldList.IsAdmin(&user) {
return ErrNeedToBeListAdmin{ListID:id, UserID:user.ID} return ErrNeedToBeListAdmin{ListID: id, UserID: user.ID}
} }
return CreateOrUpdateList(l) return CreateOrUpdateList(l)
} }
// Create implements the create method of CRUDable
func (l *List) Create(doer *User) (err error) { func (l *List) Create(doer *User) (err error) {
// Check rights // Check rights
user, _, err := GetUserByID(doer.ID) user, _, err := GetUserByID(doer.ID)
@ -54,10 +56,10 @@ func (l *List) Create(doer *User) (err error) {
return return
} }
if !namespace.CanWrite(doer) { if !namespace.CanWrite(doer) {
return ErrUserDoesNotHaveWriteAccessToNamespace{UserID:user.ID, NamespaceID:namespace.ID} return ErrUserDoesNotHaveWriteAccessToNamespace{UserID: user.ID, NamespaceID: namespace.ID}
} }
l.Owner.ID = user.ID l.Owner.ID = user.ID
return CreateOrUpdateList(l) return CreateOrUpdateList(l)
} }

View File

@ -1,5 +1,6 @@
package models package models
// Delete implements the delete method of CRUDable
func (l *List) Delete(id int64, doer *User) (err error) { func (l *List) Delete(id int64, doer *User) (err error) {
// Check if the list exists // Check if the list exists
list, err := GetListByID(id) list, err := GetListByID(id)
@ -14,7 +15,7 @@ func (l *List) Delete(id int64, doer *User) (err error) {
} }
if !list.IsAdmin(&user) { if !list.IsAdmin(&user) {
return ErrNeedToBeListAdmin{ListID:id, UserID:user.ID} return ErrNeedToBeListAdmin{ListID: id, UserID: user.ID}
} }
// Delete the list // Delete the list

View File

@ -70,6 +70,7 @@ func GetItemsByListID(listID int64) (items []*ListItem, err error) {
return return
} }
// GetListItemByID returns all items a list has
func GetListItemByID(listItemID int64) (listItem ListItem, err error) { func GetListItemByID(listItemID int64) (listItem ListItem, err error) {
exists, err := x.ID(listItemID).Get(&listItem) exists, err := x.ID(listItemID).Get(&listItem)
if err != nil { if err != nil {

View File

@ -15,7 +15,7 @@ type List struct {
Updated int64 `xorm:"updated" json:"updated"` Updated int64 `xorm:"updated" json:"updated"`
CRUDable `xorm:"-" json:"-"` CRUDable `xorm:"-" json:"-"`
Rights `xorm:"-" json:"-"` Rights `xorm:"-" json:"-"`
} }
// Lists is a multiple of list // Lists is a multiple of list
@ -45,13 +45,14 @@ func GetListByID(id int64) (list List, err error) {
return list, nil return list, nil
} }
// GetListsByNamespaceID gets all lists in a namespace
func GetListsByNamespaceID(nID int64) (lists []*List, err error) { func GetListsByNamespaceID(nID int64) (lists []*List, err error) {
err = x.Where("namespace_id = ?", nID).Find(&lists) err = x.Where("namespace_id = ?", nID).Find(&lists)
return lists, err return lists, err
} }
// ReadAll gets all List a user has access to // ReadAll gets all List a user has access to
func (list *List) ReadAll(user *User) (interface{}, error) { func (l *List) ReadAll(user *User) (interface{}, error) {
lists := Lists{} lists := Lists{}
fullUser, _, err := GetUserByID(user.ID) fullUser, _, err := GetUserByID(user.ID)
if err != nil { if err != nil {
@ -75,6 +76,7 @@ func (l *List) ReadOne(id int64) (err error) {
return return
} }
// IsAdmin returns whether the user has admin rights on the list or not
func (l *List) IsAdmin(user *User) bool { func (l *List) IsAdmin(user *User) bool {
// Owners are always admins // Owners are always admins
if l.Owner.ID == user.ID { if l.Owner.ID == user.ID {
@ -89,4 +91,4 @@ func (l *List) IsAdmin(user *User) bool {
// TODO // TODO
return false return false
} }

View File

@ -1,5 +1,6 @@
package models package models
// DeleteNamespaceByID deletes a namespace and takes its id as an argument
func DeleteNamespaceByID(namespaceID int64, doer *User) (err error) { func DeleteNamespaceByID(namespaceID int64, doer *User) (err error) {
// Check if the namespace exists // Check if the namespace exists

View File

@ -13,7 +13,7 @@ type Namespace struct {
Updated int64 `xorm:"updated" json:"updated"` Updated int64 `xorm:"updated" json:"updated"`
CRUDable `xorm:"-" json:"-"` CRUDable `xorm:"-" json:"-"`
Rights `xorm:"-" json:"-"` Rights `xorm:"-" json:"-"`
} }
// TableName makes beautiful table names // TableName makes beautiful table names
@ -39,6 +39,7 @@ const (
NamespaceRightAdmin NamespaceRightAdmin
) )
// IsNamespaceAdmin returns whether the usre has admin rights in a namespace
func (user *User) IsNamespaceAdmin(namespace *Namespace) (err error) { func (user *User) IsNamespaceAdmin(namespace *Namespace) (err error) {
// Owners always have admin rights // Owners always have admin rights
if user.ID == namespace.Owner.ID { if user.ID == namespace.Owner.ID {
@ -50,6 +51,7 @@ func (user *User) IsNamespaceAdmin(namespace *Namespace) (err error) {
return ErrUserNeedsToBeNamespaceAdmin{UserID: user.ID, NamespaceID: namespace.ID} return ErrUserNeedsToBeNamespaceAdmin{UserID: user.ID, NamespaceID: namespace.ID}
} }
// HasNamespaceAccess checks if the User has namespace read access
func (user *User) HasNamespaceAccess(namespace *Namespace) (err error) { func (user *User) HasNamespaceAccess(namespace *Namespace) (err error) {
// Owners always have access // Owners always have access
if user.ID == namespace.Owner.ID { if user.ID == namespace.Owner.ID {
@ -61,6 +63,7 @@ func (user *User) HasNamespaceAccess(namespace *Namespace) (err error) {
return ErrUserDoesNotHaveAccessToNamespace{UserID: user.ID, NamespaceID: namespace.ID} return ErrUserDoesNotHaveAccessToNamespace{UserID: user.ID, NamespaceID: namespace.ID}
} }
// CanWrite checks if a user has write access to a namespace
func (n *Namespace) CanWrite(user *User) bool { func (n *Namespace) CanWrite(user *User) bool {
if err := user.HasNamespaceAccess(n); err != nil { if err := user.HasNamespaceAccess(n); err != nil {
return false return false
@ -69,6 +72,7 @@ func (n *Namespace) CanWrite(user *User) bool {
return true return true
} }
// HasNamespaceWriteAccess checks if a user has write access to a namespace
func (user *User) HasNamespaceWriteAccess(namespace *Namespace) (err error) { func (user *User) HasNamespaceWriteAccess(namespace *Namespace) (err error) {
// Owners always have access // Owners always have access
@ -81,6 +85,7 @@ func (user *User) HasNamespaceWriteAccess(namespace *Namespace) (err error) {
return ErrUserDoesNotHaveAccessToNamespace{UserID: user.ID, NamespaceID: namespace.ID} return ErrUserDoesNotHaveAccessToNamespace{UserID: user.ID, NamespaceID: namespace.ID}
} }
// GetNamespaceByID returns a namespace object by its ID
func GetNamespaceByID(id int64) (namespace Namespace, err error) { func GetNamespaceByID(id int64) (namespace Namespace, err error) {
namespace.ID = id namespace.ID = id
exists, err := x.Get(&namespace) exists, err := x.Get(&namespace)

View File

@ -1,5 +1,6 @@
package models package models
// Rights defines rights methods
type Rights interface { type Rights interface {
IsAdmin(*User) bool IsAdmin(*User) bool
CanWrite(*User) bool CanWrite(*User) bool

View File

@ -34,7 +34,7 @@ func (TeamMember) TableName() string {
return "team_members" return "team_members"
} }
// TeamNamespaces defines the relationship between a Team and a Namespace // TeamNamespace defines the relationship between a Team and a Namespace
type TeamNamespace struct { type TeamNamespace struct {
ID int64 `xorm:"int(11) autoincr not null unique pk"` ID int64 `xorm:"int(11) autoincr not null unique pk"`
TeamID int64 `xorm:"int(11) autoincr not null"` TeamID int64 `xorm:"int(11) autoincr not null"`
@ -64,6 +64,7 @@ func (TeamList) TableName() string {
return "team_list" return "team_list"
} }
// GetAllTeamsByNamespaceID returns all teams for a namespace
func GetAllTeamsByNamespaceID(id int64) (teams []*Team, err error) { func GetAllTeamsByNamespaceID(id int64) (teams []*Team, err error) {
err = x.Table("teams"). err = x.Table("teams").
Join("INNER", "team_namespaces", "teams.id = team_id"). Join("INNER", "team_namespaces", "teams.id = team_id").

View File

@ -27,15 +27,16 @@ func (User) TableName() string {
return "users" return "users"
} }
// ApiUserPassword represents a user object without timestamps and a json password field. // APIUserPassword represents a user object without timestamps and a json password field.
type ApiUserPassword struct { type APIUserPassword struct {
ID int64 `json:"id"` ID int64 `json:"id"`
Username string `json:"username"` Username string `json:"username"`
Password string `json:"password"` Password string `json:"password"`
Email string `json:"email"` Email string `json:"email"`
} }
func (apiUser *ApiUserPassword) APIFormat() User { // APIFormat formats an API User into a normal user struct
func (apiUser *APIUserPassword) APIFormat() User {
return User{ return User{
ID: apiUser.ID, ID: apiUser.ID,
Username: apiUser.Username, Username: apiUser.Username,

View File

@ -7,6 +7,7 @@ import (
"strconv" "strconv"
) )
// DeleteListItemByIDtemByID is the web handler to delete a list item
func DeleteListItemByIDtemByID(c echo.Context) error { func DeleteListItemByIDtemByID(c echo.Context) error {
// swagger:operation DELETE /item/{itemID} lists deleteListItem // swagger:operation DELETE /item/{itemID} lists deleteListItem
// --- // ---

View File

@ -6,6 +6,7 @@ import (
"net/http" "net/http"
) )
// GetListsByNamespaceID is the web handler to delete a namespace
func GetListsByNamespaceID(c echo.Context) error { func GetListsByNamespaceID(c echo.Context) error {
// swagger:operation GET /namespaces/{namespaceID}/lists namespaces getListsByNamespace // swagger:operation GET /namespaces/{namespaceID}/lists namespaces getListsByNamespace
// --- // ---

View File

@ -1,13 +1,14 @@
package v1 package v1
import ( import (
// "git.kolaente.de/konrad/list/models" // "git.kolaente.de/konrad/list/models"
"github.com/labstack/echo" "github.com/labstack/echo"
// "net/http" // "net/http"
// "strconv" // "strconv"
"net/http" "net/http"
) )
// DeleteListByID ...
func DeleteListByID(c echo.Context) error { func DeleteListByID(c echo.Context) error {
// swagger:operation DELETE /lists/{listID} lists deleteList // swagger:operation DELETE /lists/{listID} lists deleteList
// --- // ---
@ -35,34 +36,34 @@ func DeleteListByID(c echo.Context) error {
// "$ref": "#/responses/Message" // "$ref": "#/responses/Message"
/* /*
// Check if we have our ID // Check if we have our ID
id := c.Param("id") id := c.Param("id")
// Make int // Make int
itemID, err := strconv.ParseInt(id, 10, 64) itemID, err := strconv.ParseInt(id, 10, 64)
if err != nil { if err != nil {
return c.JSON(http.StatusBadRequest, models.Message{"Invalid ID."}) return c.JSON(http.StatusBadRequest, models.Message{"Invalid ID."})
}
// Check if the user has the right to delete that list
user, err := models.GetCurrentUser(c)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
}
// err = models.DeleteListByID(itemID, &user)
if err != nil {
if models.IsErrNeedToBeListAdmin(err) {
return c.JSON(http.StatusForbidden, models.Message{"You need to be the list owner to delete a list."})
} }
if models.IsErrListDoesNotExist(err) { // Check if the user has the right to delete that list
return c.JSON(http.StatusNotFound, models.Message{"This list does not exist."}) user, err := models.GetCurrentUser(c)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
} }
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."}) // err = models.DeleteListByID(itemID, &user)
} if err != nil {
if models.IsErrNeedToBeListAdmin(err) {
return c.JSON(http.StatusForbidden, models.Message{"You need to be the list owner to delete a list."})
}
return c.JSON(http.StatusOK, models.Message{"The list was deleted with success."}) if models.IsErrListDoesNotExist(err) {
return c.JSON(http.StatusNotFound, models.Message{"This list does not exist."})
}
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
}
return c.JSON(http.StatusOK, models.Message{"The list was deleted with success."})
*/ */
return echo.NewHTTPError(http.StatusNotImplemented) return echo.NewHTTPError(http.StatusNotImplemented)

View File

@ -7,6 +7,7 @@ import (
"strconv" "strconv"
) )
// AddListItem ...
func AddListItem(c echo.Context) error { func AddListItem(c echo.Context) error {
// swagger:operation PUT /lists/{listID} lists addListItem // swagger:operation PUT /lists/{listID} lists addListItem
// --- // ---
@ -46,6 +47,7 @@ func AddListItem(c echo.Context) error {
return updateOrCreateListItemHelper(listID, 0, c) return updateOrCreateListItemHelper(listID, 0, c)
} }
// UpdateListItem ...
func UpdateListItem(c echo.Context) error { func UpdateListItem(c echo.Context) error {
// swagger:operation PUT /item/{itemID} lists updateListItem // swagger:operation PUT /item/{itemID} lists updateListItem
// --- // ---

View File

@ -6,6 +6,7 @@ import (
"net/http" "net/http"
) )
// AddList ...
func AddList(c echo.Context) error { func AddList(c echo.Context) error {
// swagger:operation PUT /namespaces/{namespaceID}/lists lists addList // swagger:operation PUT /namespaces/{namespaceID}/lists lists addList
// --- // ---
@ -86,6 +87,7 @@ func AddList(c echo.Context) error {
return c.JSON(http.StatusOK, list) return c.JSON(http.StatusOK, list)
} }
// UpdateList ...
func UpdateList(c echo.Context) error { func UpdateList(c echo.Context) error {
// swagger:operation POST /lists/{listID} lists upadteList // swagger:operation POST /lists/{listID} lists upadteList
// --- // ---

View File

@ -7,6 +7,7 @@ import (
"strconv" "strconv"
) )
// AddNamespace ...
func AddNamespace(c echo.Context) error { func AddNamespace(c echo.Context) error {
// swagger:operation PUT /namespaces namespaces addNamespace // swagger:operation PUT /namespaces namespaces addNamespace
// --- // ---
@ -33,6 +34,7 @@ func AddNamespace(c echo.Context) error {
return addOrUpdateNamespace(c) return addOrUpdateNamespace(c)
} }
// UpdateNamespace ...
func UpdateNamespace(c echo.Context) error { func UpdateNamespace(c echo.Context) error {
// swagger:operation POST /namespaces/{namespaceID} namespaces upadteNamespace // swagger:operation POST /namespaces/{namespaceID} namespaces upadteNamespace
// --- // ---

View File

@ -7,6 +7,7 @@ import (
"strconv" "strconv"
) )
// DeleteNamespaceByID ...
func DeleteNamespaceByID(c echo.Context) error { func DeleteNamespaceByID(c echo.Context) error {
// swagger:operation DELETE /namespaces/{namespaceID} namespaces deleteNamespace // swagger:operation DELETE /namespaces/{namespaceID} namespaces deleteNamespace
// --- // ---

View File

@ -7,6 +7,7 @@ import (
"strconv" "strconv"
) )
// ShowNamespace ...
func ShowNamespace(c echo.Context) error { func ShowNamespace(c echo.Context) error {
// swagger:operation GET /namespaces/{namespaceID} namespaces getNamespace // swagger:operation GET /namespaces/{namespaceID} namespaces getNamespace
// --- // ---

View File

@ -6,6 +6,7 @@ import (
"net/http" "net/http"
) )
// GetAllNamespacesByCurrentUser ...
func GetAllNamespacesByCurrentUser(c echo.Context) error { func GetAllNamespacesByCurrentUser(c echo.Context) error {
// swagger:operation GET /namespaces namespaces getNamespaces // swagger:operation GET /namespaces namespaces getNamespaces
// --- // ---

View File

@ -12,7 +12,7 @@ type swaggerParameterBodies struct {
UserLogin models.UserLogin UserLogin models.UserLogin
// in:body // in:body
ApiUserPassword models.ApiUserPassword APIUserPassword models.APIUserPassword
// in:body // in:body
List models.List List models.List

View File

@ -7,6 +7,7 @@ import (
"strconv" "strconv"
) )
// RegisterUser ...
func RegisterUser(c echo.Context) error { func RegisterUser(c echo.Context) error {
// swagger:operation POST /register user register // swagger:operation POST /register user register
@ -20,7 +21,7 @@ func RegisterUser(c echo.Context) error {
// - name: body // - name: body
// in: body // in: body
// schema: // schema:
// "$ref": "#/definitions/ApiUserPassword" // "$ref": "#/definitions/APIUserPassword"
// responses: // responses:
// "200": // "200":
// "$ref": "#/responses/User" // "$ref": "#/responses/User"
@ -38,7 +39,7 @@ func userAddOrUpdate(c echo.Context) error {
// TODO: prevent everyone from updating users // TODO: prevent everyone from updating users
// Check for Request Content // Check for Request Content
var datUser *models.ApiUserPassword var datUser *models.APIUserPassword
if err := c.Bind(&datUser); err != nil { if err := c.Bind(&datUser); err != nil {
return c.JSON(http.StatusBadRequest, models.Message{"No user model provided."}) return c.JSON(http.StatusBadRequest, models.Message{"No user model provided."})

View File

@ -1,4 +1,4 @@
package CRUD package crud
import ( import (
"fmt" "fmt"
@ -7,16 +7,17 @@ import (
"net/http" "net/http"
) )
// WebHandler defines the webhandler object
// This does web stuff, aka returns json etc. Uses CRUDable Methods to get the data // This does web stuff, aka returns json etc. Uses CRUDable Methods to get the data
type CRUDWebHandler struct { type WebHandler struct {
CObject interface{ CObject interface {
models.CRUDable models.CRUDable
models.Rights models.Rights
} }
} }
// This does json, handles the request // ReadOneWeb is the webhandler to get one object
func (c *CRUDWebHandler) ReadOneWeb(ctx echo.Context) error { func (c *WebHandler) ReadOneWeb(ctx echo.Context) error {
// Get the ID // Get the ID
id, err := models.GetIntURLParam("id", ctx) id, err := models.GetIntURLParam("id", ctx)
@ -39,8 +40,8 @@ func (c *CRUDWebHandler) ReadOneWeb(ctx echo.Context) error {
return ctx.JSON(http.StatusOK, c.CObject) return ctx.JSON(http.StatusOK, c.CObject)
} }
// ReadAllWeb returns all elements of a type // ReadAllWeb is the webhandler to get all objects of a type
func (c *CRUDWebHandler) ReadAllWeb(ctx echo.Context) error { func (c *WebHandler) ReadAllWeb(ctx echo.Context) error {
currentUser, err := models.GetCurrentUser(ctx) currentUser, err := models.GetCurrentUser(ctx)
if err != nil { if err != nil {
return ctx.JSON(http.StatusInternalServerError, models.Message{"Could not determine the current user."}) return ctx.JSON(http.StatusInternalServerError, models.Message{"Could not determine the current user."})
@ -56,7 +57,7 @@ func (c *CRUDWebHandler) ReadAllWeb(ctx echo.Context) error {
} }
// UpdateWeb is the webhandler to update an object // UpdateWeb is the webhandler to update an object
func (c *CRUDWebHandler) UpdateWeb(ctx echo.Context) error { func (c *WebHandler) UpdateWeb(ctx echo.Context) error {
// Get the object // Get the object
if err := ctx.Bind(&c.CObject); err != nil { if err := ctx.Bind(&c.CObject); err != nil {
return ctx.JSON(http.StatusBadRequest, models.Message{"No model provided."}) return ctx.JSON(http.StatusBadRequest, models.Message{"No model provided."})
@ -88,7 +89,7 @@ func (c *CRUDWebHandler) UpdateWeb(ctx echo.Context) error {
} }
// CreateWeb is the handler to create an object // CreateWeb is the handler to create an object
func (c *CRUDWebHandler) CreateWeb(ctx echo.Context) error { func (c *WebHandler) CreateWeb(ctx echo.Context) error {
// Get the object // Get the object
if err := ctx.Bind(&c.CObject); err != nil { if err := ctx.Bind(&c.CObject); err != nil {
return ctx.JSON(http.StatusBadRequest, models.Message{"No model provided."}) return ctx.JSON(http.StatusBadRequest, models.Message{"No model provided."})
@ -111,7 +112,7 @@ func (c *CRUDWebHandler) CreateWeb(ctx echo.Context) error {
} }
// DeleteWeb is the web handler to delete something // DeleteWeb is the web handler to delete something
func (c *CRUDWebHandler) DeleteWeb(ctx echo.Context) error { func (c *WebHandler) DeleteWeb(ctx echo.Context) error {
// Get the ID // Get the ID
id, err := models.GetIntURLParam("id", ctx) id, err := models.GetIntURLParam("id", ctx)
if err != nil { if err != nil {
@ -138,4 +139,4 @@ func (c *CRUDWebHandler) DeleteWeb(ctx echo.Context) error {
} }
return ctx.JSON(http.StatusOK, models.Message{"Successfully deleted."}) return ctx.JSON(http.StatusOK, models.Message{"Successfully deleted."})
} }

View File

@ -1,3 +1,5 @@
package routes
// Package v1 List API. // Package v1 List API.
// //
// This documentation describes the List API. // This documentation describes the List API.
@ -23,14 +25,13 @@
// in: header // in: header
// //
// swagger:meta // swagger:meta
package routes
import ( import (
"github.com/labstack/echo" "github.com/labstack/echo"
"github.com/labstack/echo/middleware" "github.com/labstack/echo/middleware"
"git.kolaente.de/konrad/list/models" "git.kolaente.de/konrad/list/models"
CRUD "git.kolaente.de/konrad/list/routes/CRUD" crud "git.kolaente.de/konrad/list/routes/crud"
apiv1 "git.kolaente.de/konrad/list/routes/api/v1" apiv1 "git.kolaente.de/konrad/list/routes/api/v1"
_ "git.kolaente.de/konrad/list/routes/api/v1/swagger" // for docs generation _ "git.kolaente.de/konrad/list/routes/api/v1/swagger" // for docs generation
) )
@ -50,7 +51,6 @@ func NewEcho() *echo.Echo {
// RegisterRoutes registers all routes for the application // RegisterRoutes registers all routes for the application
func RegisterRoutes(e *echo.Echo) { func RegisterRoutes(e *echo.Echo) {
// TODO: Use proper cors middleware by echo // TODO: Use proper cors middleware by echo
// Middleware for cors // Middleware for cors
@ -87,7 +87,7 @@ func RegisterRoutes(e *echo.Echo) {
a.Use(middleware.JWT(models.Config.JWTLoginSecret)) a.Use(middleware.JWT(models.Config.JWTLoginSecret))
a.POST("/tokenTest", apiv1.CheckToken) a.POST("/tokenTest", apiv1.CheckToken)
listHandler := &CRUD.CRUDWebHandler{ listHandler := &crud.WebHandler{
CObject: &models.List{}, CObject: &models.List{},
} }
a.GET("/lists", listHandler.ReadAllWeb) a.GET("/lists", listHandler.ReadAllWeb)