コード例 #1
0
def show_songs():
    if request.method == "GET":
        songs = Song.query.order_by(Song.artist_id).all()
        serialized = [s.serialize() for s in songs]
        return jsonify(songs=serialized)
    else:
        data = request.get_json()

        if not data['title']:
            return jsonify({"msg": "Missing title parameter"}), 400
        if not data['artist']:
            return jsonify({"msg": "Missing artist parameter"}), 400
        if not data['album']:
            return jsonify({"msg": "Missing album parameter"}), 400

        title = data['title']
        artist = Artist.get_by_name(data['artist'])
        album = Album.get_by_title(data['album'])

        try:
            if not artist.id > 0:
                return jsonify({"msg": "Artist not found."}), 400
            if not album.id > 0:
                return jsonify({"msg": "Album not found."}), 400

            artist_id = artist.id
            album_id = album.id
            new_song = Song(title=title, artist_id=artist_id,
                            album_id=album_id)

            db.session.add(new_song)
            db.session.commit()

            return jsonify({"msg": "Song added successfully."}), 201
        except Exception as e:
            print(e)
            return jsonify(msg="Could not add song to database."), 400