From 7c9f5e889c4073143f386987b07d9c71838a0502 Mon Sep 17 00:00:00 2001
From: Frank Sauerburger <frank@sauerburger.com>
Date: Sun, 28 Aug 2022 18:10:22 +0200
Subject: [PATCH] Add title-by-id lookup endpoint

---
 Makefile        |  2 +-
 app.py          | 23 ++++++++++++++++++++---
 deployment.yaml |  2 +-
 3 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/Makefile b/Makefile
index 55c692e..ab14b38 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-TAG=0.3.0
+TAG=0.4.0
 
 build:
 	DOCKER_DEFAULT_PLATFORM=linux/amd64 docker build -t gitlab.sauerburger.com:5049/frank/wakefield-movie-recommender:$(TAG) .
diff --git a/app.py b/app.py
index 22d9b34..ba37e80 100644
--- a/app.py
+++ b/app.py
@@ -4,7 +4,7 @@
 """Movie title suggestion api"""
 
 import json
-from flask import Flask, request
+from flask import Flask, request, abort
 import qgram
 
 app = Flask(__name__)
@@ -13,9 +13,12 @@ with open("examples/titles.json", encoding="utf-8") as fileobj:
     titles = json.load(fileobj)
 
 movie_ids = {}
+movie_titles = {}
 index = qgram.QGramIndex()
-for tid, (title, _) in titles.items():
-    movie_ids[title] = int(tid)
+for tid, (title, year) in titles.items():
+    tid = int(tid)
+    movie_titles[tid] = (title, year)
+    movie_ids[title] = tid
     index.add_term(title)
 
 
@@ -46,6 +49,20 @@ def suggest():
         "suggestions": results
     }
 
+
+@app.route("/titles/<int:mid>")
+def byid(mid):
+    """Show healthz status"""
+    if mid not in movie_titles:
+        abort(404)
+
+    title, year = movie_titles[mid]
+    return {
+        "id": mid,
+        "title": title,
+        "year": year
+    }
+
 @app.route("/titles/healthz")
 def healthz():
     """Show healthz status"""
diff --git a/deployment.yaml b/deployment.yaml
index 2c0d865..7f8de97 100644
--- a/deployment.yaml
+++ b/deployment.yaml
@@ -16,7 +16,7 @@ spec:
     spec:
       containers:
       - name: titles
-        image: gitlab.sauerburger.com:5049/frank/wakefield-movie-recommender:0.3.0
+        image: gitlab.sauerburger.com:5049/frank/wakefield-movie-recommender:0.4.0
         livenessProbe:
           httpGet:
             path: /titles/healthz
-- 
GitLab