contrib/benitlux: add services to list known beers and days
authorAlexis Laferrière <alexis.laf@xymus.net>
Thu, 6 Aug 2015 19:30:58 +0000 (15:30 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Thu, 6 Aug 2015 20:18:32 +0000 (16:18 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

contrib/benitlux/src/benitlux_db.nit

index 05a1a81..36906c8 100644 (file)
@@ -118,6 +118,33 @@ class DB
                return events
        end
 
+       # List known beers
+       #
+       # Return `null` on error.
+       fun beers: nullable Array[Beer]
+       do
+               var stmt = select("name, desc FROM beers")
+               if stmt == null then return null
+               return [for row in stmt do row.to_beer]
+       end
+
+       # Days where `beer` was available, all known days if `beer == null`
+       #
+       # Return `null` on error.
+       fun days(beer: nullable Beer): nullable Array[String]
+       do
+               var stmt
+               if beer == null then
+                       stmt = select("DISTINCT day FROM daily")
+               else
+                       stmt = select("""
+DISTINCT day FROM daily WHERE beer=(SELECT ROWID FROM beers WHERE name="{{{beer.name}}}")""")
+               end
+
+               if stmt == null then return null
+               return [for row in stmt do row[0].to_s]
+       end
+
        # All the subscribers to the mailing list
        fun subscribers: Array[String]
        do