diff --git a/Featurecreep.md b/Featurecreep.md index be6c9e54..a99b24fc 100644 --- a/Featurecreep.md +++ b/Featurecreep.md @@ -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` diff --git a/models/error.go b/models/error.go index 55de12ec..9908c045 100644 --- a/models/error.go +++ b/models/error.go @@ -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) +} diff --git a/models/team_namespace.go b/models/team_namespace.go new file mode 100644 index 00000000..589ade77 --- /dev/null +++ b/models/team_namespace.go @@ -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" +} diff --git a/models/team_namespace_create.go b/models/team_namespace_create.go new file mode 100644 index 00000000..e51e69d7 --- /dev/null +++ b/models/team_namespace_create.go @@ -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 +} diff --git a/models/team_namespace_rights.go b/models/team_namespace_rights.go new file mode 100644 index 00000000..b29dd629 --- /dev/null +++ b/models/team_namespace_rights.go @@ -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) +} diff --git a/models/teams.go b/models/teams.go index 92a32514..4fa8b8c6 100644 --- a/models/teams.go +++ b/models/teams.go @@ -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"` diff --git a/routes/crud/create.go b/routes/crud/create.go index a75e28a3..19b8e614 100644 --- a/routes/crud/create.go +++ b/routes/crud/create.go @@ -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) } diff --git a/routes/routes.go b/routes/routes.go index 5804c2f1..7e52ff60 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -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{}, }