generated from mwc/project_banjo_app
Honestly this was really fun to do. I am pretty proud to build an app to successfully work. I was able to create multiple differnt routes including adding movies, searching, updating status, rating, and genre. This project has made me realize how useful it is to understanding how apps are developed. I feel like this is super applicable and super knowledgable to understand. Some skills I learned with this project is how to design and build models, create API routes, working databases, and handling errors.
This commit is contained in:
70
project/app/views.py
Normal file
70
project/app/views.py
Normal file
@@ -0,0 +1,70 @@
|
||||
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")
|
||||
Reference in New Issue
Block a user