-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
109 lines (82 loc) · 2.6 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
import os
# Init app
app = Flask(__name__)
basedir = os.path.abspath(os.path.dirname(__file__))
# Database
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///" + os.path.join(
basedir, "db.sqlite"
)
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
# Init db
db = SQLAlchemy(app)
# Init ma
ma = Marshmallow(app)
# Artist Class/Model
class Artist(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), unique=True)
description = db.Column(db.String(200))
score = db.Column(db.Float)
noOfMovie = db.Column(db.Integer)
def __init__(self, name, description, score, noOfMovie):
self.name = name
self.description = description
self.score = score
self.noOfMovie = noOfMovie
# Artist Schema
class ArtistSchema(ma.Schema):
class Meta:
fields = ("id", "name", "description", "score", "noOfMovie")
# Init schema
artist_schema = ArtistSchema()
artists_schema = ArtistSchema(many=True)
# Create a Artist
@app.route("/artist", methods=["POST"])
def add_artist():
# print(request.json["name"])
name = request.json["name"]
description = request.json["description"]
score = request.json["score"]
noOfMovie = request.json["noOfMovie"]
new_artist = Artist(name, description, score, noOfMovie)
db.session.add(new_artist)
db.session.commit()
return artist_schema.jsonify(new_artist)
# Get All Artists
@app.route("/artist", methods=["GET"])
def get_artists():
all_artists = Artist.query.all()
result = artists_schema.dump(all_artists)
return jsonify(result)
# Get Single Artists
@app.route("/artist/<id>", methods=["GET"])
def get_artist(id):
artist = Artist.query.get(id)
return artist_schema.jsonify(artist)
# Update a Artist
@app.route("/artist/<id>", methods=["PUT"])
def update_artist(id):
artist = Artist.query.get(id)
name = request.json["name"]
description = request.json["description"]
score = request.json["score"]
noOfMovie = request.json["noOfMovie"]
artist.name = name
artist.description = description
artist.score = score
artist.noOfMovie = noOfMovie
db.session.commit()
return artist_schema.jsonify(artist)
# Delete
@app.route("/artist/<id>", methods=["DELETE"])
def delete_artist(id):
artist = Artist.query.get(id)
db.session.delete(artist)
db.session.commit()
return artist_schema.jsonify(artist)
# Run Server
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)