github.com/readium/readium-lcp-server@v0.0.0-20240101192032-6e95190e99f1/frontend/api/common.go (about) 1 // Copyright (c) 2016 Readium Foundation 2 // 3 // Redistribution and use in source and binary forms, with or without modification, 4 // are permitted provided that the following conditions are met: 5 // 6 // 1. Redistributions of source code must retain the above copyright notice, this 7 // list of conditions and the following disclaimer. 8 // 2. Redistributions in binary form must reproduce the above copyright notice, 9 // this list of conditions and the following disclaimer in the documentation and/or 10 // other materials provided with the distribution. 11 // 3. Neither the name of the organization nor the names of its contributors may be 12 // used to endorse or promote products derived from this software without specific 13 // prior written permission 14 // 15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 26 package staticapi 27 28 import ( 29 "net/http" 30 "strconv" 31 32 "github.com/readium/readium-lcp-server/api" 33 "github.com/readium/readium-lcp-server/frontend/webdashboard" 34 "github.com/readium/readium-lcp-server/frontend/weblicense" 35 "github.com/readium/readium-lcp-server/frontend/webpublication" 36 "github.com/readium/readium-lcp-server/frontend/webpurchase" 37 "github.com/readium/readium-lcp-server/frontend/webrepository" 38 "github.com/readium/readium-lcp-server/frontend/webuser" 39 ) 40 41 //IServer defines methods for db interaction 42 type IServer interface { 43 RepositoryAPI() webrepository.WebRepository 44 PublicationAPI() webpublication.WebPublication 45 UserAPI() webuser.WebUser 46 PurchaseAPI() webpurchase.WebPurchase 47 DashboardAPI() webdashboard.WebDashboard 48 LicenseAPI() weblicense.WebLicense 49 } 50 51 // Pagination used to paginate listing 52 type Pagination struct { 53 Page int 54 PerPage int 55 } 56 57 // ExtractPaginationFromRequest extract from http.Request pagination information 58 func ExtractPaginationFromRequest(r *http.Request) (Pagination, error) { 59 var err error 60 var page int64 // default: page 1 61 var perPage int64 // default: 30 items per page 62 pagination := Pagination{} 63 64 if r.FormValue("page") != "" { 65 page, err = strconv.ParseInt((r).FormValue("page"), 10, 32) 66 if err != nil { 67 return pagination, err 68 } 69 } else { 70 page = 1 71 } 72 73 if r.FormValue("per_page") != "" { 74 perPage, err = strconv.ParseInt((r).FormValue("per_page"), 10, 32) 75 if err != nil { 76 return pagination, err 77 } 78 } else { 79 perPage = 30 80 } 81 82 if page > 0 { 83 page-- //pagenum starting at 0 in code, but user interface starting at 1 84 } 85 86 if page < 0 { 87 return pagination, err 88 } 89 90 pagination.Page = int(page) 91 pagination.PerPage = int(perPage) 92 return pagination, err 93 } 94 95 // PrepareListHeaderResponse set several http headers 96 // sets previous and next link headers 97 func PrepareListHeaderResponse(resourceCount int, resourceLink string, pagination Pagination, w http.ResponseWriter) { 98 if resourceCount > 0 { 99 nextPage := strconv.Itoa(int(pagination.Page) + 1) 100 w.Header().Set("Link", "<"+resourceLink+"?page="+nextPage+">; rel=\"next\"; title=\"next\"") 101 } 102 if pagination.Page > 1 { 103 previousPage := strconv.Itoa(int(pagination.Page) - 1) 104 w.Header().Set("Link", "<"+resourceLink+"/?page="+previousPage+">; rel=\"previous\"; title=\"previous\"") 105 } 106 w.Header().Set("Content-Type", api.ContentType_JSON) 107 }