generated from mwc/project_banjo_app
70 lines
2.2 KiB
Python
70 lines
2.2 KiB
Python
from banjo.urls import route_get, route_post
|
|
from banjo.http import BadRequest
|
|
from app.models import Movie
|
|
|
|
@route_get('movies/all')
|
|
def all_movies(params):
|
|
movies = Movie.objects.all()
|
|
return {'movies': [movie.to_dict() for movie in movies]}
|
|
|
|
@route_get('movies/search', args={'query': str})
|
|
def search_movie(params):
|
|
query = params['query']
|
|
movies = Movie.objects.filter(title__icontains=query)
|
|
return {'movies': [movie.to_dict() for movie in movies]}
|
|
|
|
@route_get('movies/want-to-watch')
|
|
def want_to_watch(params):
|
|
movies = Movie.objects.filter(status="Want to Watch")
|
|
return {'movies': [movie.to_dict() for movie in movies]}
|
|
|
|
@route_get('movies/watched')
|
|
def watched_movies(params):
|
|
movies = Movie.objects.filter(status="Watched")
|
|
return {'movies': [movie.to_dict() for movie in movies]}
|
|
|
|
@route_post('movies/add', args={'title': str})
|
|
def add_movie(params):
|
|
movie = Movie(
|
|
title=params['title'],
|
|
genre=params.get('genre'),
|
|
status=params.get('status', 'Want to Watch'),
|
|
rating=params.get('rating')
|
|
)
|
|
movie.save()
|
|
return movie.to_dict()
|
|
|
|
@route_post('movies/update-status', args={'id': int, 'status': str})
|
|
def update_status(params):
|
|
try:
|
|
movie = Movie.objects.get(id=params['id'])
|
|
movie.status = params['status']
|
|
movie.save()
|
|
return movie.to_dict()
|
|
except Movie.DoesNotExist:
|
|
raise NotFound("Movie not found")
|
|
|
|
|
|
@route_post('movies/update-rating', args={'id': int, 'rating': int})
|
|
def update_rating(params):
|
|
try:
|
|
movie = Movie.objects.get(id=params['id'])
|
|
rating = params['rating']
|
|
if rating < 1 or rating > 10:
|
|
raise BadRequest("Rating must be between 1 and 10")
|
|
movie.rating = rating
|
|
movie.save()
|
|
return movie.to_dict()
|
|
except Movie.DoesNotExist:
|
|
raise NotFound("Movie not found")
|
|
|
|
@route_post('movies/update-genre', args={'id': int, 'genre': str})
|
|
def update_genre(params):
|
|
"""Update the genre of an existing movie"""
|
|
try:
|
|
movie = Movie.objects.get(id=params['id'])
|
|
movie.genre = params['genre']
|
|
movie.save()
|
|
return movie.to_dict()
|
|
except Movie.DoesNotExist:
|
|
raise NotFound("Movie not found") |