...

Source file src/jschan/app/boardlist.go

Documentation: jschan/app

     1  package jschan
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"jschan/app/models"
     7  	"net/http"
     8  	"net/url"
     9  )
    10  
    11  type GetWebringResponse struct {
    12  	*models.Webring
    13  }
    14  
    15  func (c *Client) GetWebring(ctx context.Context) (*GetWebringResponse, error) {
    16  
    17  	url := fmt.Sprintf("%s/webring.json", c.BaseURL)
    18  
    19  	req, err := http.NewRequest(http.MethodGet, url, nil)
    20  	if err != nil {
    21  		return nil, err
    22  	}
    23  
    24  	req = req.WithContext(ctx)
    25  
    26  	res := GetWebringResponse{}
    27  	if err := c.sendRequest(req, &res, nil); err != nil {
    28  		return nil, err
    29  	}
    30  
    31  	return &res, nil
    32  
    33  }
    34  
    35  type GetBoardsResponse struct {
    36  	Boards  []models.Board `json:"boards"`
    37  	Page    int            `json:"page"`
    38  	MaxPage int            `json:"maxPage"`
    39  }
    40  
    41  type GetBoardsPublicOptions struct {
    42  	Search        string
    43  	Sort          string
    44  	SortDirection string
    45  	Page          int
    46  	LocalFirst    bool
    47  	Sites         []string
    48  }
    49  
    50  func (c *Client) GetBoardsPublic(ctx context.Context, options *GetBoardsPublicOptions) (*GetBoardsResponse, error) {
    51  
    52  	page := 1
    53  	search := ""
    54  	sort := "popularity"
    55  	direction := "desc"
    56  	local_first := false
    57  	sites := []string{}
    58  	if options != nil {
    59  		search = options.Search
    60  		sort = options.Sort
    61  		direction = options.SortDirection
    62  		local_first = options.LocalFirst
    63  		sites = options.Sites
    64  		page = options.Page
    65  	}
    66  
    67  	query := url.Values{}
    68  	query.Set("search", search)
    69  	query.Set("page", fmt.Sprintf("%d", page))
    70  	query.Set("sort", sort)
    71  	query.Set("direction", direction)
    72  	if local_first {
    73  		query.Set("local_first", "true")
    74  	}
    75  	for _, site := range sites {
    76  		query.Add("sites", site)
    77  	}
    78  
    79  	url := fmt.Sprintf("%s/boards.json?%s", c.BaseURL, query.Encode())
    80  
    81  	req, err := http.NewRequest(http.MethodGet, url, nil)
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  
    86  	req = req.WithContext(ctx)
    87  
    88  	res := GetBoardsResponse{}
    89  	if err := c.sendRequest(req, &res, nil); err != nil {
    90  		return nil, err
    91  	}
    92  
    93  	return &res, nil
    94  
    95  }
    96  

View as plain text