from flask import jsonify, request

from soundem import app, db
from .models import Artist, Album, Song, Favorite, user_datastore


@app.route('/api/v1/register', methods=['POST'])
def register():
    data = request.get_json()
    email = data.get('email')
    password = data.get('password')
    errors = {}

    if not email:
        errors['email'] = 'Field is required.'

    if not password:
        errors['password'] = 'Field is required.'

    existing_user = user_datastore.find_user(email=email)

    if existing_user:
        errors['email'] = 'Email is already taken'

    if errors:
        return jsonify({'errors': errors}), 400

    user = user_datastore.create_user(email=email, password=password)
    db.session.commit()

    user_data = {
        'id': user.id,
        'email': user.email,
        'active': user.active
    }

    return jsonify({'user': user_data}), 201


@app.route('/api/v1/artists', methods=['GET'])
def get_artists():
    artists = []

    for artist in Artist.query.all():
        artists.append({
            'id': artist.id,
            'name': artist.name,
            'bio': artist.bio,
            'albums': [album.id for album in artist.albums.all()]
        })

    return jsonify({'artists': artists})


@app.route('/api/v1/albums', methods=['GET'])
def get_albums():
    albums = []

    for album in Album.query.all():
        albums.append({
            'id': album.id,
            'name': album.name,
            'songs': [song.id for song in album.songs.all()]
        })

    return jsonify({'albums': albums})


@app.route('/api/v1/songs', methods=['GET'])
def get_songs():
    songs = []

    for song in Song.query.all():
        favorite = Favorite.query.filter_by(song=song).first()
        songs.append({
            'id': song.id,
            'name': song.name,
            'album': song.album.id,
            'favorite': True if favorite else False
        })

    return jsonify({'songs': songs})

@@ -27,16 +27,53 @@
  request
+%0Afrom flask.ext.security import utils
 %0A%0Afrom s
@@ -162,16 +162,910 @@
 store%0A%0A%0A
+@app.route('/api/v1/login', methods=%5B'POST'%5D)%0Adef login():%0A    data = request.get_json()%0A    email = data.get('email')%0A    password = data.get('password')%0A    errors = %7B%7D%0A%0A    if not email:%0A        errors%5B'email'%5D = 'Field is required.'%0A%0A    if not password:%0A        errors%5B'password'%5D = 'Field is required.'%0A%0A    user = user_datastore.get_user(email)%0A%0A    if not user:%0A        errors%5B'email'%5D = 'User does not exist.'%0A%0A    if not utils.verify_and_update_password(password, user):%0A        errors%5B'password'%5D = 'Invalid password.'%0A%0A    if not user.is_active():%0A        errors%5B'email'%5D = 'User is not active.'%0A%0A    if errors:%0A        return jsonify(%7B'errors': errors%7D), 400%0A%0A    utils.login_user(user)%0A%0A    user_data = %7B%0A        'id': user.id,%0A        'email': user.email,%0A        'active': user.is_active(),%0A        'token': user.get_auth_token()%0A    %7D%0A%0A    return jsonify(%7B'user': user_data%7D)%0A%0A
 @app.rou
@@ -1411,12 +1411,11 @@
 ore.
-find
+get
 _use
@@ -1756,22 +1756,67 @@
 ': user.
+is_
 active
+(),%0A        'token': user.get_auth_token()
 %0A    %7D%0A%0A

Implement login endpoint