//
// crud.go
// Copyright (C) 2025 veypi <i@veypi.com>
// 2025-05-06 15:12
// Distributed under terms of the MIT license.
//

package role

import (
	"github.com/veypi/vbase/cfg"
	"github.com/veypi/vbase/models"
	"github.com/veypi/vigo"
)

type patchOpts struct {
	ID     string  `json:"id"  src:"path@id" desc:"记录ID"`
	Status *string `json:"status"  src:"json" desc:"状态"`
}

var _ = Router.Patch("/{id}", "更新用户角色", userRolePatch)

func userRolePatch(x *vigo.X, opts *patchOpts) (*models.UserRole, error) {
	data := &models.UserRole{}
	err := cfg.DB().Where("id = ?", opts.ID).First(data).Error
	if err != nil {
		return nil, err
	}
	optsMap := make(map[string]any)
	if opts.Status != nil {
		optsMap["status"] = opts.Status
	}
	err = cfg.DB().Model(data).Updates(optsMap).Error

	return data, err
}
