Show
Ignore:
Timestamp:
08/26/10 20:21:08 (21 months ago)
Author:
Antti-Juhani Kaijanaho <antti-juhani@…>
Children:
6c86e7ccc32f1c99d204262844a52b1b06359145
Parents:
7ccc366e52cc1e51e123b39fccaf56267361ca58
git-committer:
Antti-Juhani Kaijanaho <antti-juhani@…> (08/26/10 20:21:08)
Message:

Add support for paginating thread and article lists.

Signed-off-by: Antti-Juhani Kaijanaho <antti-juhani@…>

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • http/threads.cc

    r751fac6 r43f5212  
    9494                else 
    9595                { 
     96                        std::string f = req->get_form_field("s"); 
     97                        size_t start = util::is_number(f) 
     98                                ? util::parse_nonnegative_integer<size_t>(f) 
     99                                : 0; 
     100                        f = req->get_form_field("n"); 
     101                        size_t length = util::is_number(f) 
     102                                ? util::parse_nonnegative_integer<size_t>(f) 
     103                                : 10; 
     104 
    96105                        tlate::list_value::ptr tl(new tlate::list_value); 
    97106                        std::list<db::thread_node::ptr> thrs  
     
    100109                        thrs.sort(db::thread_node::cmp_latest_date_rev); 
    101110 
     111                        size_t cur = 0; 
     112                        bool more = false; 
    102113                        for (std::list<db::thread_node::ptr>::const_iterator 
    103114                                     it = thrs.begin(); 
    104115                             it != thrs.end(); it++) 
    105116                        { 
    106                                 tl->push_back(tlate::msg_value::mk(*it, u)); 
     117                                cur++; 
     118                                if (cur >= start + length) 
     119                                { 
     120                                        more = true; 
     121                                        break; 
     122                                } 
     123                                if (cur > start) 
     124                                        tl->push_back 
     125                                                (tlate::msg_value::mk(*it, u)); 
    107126                        } 
    108127                        am->insert("threads", tl); 
     128 
     129                        tlate::data_model::ptr pg(new tlate::data_model); 
     130                        pg->insert("first", start); 
     131                        pg->insert("last", cur - 1); 
     132                        pg->insert("size", cur - start); 
     133                        pg->insert("max_size", length); 
     134 
     135                        if (more) 
     136                        { 
     137                                ::uri u = req->get_uri(); 
     138                                u.replace_query_param("s", start + length); 
     139                                u.replace_query_param("n", length); 
     140                                pg->insert("next", u.to_string()); 
     141                        } 
     142                         
     143                        if (start > 0) 
     144                        { 
     145                                ::uri u = req->get_uri(); 
     146                                u.replace_query_param("s", 
     147                                                      start > length 
     148                                                      ? start - length 
     149                                                      : 0); 
     150                                u.replace_query_param("n", length); 
     151                                pg->insert("prev", u.to_string()); 
     152                        } 
     153                        am->insert("page", pg); 
    109154                } 
    110155        }