예제 #1
0
def delete_playlist(id):
    """ Deletes a playlist"""
    logging.debug("{users_controller} BEGIN function delete_playlist()")

    if id is '':
        return RESP.response_400(message='The id parameter is empty!')

    try:
        playlist = CRUD.read_playlist_by_id(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if playlist is None:
        return RESP.response_404(message='Playlist not found!')

    try:
        CRUD.delete_object(playlist)

        entries = CRUD.read_songs_from_playlist(id)

        for entry in entries:
            CRUD.delete_object(entry)

        CRUD.commit()
    except Exception:
        CRUD.rollback()
        RESP.response_500(message='Database is down!')

    return RESP.response_200(message='Playlist deleted with success')
예제 #2
0
def get_playlist_songs(id):
    """ Retrieves all playlist songs' ids"""
    logging.debug(
        "{users_controller} BEGIN function delete_song_from_playlist()")

    if id is '':
        return RESP.response_400(message='A given parameter is empty')

    try:
        playlist = CRUD.read_playlist_by_id(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if playlist is None:
        return RESP.response_404(message='Playlist not found!')

    try:
        songs = CRUD.read_songs_from_playlist(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    array = []

    for song in songs:
        array.append(song.dump())

    return RESP.response_200(message=array)
예제 #3
0
def update_user(id, body):
    """ Updates an active user matching a given id with given parameters such as name, email and password. When a
    parameter is empty it is not updated"""
    logging.debug("{users_controller} BEGIN function update_user()")

    if id is '':
        return RESP.response_400(message='The id parameter is empty!')

    try:
        user = CRUD.read_user_by_id(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if user is None:
        return RESP.response_404(message='User not found!')

    try:
        if body['email'] is not '' and body['email'] != user.email:
            user_email = CRUD.read_user_by_email(body['email'])
            if user_email is not None:
                return RESP.response_409(
                    message='The given email is already in use!')
    except Exception:
        return RESP.response_500(message='Database is down!')

    try:
        CRUD.update_user(user, body['name'], body['email'],
                         UTILS.hash_password(body['password']))
        CRUD.commit()
    except Exception:
        CRUD.rollback()
        return RESP.response_500(message='Database is down!')

    return RESP.response_200(message='User updated with success!')
예제 #4
0
def update_song(id, body):
    """ Updates an active song matching a given id with given parameters such as title, artist, album, release year and
    path. When a parameter is empty it is not updated"""
    logging.debug("{songs_controller} BEGIN function update_song()")

    if id is '':
        return RESP.response_400(message='The id parameter is empty!')

    try:
        song = CRUD.read_song_by_song_id(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if song is None:
        return RESP.response_404(message='Song not found!')

    try:
        CRUD.update_song(song, body['title'], body['artist'], body['album'],
                         body['release_year'], body['path'])
        CRUD.commit()
    except Exception:
        CRUD.rollback()
        return RESP.response_500(message='Database is down!')

    return RESP.response_200(message='Song updated with success!')
예제 #5
0
def update_playlist(id, body):
    """ Updates a playlist matching a given id with given the given name"""
    logging.debug("{users_controller} BEGIN function update_playlist()")

    if id is '':
        return RESP.response_400(message='The id parameter is empty!')

    if body['name'] is '':
        return RESP.response_400(message='The name parameter is empty!')

    try:
        playlist = CRUD.read_playlist_by_id(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if playlist is None:
        return RESP.response_404(message='Playlist not found!')

    try:
        CRUD.update_playlist(playlist, name=body['name'])
        CRUD.commit()
    except Exception:
        CRUD.rollback()
        return RESP.response_500(message='Database is down!')

    return RESP.response_200(message='Playlist updated with success!')
예제 #6
0
def create_playlist(body):
    """ Creates a new playlist object given a name, and user id"""
    logging.debug("{users_controller} BEGIN function create_playlist()")

    if body['name'] is '' or body['user_id'] is '':
        return RESP.response_400(message='A given parameter is empty!')

    try:
        playlist = CRUD.create_playlist(body['name'], body['user_id'])
        CRUD.commit()
    except Exception:
        CRUD.rollback()
        return RESP.response_500(message='Database is down!')

    if playlist is None:
        return RESP.response_500(
            message='Error adding playlist into database!')

    return RESP.response_201(message='Playlist created with success!')
예제 #7
0
def check_login(body):
    """ Checks the login parameters"""
    logging.debug("{users_controller} BEGIN function check_login()")

    if body['email'] is '' or body['password'] is '':
        return RESP.response_400(message='A given parameter is empty!')

    user = CRUD.read_user_by_email_not_deleted(body['email'])
    try:
        user = CRUD.read_user_by_email_not_deleted(body['email'])
    except Exception:
        return RESP.response_500(message='Database is down!')

    if user is None:
        return RESP.response_400(message='Bad login!')

    if UTILS.hash_password(body['password']) != user.password:
        return RESP.response_400(message='Bad login!')

    return RESP.response_200(message=user.dump())
예제 #8
0
def create_user(body):
    """ Creates a new user object given a name, email and password"""
    logging.debug("{users_controller} BEGIN function create_user()")

    if body['name'] is '' or body['email'] is '' or body['password'] is '':
        return RESP.response_400(message='A given parameter is empty!')

    try:
        if CRUD.read_user_by_email(body['email']) is not None:
            return RESP.response_409(
                message='The given email is already in use!')
    except Exception:
        return RESP.response_500(message='Database is down!')

    try:
        user = CRUD.create_user(body['name'], body['email'],
                                UTILS.hash_password(body['password']))
        CRUD.commit()
    except Exception:
        CRUD.rollback()
        return RESP.response_500(message='Database is down!')

    if user is None:
        return RESP.response_500(message='Error adding user into database!')

    return RESP.response_201(message='User created with success!')
예제 #9
0
def create_song(body):
    logging.debug("{songs_controller} BEGIN function create_song()")

    if body['title'] is '' or body['artist'] is '' or body[
            'album'] is '' or body['release_year'] is '' or body[
                'path'] is '' or body['user_id'] is '':
        return RESP.response_400(message='A given parameter is empty!')

    try:
        song = CRUD.create_song(body['title'], body['artist'], body['album'],
                                body['release_year'], body['path'],
                                body['user_id'])
        CRUD.commit()
    except Exception:
        CRUD.rollback()
        return RESP.response_500(message='Database is down!')

    if song is None:
        return RESP.response_500(message='Error adding song into database!')

    time.sleep(random.expovariate(3))
    return RESP.response_201(message='Song created with success!')
예제 #10
0
def read_songs_criteria(expression):
    """ Returns a list of songs given an expression"""
    logging.debug("{songs_controller} BEGIN function read_song_criteria()")

    try:
        songs = CRUD.read_songs_by_criteria(expression)
    except Exception:
        return RESP.response_500(message='Database is down!')

    array = []

    for song in songs:
        array.append(song.dump())

    return RESP.response_200(message=array)
예제 #11
0
def read_song(id):
    """ Returns a song (if any) given an id"""
    logging.debug("{songs_controller} BEGIN function read_song()")

    if id is '':
        return RESP.response_400(message='The id parameter is empty!')

    try:
        song = CRUD.read_song_by_song_id(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if song is None:
        return RESP.response_404(message='Song not found!')

    return RESP.response_200(message=song.dump())
예제 #12
0
def get_playlist(id):
    """ Gets a playlist given an id"""
    logging.debug("{users_controller} BEGIN function get_playlist()")

    if id is '':
        return RESP.response_400(message='The id parameter is empty!')

    try:
        playlist = CRUD.read_playlist_by_id(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if playlist is None:
        return RESP.response_404(message='Playlist not found!')

    return RESP.response_200(message=playlist.dump())
예제 #13
0
def read_user(email):
    """ Returns an active user (if any) given an email"""
    logging.debug("{users_controller} BEGIN function read_user()")

    if email is '':
        return RESP.response_400(message='The email parameter is empty!')

    try:
        user = CRUD.read_user_by_email_not_deleted(email)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if user is None:
        return RESP.response_404(message='User not found!')

    return RESP.response_200(message=user.dump())
예제 #14
0
def get_user_playlists(user_id):
    """ Retrieves all user playlists"""
    logging.debug("{users_controller} BEGIN function get_user_playlists()")

    if user_id is '':
        return RESP.response_400(message='The id parameter is empty!')

    try:
        playlists = CRUD.read_all_user_playlists(user_id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    array = []

    for playlist in playlists:
        array.append(playlist.dump())

    return RESP.response_200(message=array)
예제 #15
0
def delete_song_from_playlist(id, song_id, user_id):
    """ Removes a song from a playlist"""
    logging.debug(
        "{users_controller} BEGIN function delete_song_from_playlist()")

    if id is '' or song_id is '' or user_id is '':
        return RESP.response_400(message='A given parameter is empty')

    try:
        playlist = CRUD.read_playlist_by_id(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if playlist is None:
        return RESP.response_404(message='Playlist not found!')

    if playlist.user_id != user_id:
        return RESP.response_400(
            message='This playlist belongs to another user')

    try:
        playlist_song = CRUD.read_song_from_playlist(id, song_id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if playlist_song is None:
        return RESP.response_404(message='Song not found is playlist')

    try:
        CRUD.delete_object(playlist_song)
        CRUD.commit()
    except Exception:
        CRUD.rollback()
        return RESP.response_500(message='Database is down!')

    return RESP.response_200(message='Song removed from playlist with success')
예제 #16
0
def add_song_to_playlist(id, body):
    """ Adds a song into a playlist"""
    logging.debug("{users_controller} BEGIN function add_song_to_playlist()")

    if id is '' or body['song_id'] is '' or body['user_id'] is '':
        return RESP.response_400(message='A given parameter is empty')

    try:
        playlist = CRUD.read_playlist_by_id(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if playlist is None:
        return RESP.response_404(message='Playlist not found!')

    if playlist.user_id != body['user_id']:
        return RESP.response_400(
            message='This playlist belongs to another user')

    # Checks if song exists by sending a request into the Songs Microservice
    headers = {
        'Content-Type': 'application/json',
        'Authorization': request.headers['Authorization']
    }
    param = {'id': body['song_id']}
    r = requests.get(SONGS_MS + '/songs', params=param, headers=headers)
    if r.status_code == 404:
        return RESP.response_404(message='Song not found!')
    if r.status_code == 500:
        return RESP.response_500(message='Songs_MS is down!')

    try:
        CRUD.create_song_in_playlist(id, body['song_id'])
        CRUD.commit()
    except Exception:
        CRUD.rollback()
        return RESP.response_500(message='Database is down!')

    return RESP.response_200(message='Song added into playlist with success!')
예제 #17
0
def delete_song(id):
    """ Deletes an active song given an id"""
    logging.debug("{songs_controller} BEGIN function delete_song()")

    if id is '':
        return RESP.response_400(message='The id parameter is empty!')

    try:
        song = CRUD.read_song_by_song_id(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if song is None:
        return RESP.response_404(message='Song not found!')

    try:
        CRUD.delete_song(song)
        CRUD.commit()
    except Exception:
        CRUD.rollback()
        return RESP.response_500(message='Database is down!')

    return RESP.response_200(message='Song deleted with success')
예제 #18
0
def delete_user(id):
    """ Deletes an active user given an id"""
    logging.debug("{users_controller} BEGIN function delete_user()")

    if id is '':
        return RESP.response_400(message='The id parameter is empty!')

    try:
        user = CRUD.read_user_by_id(id)
    except Exception:
        return RESP.response_500(message='Database is down!')

    if user is None:
        return RESP.response_404(message='User not found!')

    try:
        CRUD.delete_user(user)
        CRUD.commit()
    except Exception:
        CRUD.rollback()
        return RESP.response_500(message='Database is down!')

    return RESP.response_200(message='User deleted with success')