github.com/readium/readium-lcp-server@v0.0.0-20240101192032-6e95190e99f1/frontend/api/dashboard.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  	"encoding/json"
    30  	"net/http"
    31  
    32  	"github.com/readium/readium-lcp-server/api"
    33  	"github.com/readium/readium-lcp-server/frontend/webpublication"
    34  	"github.com/readium/readium-lcp-server/problem"
    35  )
    36  
    37  // GetDashboardInfos searches a publication by its uuid
    38  func GetDashboardInfos(w http.ResponseWriter, r *http.Request, s IServer) {
    39  	if pub, err := s.DashboardAPI().GetDashboardInfos(); err == nil {
    40  		enc := json.NewEncoder(w)
    41  		if err = enc.Encode(pub); err == nil {
    42  			// send json of correctly encoded user info
    43  			w.Header().Set("Content-Type", api.ContentType_JSON)
    44  			return
    45  		}
    46  
    47  		problem.Error(w, r, problem.Problem{Detail: err.Error()}, http.StatusInternalServerError)
    48  	} else {
    49  		switch err {
    50  		case webpublication.ErrNotFound:
    51  			{
    52  				problem.Error(w, r, problem.Problem{Detail: err.Error()}, http.StatusNotFound)
    53  			}
    54  		default:
    55  			{
    56  				problem.Error(w, r, problem.Problem{Detail: err.Error()}, http.StatusInternalServerError)
    57  			}
    58  		}
    59  	}
    60  }
    61  
    62  // GetDashboardBestSellers gets the dashboard bestsellers
    63  //
    64  func GetDashboardBestSellers(w http.ResponseWriter, r *http.Request, s IServer) {
    65  	if pub, err := s.DashboardAPI().GetDashboardBestSellers(); err == nil {
    66  		enc := json.NewEncoder(w)
    67  		if err = enc.Encode(pub); err == nil {
    68  			// send json of correctly encoded user info
    69  			w.Header().Set("Content-Type", api.ContentType_JSON)
    70  			return
    71  		}
    72  
    73  		problem.Error(w, r, problem.Problem{Detail: err.Error()}, http.StatusInternalServerError)
    74  	} else {
    75  		switch err {
    76  		case webpublication.ErrNotFound:
    77  			{
    78  				problem.Error(w, r, problem.Problem{Detail: err.Error()}, http.StatusNotFound)
    79  			}
    80  		default:
    81  			{
    82  				problem.Error(w, r, problem.Problem{Detail: err.Error()}, http.StatusInternalServerError)
    83  			}
    84  		}
    85  	}
    86  }