diff --git a/src/internal/admins/stats.go b/src/internal/admins/stats.go
new file mode 100644
index 0000000..0897d2c
--- /dev/null
+++ b/src/internal/admins/stats.go
@@ -0,0 +1,57 @@
+package admin
+
+import (
+ "net/http"
+
+ "github.com/jmoiron/sqlx"
+ "github.com/sirupsen/logrus"
+ "gitlab.quimbo.fr/odwrtw/canape-sql/src/internal/web"
+)
+
+const (
+ moviesCountQuery = `SELECT COUNT(*) FROM movies;`
+ showsCountQuery = `SELECT COUNT(*) FROM shows;`
+ episodesCountQuery = `SELECT COUNT(*) FROM episodes;`
+)
+
+// GetCount gets the count from a query
+func GetCount(db *sqlx.DB, query string) (int, error) {
+ var count int
+ err := db.QueryRow(query).Scan(&count)
+ if err != nil {
+ return 0, err
+ }
+ return count, nil
+}
+
+// GetStatsHandler returns the stats of the app
+func GetStatsHandler(env *web.Env, w http.ResponseWriter, r *http.Request) error {
+ log := env.Log.WithFields(logrus.Fields{
+ "function": "admin.GetStatsHandler",
+ })
+
+ log.Debug("getting stats")
+
+ stats := struct {
+ MoviesCount int `json:"movies_count"`
+ ShowsCount int `json:"shows_count"`
+ EpisodesCount int `json:"episodes_count"`
+ }{}
+
+ for _, s := range []struct {
+ query string
+ ptr *int
+ }{
+ {moviesCountQuery, &stats.MoviesCount},
+ {showsCountQuery, &stats.ShowsCount},
+ {episodesCountQuery, &stats.EpisodesCount},
+ } {
+ var err error
+ *s.ptr, err = GetCount(env.Database, s.query)
+ if err != nil {
+ return err
+ }
+ }
+
+ return env.RenderJSON(w, stats)
+}
diff --git a/src/public/js/actions/admins.js b/src/public/js/actions/admins.js
index 15d2a65..050ca88 100644
--- a/src/public/js/actions/admins.js
+++ b/src/public/js/actions/admins.js
@@ -7,6 +7,13 @@ export function getUsers() {
)
}
+export function getStats() {
+ return request(
+ "ADMIN_GET_STATS",
+ configureAxios().get("/admins/stats")
+ )
+}
+
export function updateUser(data) {
return request(
"ADMIN_UPDATE_USER",
diff --git a/src/public/js/components/admins/panel.js b/src/public/js/components/admins/panel.js
index cd1847d..5f5e5f0 100644
--- a/src/public/js/components/admins/panel.js
+++ b/src/public/js/components/admins/panel.js
@@ -4,10 +4,12 @@ import { bindActionCreators } from "redux"
import { updateUser } from "../../actions/admins"
import UserList from "./users"
+import Stats from "./stats"
function mapStateToProps(state) {
return {
users : state.adminStore.get("users"),
+ stats : state.adminStore.get("stats"),
};
}
const mapDispatchToProps = (dipatch) =>
@@ -15,10 +17,15 @@ const mapDispatchToProps = (dipatch) =>
function AdminPanel(props) {
return (
-