[backend] add endpoints for listing movies and fetching a movie by uuid
This commit is contained in:
		
							parent
							
								
									83838a4ca4
								
							
						
					
					
						commit
						51c05e94ab
					
				@ -14,15 +14,63 @@ db_params = {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
db = psql.connect(**db_params)
 | 
					db = psql.connect(**db_params)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@app.route("/")
 | 
					@app.route("/movies")
 | 
				
			||||||
def test():
 | 
					def movies():
 | 
				
			||||||
    c = db.cursor()
 | 
					    c = db.cursor()
 | 
				
			||||||
    try:
 | 
					 | 
				
			||||||
        c.execute('SELECT * FROM movies')
 | 
					 | 
				
			||||||
    except Exception:
 | 
					 | 
				
			||||||
        db.rollback()
 | 
					 | 
				
			||||||
        return "Failed to fetch table 'movies'"
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    res = c.fetchmany(10)
 | 
					    try:
 | 
				
			||||||
 | 
					        c.execute('SELECT uuid, name, scheduled_datetime, movie_details, image_url, max_passes FROM movies')
 | 
				
			||||||
 | 
					    except Exception:
 | 
				
			||||||
 | 
					        return {
 | 
				
			||||||
 | 
					                "error": "Failed to list movies",
 | 
				
			||||||
 | 
					                "result": None
 | 
				
			||||||
 | 
					        }, 503
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    res = c.fetchall()
 | 
				
			||||||
    c.close()
 | 
					    c.close()
 | 
				
			||||||
    return "<p>Hello, World! test data:<br>" + "<br>".join(str(i) for i in res) + "</p>"
 | 
					
 | 
				
			||||||
 | 
					    return {
 | 
				
			||||||
 | 
					        "error": "",
 | 
				
			||||||
 | 
					        "result": [
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                "uuid": i[0],
 | 
				
			||||||
 | 
					                "name": i[1],
 | 
				
			||||||
 | 
					                "scheduled_datetime": i[2].strftime("%Y-%m-%d %H:%M:%S"),
 | 
				
			||||||
 | 
					                "movie_details": i[3],
 | 
				
			||||||
 | 
					                "image_url": i[4],
 | 
				
			||||||
 | 
					                "max_passes": i[5]
 | 
				
			||||||
 | 
					            } for i in res
 | 
				
			||||||
 | 
					        ]
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@app.route("/movie/<movie_uuid>")
 | 
				
			||||||
 | 
					def movie_byid(movie_uuid):
 | 
				
			||||||
 | 
					    c = db.cursor()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    try:
 | 
				
			||||||
 | 
					        c.execute(f"SELECT name, scheduled_datetime, movie_details, image_url, max_passes FROM movies WHERE uuid='{movie_uuid}'")
 | 
				
			||||||
 | 
					    except Exception:
 | 
				
			||||||
 | 
					        return {
 | 
				
			||||||
 | 
					                "error": "Failed to search for a movie",
 | 
				
			||||||
 | 
					                "result": None
 | 
				
			||||||
 | 
					        }, 503
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    res = c.fetchone()
 | 
				
			||||||
 | 
					    c.close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if res:
 | 
				
			||||||
 | 
					        return {
 | 
				
			||||||
 | 
					            "error": "",
 | 
				
			||||||
 | 
					            "result": {
 | 
				
			||||||
 | 
					                "name": res[0],
 | 
				
			||||||
 | 
					                "scheduled_datetime": res[1].strftime("%Y-%m-%d %H:%M:%S"),
 | 
				
			||||||
 | 
					                "movie_details": res[2],
 | 
				
			||||||
 | 
					                "image_url": res[3],
 | 
				
			||||||
 | 
					                "max_passes": res[4]
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    else:
 | 
				
			||||||
 | 
					        return {
 | 
				
			||||||
 | 
					            "error": f"No movie with uuid={movie_uuid} has been found",
 | 
				
			||||||
 | 
					            "result": None
 | 
				
			||||||
 | 
					        }, 404
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user