<file_sep>from django.db import models

class cinema(models.Model):
	nama = models.CharField('Nama', max_length=27)
	alamat = models.CharField('Alamat', max_length=100)
	kontak = models.IntegerField()

	def __str__(self):
		return self.nama

	def __iter__(self):
		return iter([self.nama,
		self.alamat,
		self.kontak
		])

class movie(models.Model):
	judul = models.CharField('Judul', max_length=100)
	kategori = models.CharField('Kategori', max_length=100)
	tahun = models.IntegerField()

	def __str__(self):
		return self.judul

	def __iter__(self):
		return iter([self.judul,
		self.kategori,
		self.tahun
		])

class showing(models.Model):
	showing_time = models.TimeField()
	showing_date = models.DateField()
	movies = models.ForeignKey(movie, on_delete=models.CASCADE)

	def __str__(self):
		return self.movies.judul

class theater(models.Model):
	nama = models.CharField('Nama', max_length=27)
	kapasitas = models.IntegerField()
	cinemas = models.ForeignKey(cinema, on_delete=models.CASCADE)
	showings = models.ManyToManyField(showing)

	def __str__(self):
		return self.nama

class ticket(models.Model):
	nomor_kursi = models.CharField(max_length=10)
	harga = models.IntegerField()
	showings = models.ForeignKey(showing, on_delete=models.CASCADE)

	def __str__(self):
		return self.nomor_kursi<file_sep>"""Kota URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url, include
from django.contrib import admin
from MovieTheater import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^api/cinema/add/$', views.add_cinema),
    url(r'^api/showing/add/$', views.add_showing),
    url(r'^api/movie/add/$', views.add_movie),
    url(r'^api/cinema/$', views.get_all_cinema),
    url(r'^api/cinema/(?P<id>[^/]*)/$', views.get_cinema),
    url(r'^api/movie/$', views.get_all_movie),
    url(r'^api/movie/(?P<id>[^/]*)/$', views.get_movie),
    url(r'^api/showing/$', views.get_all_showing),
    url(r'^api/showing/(?P<id>[^/]*)/$', views.get_showing),
    url(r'^api/theater/$', views.get_all_theater),
    url(r'^api/theater/(?P<id>[^/]*)/$', views.get_theater),
    url(r'^api/ticket/$', views.get_all_ticket),
    url(r'^api/ticket/(?P<id>[^/]*)/$', views.get_ticket),
]
<file_sep>from django.contrib import admin
from . models import *

admin.site.register(cinema)
admin.site.register(movie)
admin.site.register(showing)
admin.site.register(theater)
admin.site.register(ticket)<file_sep>from django.shortcuts import render
from MovieTheater.serializers import *
from rest_framework.response import Response
from rest_framework.decorators import api_view
from .models import *
from rest_framework import status

@api_view(['GET'])
def get_all_cinema(request):
	cinemas = cinema.objects.all()
	serializer = cinemaSerializer(cinemas, many= True).data
	return Response({"data" : serializer}, status=status.HTTP_200_OK)

@api_view(['GET', 'PUT', 'DELETE'])
def get_cinema(request, id):
	try:
		cinema1 = cinema.objects.get(id=id)
	except cinema.DoesNotExist:
		return Response(status=status.HTTP_404_NOT_FOUND)

	if request.method == 'GET':
		serial = cinemaSerializer(cinema1).data
		return Response({"data" : serial}, status=status.HTTP_200_OK)
	elif request.method == 'PUT':
		serializer = cinemaSerializer(cinema1, data=request.data)
		if serializer.is_valid():
			serializer.save()
			return Response({"data" : serializer.data}, status=status.HTTP_200_OK)
		return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
	elif request.method == 'DELETE':
		cinema1.delete()
		return Response(status=status.HTTP_204_NO_CONTENT)

@api_view(['GET'])
def get_all_movie(request):
	movies = movie.objects.all()
	serializer = movieSerializer(movies, many= True).data
	return Response({"data" : serializer}, status=status.HTTP_200_OK)

@api_view(['GET', 'PUT', 'DELETE'])
def get_movie(request, id):
	try:
		movie1 = movie.objects.get(id=id)
	except movie.DoesNotExist:
		return Response(status=status.HTTP_404_NOT_FOUND)

	if request.method == 'GET':
		serializer = movieSerializer(movie1).data
		return Response({"data" : serializer}, status=status.HTTP_200_OK)
	elif request.method == 'PUT':
		serializer = movieSerializer(movie1, data=request.data)
		if serializer.is_valid():
			serializer.save()
			return Response({"data" : serializer.data}, status=status.HTTP_200_OK)
		return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
	elif request.method == 'DELETE':
		movie1.delete()
		return Response(status=status.HTTP_204_NO_CONTENT)

@api_view(['GET'])
def get_all_showing(request):
	showings = showing.objects.all()
	serializer = showingSerializer(showings, many= True).data
	return Response({"data" : serializer}, status=status.HTTP_200_OK)

@api_view(['GET', 'PUT', 'DELETE'])
def get_showing(request, id):
	try:
		showing1 = showing.objects.get(id=id)
	except showing.DoesNotExist:
		return Response(status=status.HTTP_404_NOT_FOUND)
	
	if request.method == 'GET':
		serializer = showingSerializer(showing1).data
		return Response({"data" : serializer}, status=status.HTTP_200_OK)
	elif request.method == 'PUT':
		serializer = showingSerializer(showing1, data=request.data)
		if serializer.is_valid():
			serializer.save()
			return Response({"data" : serializer.data}, status=status.HTTP_200_OK)
		return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
	elif request.method == 'DELETE':
		showing1.delete()
		return Response(status=status.HTTP_204_NO_CONTENT)

@api_view(['POST'])
def add_cinema(request):
	cinema = cinemaSerializer(data = request.data)
	if cinema.is_valid():
		cinema.save()
		return Response({"data" : "Berhasil Memasukkan Cinema"}, status=status.HTTP_201_CREATED)
	else:
		error_details = []
		for key in cinema.errors.keys():
			error_details.append({"field": key, "message": cinema.errors[key][0]})

		data = {
				"Error": {
					"status": 400,
					"message": "Data tidak valid",
					"error_details": error_details
					}
				}

		return Response(data, status=status.HTTP_400_BAD_REQUEST)

@api_view(['POST'])
def add_movie(request):
	movie = movieSerializer(data = request.data)
	if movie.is_valid():
		movie.save()
		return Response({"data" : "Berhasil Memasukkan Movie"}, status=status.HTTP_201_CREATED)
	else:
		error_details = []
		for key in movie.errors.keys():
			error_details.append({"field": key, "message": movie.errors[key][0]})

		data = {
				"Error": {
					"status": 400,
					"message": "Data tidak valid",
					"error_details": error_details
					}
				}

		return Response(data, status=status.HTTP_400_BAD_REQUEST)

@api_view(['POST'])
def add_showing(request):
	showing = showingSerializer(data = request.data)
	if showing.is_valid():
		showing.save()
		return Response({"data" : "Berhasil Memasukkan Showing"}, status=status.HTTP_201_CREATED)
	else:
		error_details = []
		for key in showing.errors.keys():
			error_details.append({"field": key, "message": showing.errors[key][0]})

		data = {
				"Error": {
					"status": 400,
					"message": "Data tidak valid",
					"error_details": error_details
					}
				}

		return Response(data, status=status.HTTP_400_BAD_REQUEST)

@api_view(['GET'])
def get_all_theater(request):
	theaters = theater.objects.all()
	serializer = theaterSerializer(theaters, many= True).data
	return Response({"data" : serializer}, status=status.HTTP_200_OK)

@api_view(['GET'])
def get_all_ticket(request):
	tickets = ticket.objects.all()
	serializer = ticketSerializer(tickets, many= True).data
	return Response({"data" : serializer}, status=status.HTTP_200_OK)

@api_view(['GET', 'PUT', 'DELETE'])
def get_theater(request, id):
	try:
		theater1 = theater.objects.get(id=id)
	except theater.DoesNotExist:
		return Response(status=status.HTTP_404_NOT_FOUND)

	if request.method == 'GET':
		serializer = theaterSerializer(theater1).data
		return Response({"data" : serializer}, status=status.HTTP_200_OK)
	elif request.method == 'PUT':
		serializer = theaterSerializer(theater1, data=request.data)
		if serializer.is_valid():
			serializer.save()
			return Response({"data" : serializer.data}, status=status.HTTP_200_OK)
		return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
	elif request.method == 'DELETE':
		theater1.delete()
		return Response(status=status.HTTP_204_NO_CONTENT)

@api_view(['GET', 'PUT', 'DELETE'])
def get_ticket(request, id):
	try:
		ticket1 = ticket.objects.get(id=id)
	except ticket.DoesNotExist:
		return Response(status=status.HTTP_404_NOT_FOUND)

	if request.method == 'GET':
		serializer = ticketSerializer(ticket1).data
		return Response({"data" : serializer}, status=status.HTTP_200_OK)
	elif request.method == 'PUT':
		serializer = ticketSerializer(ticket1, data=request.data)
		if serializer.is_valid():
			serializer.save()
			return Response({"data" : serializer.data}, status=status.HTTP_200_OK)
		return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
	elif request.method == 'DELETE':
		ticket1.delete()
		return Response(status=status.HTTP_204_NO_CONTENT)<file_sep># -*- coding: utf-8 -*-
# Generated by Django 1.11.4 on 2019-04-12 09:48
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='cinema',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('nama', models.CharField(max_length=27, verbose_name='Nama')),
                ('alamat', models.CharField(max_length=100, verbose_name='Alamat')),
                ('kontak', models.IntegerField()),
            ],
        ),
        migrations.CreateModel(
            name='movie',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('judul', models.CharField(max_length=100, verbose_name='Judul')),
                ('kategori', models.CharField(max_length=100, verbose_name='Kategori')),
                ('tahun', models.IntegerField()),
            ],
        ),
        migrations.CreateModel(
            name='showing',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('showing_time', models.TimeField()),
                ('showing_date', models.DateField()),
                ('movies', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='MovieTheater.movie')),
            ],
        ),
        migrations.CreateModel(
            name='theater',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('nama', models.CharField(max_length=27, verbose_name='Nama')),
                ('kapasitas', models.IntegerField()),
                ('cinemas', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='MovieTheater.cinema')),
                ('showings', models.ManyToManyField(to='MovieTheater.showing')),
            ],
        ),
        migrations.CreateModel(
            name='ticket',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('nomor_kursi', models.CharField(max_length=10)),
                ('harga', models.IntegerField()),
                ('showings', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='MovieTheater.showing')),
            ],
        ),
    ]
<file_sep>from rest_framework import serializers
from .models import *

class cinemaSerializer(serializers.ModelSerializer):
	# nama = serializers.CharField(max_length=27)
	# alamat = serializers.CharField(max_length=100)
	# kontak = serializers.IntegerField()
	class Meta:
		model = cinema
		fields = ('nama', 'alamat', 'kontak')

	def create(self, validated_data):
		cinemas = cinema(**validated_data)
		cinemas.save()
		return cinemas

class movieSerializer(serializers.ModelSerializer):
	# judul = serializers.CharField(max_length=100)
	# kategori = serializers.CharField( max_length=100)
	# tahun = serializers.IntegerField()
	class Meta:
		model = movie
		fields = '__all__'

	def create(self, validated_data):
		movies = movie(**validated_data)
		movies.save()
		return movies

class showingSerializer(serializers.ModelSerializer):
	# showing_time = serializers.TimeField()
	# showing_date = serializers.DateField()
	# movies = serializers.ForeignKey(movie, on_delete=models.CASCADE)
	movies = movieSerializer(many=False)
	class Meta:
		model = showing
		fields = ('showing_time', 'showing_date', 'movies')
		# fields = '__all__'

	def create(self, validated_data):
		movie_data = validated_data.pop('movies')
		movie1 = movie.objects.get(**movie_data)
		showings = showing.objects.create(movies=movie1, **validated_data)
		return showings
		# showings = showing(**validated_data)
		# showings.save()
		# return showings

	def update(self, instance, validated_data):
		instance.showing_time = validated_data.get('showing_time', instance.showing_time)
		instance.showing_date = validated_data.get('showing_date', instance.showing_date)
		movie_data = validated_data.pop('movies')
		movie1 = movie.objects.get(**movie_data)
		instance.movies = movie1
		instance.save()
		return instance

class theaterSerializer(serializers.ModelSerializer):
	# nama = serializers.CharField(max_length=27)
	# kapasitas = serializers.IntegerField()
	# cinemas = serializers.ForeignKey(cinema, on_delete=models.CASCADE)
	# showings = serializers.ManyToManyField(showing)
	cinemas = cinemaSerializer(many=False, read_only=True)
	showings = showingSerializer(many=True)
	class Meta:
		model = theater
		fields = ('nama', 'kapasitas', 'cinemas', 'showings')

class ticketSerializer(serializers.ModelSerializer):
	# nomor_kursi = serializers.CharField(max_length=10)
	# harga = serializers.IntegerField()
	# showings = serializers.ForeignKey(showing, on_delete=models.CASCADE)
	showings = showingSerializer(many=False)
	class Meta:
		model = ticket
		fields = '__all__'<file_sep>from django.apps import AppConfig


class MovietheaterConfig(AppConfig):
    name = 'MovieTheater'
