92 lines
2.7 KiB
Go
92 lines
2.7 KiB
Go
package govndb
|
|
|
|
import "context"
|
|
|
|
var defaultClient = New()
|
|
|
|
const (
|
|
getVNFields = "title,alttitle,released,image{id,url},description,rating,tags{id,name,category},developers{id,name,original}"
|
|
getVNRatingFields = "rating"
|
|
getVNCharactersFields = "name,original,image{id,url},vns{role}"
|
|
searchVNFields = "title,alttitle"
|
|
)
|
|
|
|
// GetVN fetches a visual novel by VNDB ID using the package default client.
|
|
func GetVN(vnid string) (*VN, error) {
|
|
return defaultClient.GetVN(context.Background(), vnid)
|
|
}
|
|
|
|
// GetVNRating fetches only the rating for a visual novel using the package default client.
|
|
func GetVNRating(vnid string) (*float64, error) {
|
|
return defaultClient.GetVNRating(context.Background(), vnid)
|
|
}
|
|
|
|
// GetVNCharacters fetches the core character listing for a visual novel using the package default client.
|
|
func GetVNCharacters(vnid string) ([]Character, error) {
|
|
return defaultClient.GetVNCharacters(context.Background(), vnid)
|
|
}
|
|
|
|
// SearchVN searches visual novels by keyword using the package default client.
|
|
func SearchVN(keyword string) ([]VN, error) {
|
|
return defaultClient.SearchVN(context.Background(), keyword)
|
|
}
|
|
|
|
// GetVN fetches a visual novel by VNDB ID with a compact set of core fields.
|
|
func (c *Client) GetVN(ctx context.Context, vnid string) (*VN, error) {
|
|
response, err := c.QueryVNs(ctx, QueryRequest{
|
|
Filters: []any{"id", "=", vnid},
|
|
Fields: getVNFields,
|
|
Results: 1,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(response.Results) == 0 {
|
|
return nil, nil
|
|
}
|
|
return &response.Results[0], nil
|
|
}
|
|
|
|
// GetVNRating fetches only the rating for a visual novel.
|
|
//
|
|
// It returns nil when the VN does not exist or when no rating is available.
|
|
func (c *Client) GetVNRating(ctx context.Context, vnid string) (*float64, error) {
|
|
response, err := c.QueryVNs(ctx, QueryRequest{
|
|
Filters: []any{"id", "=", vnid},
|
|
Fields: getVNRatingFields,
|
|
Results: 1,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(response.Results) == 0 {
|
|
return nil, nil
|
|
}
|
|
return response.Results[0].Rating, nil
|
|
}
|
|
|
|
// GetVNCharacters fetches all characters linked to a visual novel with core display fields.
|
|
func (c *Client) GetVNCharacters(ctx context.Context, vnid string) ([]Character, error) {
|
|
response, err := c.QueryCharacters(ctx, QueryRequest{
|
|
Filters: []any{"vn", "=", []any{"id", "=", vnid}},
|
|
Fields: getVNCharactersFields,
|
|
Results: 100,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return response.Results, nil
|
|
}
|
|
|
|
// SearchVN searches visual novels by keyword with a minimal result payload.
|
|
func (c *Client) SearchVN(ctx context.Context, keyword string) ([]VN, error) {
|
|
response, err := c.QueryVNs(ctx, QueryRequest{
|
|
Filters: []any{"search", "=", keyword},
|
|
Fields: searchVNFields,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return response.Results, nil
|
|
}
|