package author

import (
	"context"
	v1 "server/api/author/v1"
	"server/internal/model"
	"server/internal/service"
	"server/utility/ecode"

	"github.com/gogf/gf/v2/frame/g"
)

func (c *ControllerV1) Follow(ctx context.Context, req *v1.FollowReq) (res *v1.FollowRes, err error) {
	// 从 ctx 获取用户ID
	userId := g.RequestFromCtx(ctx).GetCtxVar("id").Int64()
	if userId == 0 {
		return nil, ecode.Logout
	}

	out, err := service.UserFollowAuthor().Create(ctx, &model.UserFollowAuthorAddIn{
		UserId:   userId,
		AuthorId: req.AuthorId,
	})
	if err != nil {
		return nil, err
	}

	return &v1.FollowRes{
		Success: out.Success,
	}, nil
}
