implemented create team <-> namespace relation

This commit is contained in:
konrad 2018-07-17 08:44:21 +02:00 committed by kolaente
parent 2e3c10be13
commit 7feb82702d
Signed by untrusted user: konrad
GPG Key ID: F40E70337AB24C9B
8 changed files with 80 additions and 17 deletions

View File

@ -133,7 +133,7 @@ Teams sind global, d.h. Ein Team kann mehrere Namespaces verwalten.
*Routen*
* [ ] `namespaces/:id/teams`
* [ ] Create
* [x] Create
* [ ] ReadAll
* [ ] Delete
* [ ] `lists/:id/teams`

View File

@ -387,3 +387,18 @@ func IsErrTeamDoesNotExist(err error) bool {
func (err ErrTeamDoesNotExist) Error() string {
return fmt.Sprintf("Team does not exist [Team ID: %d]", err.TeamID)
}
// ErrInvalidTeamRight represents an error where a team right is invalid
type ErrInvalidTeamRight struct {
Right NamespaceRight
}
// IsErrInvalidTeamRight checks if an error is ErrInvalidTeamRight.
func IsErrInvalidTeamRight(err error) bool {
_, ok := err.(ErrInvalidTeamRight)
return ok
}
func (err ErrInvalidTeamRight) Error() string {
return fmt.Sprintf("The right is invalid [Right: %d]", err.Right)
}

20
models/team_namespace.go Normal file
View File

@ -0,0 +1,20 @@
package models
// TeamNamespace defines the relationship between a Team and a Namespace
type TeamNamespace struct {
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"`
TeamID int64 `xorm:"int(11) not null" json:"team_id"`
NamespaceID int64 `xorm:"int(11) not null" json:"namespace_id"`
Right NamespaceRight `xorm:"int(11)" json:"right"`
Created int64 `xorm:"created" json:"created"`
Updated int64 `xorm:"updated" json:"updated"`
CRUDable `xorm:"-" json:"-"`
Rights `xorm:"-" json:"-"`
}
// TableName makes beautiful table names
func (TeamNamespace) TableName() string {
return "team_namespaces"
}

View File

@ -0,0 +1,27 @@
package models
// Create creates a new team <-> namespace relation
func (tn *TeamNamespace) Create(doer *User, nID int64) (err error) {
// Check if the rights are valid
if tn.Right != NamespaceRightAdmin && tn.Right != NamespaceRightRead && tn.Right != NamespaceRightWrite {
return ErrInvalidTeamRight{tn.Right}
}
// Check if the team exists
_, err = GetTeamByID(tn.TeamID)
if err != nil {
return
}
// Check if the namespace exists
_, err = GetNamespaceByID(nID)
if err != nil {
return
}
tn.NamespaceID = nID
// Insert the new team
_, err = x.Insert(tn)
return
}

View File

@ -0,0 +1,7 @@
package models
// CanCreate checks if one can create a new team <-> namespace relation
func (tn *TeamNamespace) CanCreate(user *User, _ int64) bool {
n, _ := GetNamespaceByID(tn.NamespaceID)
return n.IsAdmin(user)
}

View File

@ -60,22 +60,6 @@ type TeamUser struct {
IsAdmin bool `json:"is_admin"`
}
// TeamNamespace defines the relationship between a Team and a Namespace
type TeamNamespace struct {
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"`
TeamID int64 `xorm:"int(11) not null" json:"team_id"`
NamespaceID int64 `xorm:"int(11) not null" json:"namespace_id"`
Rights NamespaceRight `xorm:"int(11)" json:"rights"`
Created int64 `xorm:"created" json:"created"`
Updated int64 `xorm:"updated" json:"updated"`
}
// TableName makes beautiful table names
func (TeamNamespace) TableName() string {
return "team_namespaces"
}
// TeamList defines the relation between a team and a list
type TeamList struct {
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"`

View File

@ -1,6 +1,7 @@
package crud
import (
"fmt"
"git.kolaente.de/konrad/list/models"
"github.com/labstack/echo"
"net/http"
@ -62,6 +63,8 @@ func (c *WebHandler) CreateWeb(ctx echo.Context) error {
return echo.NewHTTPError(http.StatusBadRequest, "The team name cannot be empty.")
}
fmt.Println(err)
return echo.NewHTTPError(http.StatusInternalServerError)
}

View File

@ -113,6 +113,13 @@ func RegisterRoutes(e *echo.Echo) {
a.DELETE("/namespaces/:id", namespaceHandler.DeleteWeb)
a.GET("/namespaces/:id/lists", apiv1.GetListsByNamespaceID)
namespaceTeamHandler := &crud.WebHandler{
CObject: &models.TeamNamespace{},
}
a.GET("/namespaces/:id/teams", namespaceTeamHandler.ReadAllWeb)
a.PUT("/namespaces/:id/teams", namespaceTeamHandler.CreateWeb)
a.DELETE("/namespaces/:nid/teams/:id", namespaceTeamHandler.DeleteWeb)
teamHandler := &crud.WebHandler{
CObject: &models.Team{},
}