// Copyright (C) 2024 veypi <i@veypi.com>
// 2025-03-04 16:08:06
// Distributed under terms of the MIT license.

package org

import (
	"github.com/veypi/vbase/auth"
	"github.com/veypi/vigo"
)

var Router = vigo.NewRouter()

func init() {
	Router.Get("/", "组织列表", auth.VBaseAuth.Perm("org:read"), list)
	Router.Post("/", "创建组织", auth.VBaseAuth.Perm("org:create"), create)
	Router.Get("/{org_id}", "获取组织详情", setOrgID, auth.VBaseAuth.Perm("org:read"), get)
	Router.Patch("/{org_id}", "更新组织", setOrgID, auth.VBaseAuth.Perm("org:update"), patch)
	Router.Delete("/{org_id}", "删除组织", setOrgID, auth.VBaseAuth.Perm("org:delete"), del)
	Router.Get("/tree", "组织树", auth.VBaseAuth.Perm("org:read"), tree)
	Router.Get("/{org_id}/members", "组织成员列表", setOrgID, auth.VBaseAuth.Perm("org:read"), listMembers)
}

func setOrgID(x *vigo.X) error {
	orgID := x.PathParams.Get("org_id")
	if orgID != "" {
		x.Set("org_id", orgID)
	}
	return nil
}
