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!')
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!')
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!')
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')
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!')
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!')
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!')
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!')
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')
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')
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')