//
// del.go
// Copyright (C) 2024 veypi <i@veypi.com>
// Distributed under terms of the MIT license.
//

package access

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

type deleteOpts struct {
	ID string `src:"path@id" desc:"记录ID"`
}

var _ = Router.Delete("/{id}", "删除访问权限", deleteAccess)

type deleteResponse struct {
	Message string `json:"message"`
}

func deleteAccess(x *vigo.X, opts *deleteOpts) (*deleteResponse, error) {
	// 查找记录
	var access models.Access
	if err := cfg.DB().Where("id = ?", opts.ID).First(&access).Error; err != nil {
		return nil, vigo.NewError("未找到资源").WithCode(404)
	}

	// 删除记录
	if err := cfg.DB().Delete(&access).Error; err != nil {
		return nil, err
	}

	return &deleteResponse{Message: "删除成功"}, nil
}
