def put(self, album_id): data = parser.parse_args() _album_id = AlbumModel.find_by_id(album_id) _artist_id = ArtistModel.find_by_id(data['artist_id']) _album_name = AlbumModel.find_by_title(data['title']) _album_artist = AlbumModel.find_by_id_and_title( data['artist_id'], data['title']) if not _album_id: return {'message': 'No Such Album Exist'}, 404 if _artist_id is None: return {'message': 'Artist with this ID does not exists'}, 404 if _album_artist: return {'message': 'Album with this artist already exists'}, 403 if not data['title'] or data['title'].isspace(): return {'message': 'Please Enter Album Name'}, 400 album = AlbumModel.find_by_id(album_id) album.title = data['title'] album.artist_id = data['artist_id'] artist = ArtistModel.find_by_id(data['artist_id']) db.session.commit() return { 'message': 'Genre Has been Updated', 'GenreDetails': { 'id': album_id, 'title': data['title'], 'created_at': album.created_at.strftime("%Y-%m-%d %H:%M:%S"), 'artist_name': artist.name } }, 200
def put(self, artist_id): id = ArtistModel.find_by_id(artist_id) if not id: return {'message': 'No Such Artist Exist'}, 404 data = parser.parse_args() _date = id.created_at if ArtistModel.find_by_name(data['name']): return {'message': 'Artist with this name already exists'}, 403 updated_artist = ArtistModel( name=data['name'], created_at=_date.strftime("%Y-%m-%d %H:%M:%S") ) if not data['name'] or data['name'].isspace(): return {'message': 'Please Enter Artist Name'}, 400 try: artist = ArtistModel.find_by_id(artist_id) artist.name = data['name'] updated_artist.commit_db() return { 'message': 'Artist Has been Updated', 'ArtistDetails': { 'id': artist_id, 'name': data['name'], 'created_at': _date.strftime("%Y-%m-%d %H:%M:%S") } }, 200 except: return { 'message': 'Something went Wrong' }, 500
def post(self): data = parser.parse_args() _album_artist = AlbumModel.find_by_id_and_title( data['artist_id'], data['title']) if _album_artist: return {'message': 'Album with this artist already exists'}, 403 artist = ArtistModel.find_by_id(data['artist_id']) if not artist: return {'message': 'No Such Artist Exists'}, 404 if not data['title'] or data['title'].isspace(): return {'message': 'Please Enter Album Name'}, 400 _created_at = str( datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) new_album = AlbumModel(title=data['title'], artist_id=data['artist_id'], created_at=_created_at) try: new_album.save_to_db() new_id = new_album.id return { 'message': 'New Album Has been Added', 'AlbumDetails': { 'id': new_id, 'title': data['title'], 'created_at': _created_at, 'artist_name': artist.name } }, 201 except: return {'message': 'Something Went Wrong'}, 500
def delete(self, id): with lock.lock: artist = ArtistModel.find_by_id(id) if artist: artist.delete_from_db() return {n.MSG: "Artist deleted successfully"}, 200 else: return {n.MSG: "Artist not found"}, 404
def to_json(x): return { 'data': { 'album_id': x.id, 'title': x.title, 'artist_name': ArtistModel.find_by_id(x.artist_id).name, 'created_at': x.created_at.strftime("%Y-%m-%d %H:%M:%S") } }
def json(self, album_id): return { 'data': { 'album_id': album_id, 'title': self.title, 'artist_name': ArtistModel.find_by_id(self.artist_id).name, 'created_at': self.created_at.strftime("%Y-%m-%d %H:%M:%S") } }
def get(self, artist_id: int): artist = ArtistModel.find_by_id(artist_id) if not artist: return {'message': 'Artist Not Found'}, 404 try: return artist.json(), 200 except: return { 'message': 'Something went Wrong' }, 500
def get(self, id): artist = ArtistModel.find_by_id(id) if artist: return { n.EVNTS: [ event.json()[n.EVNT] for event in EventModel.query.filter( EventModel.artists.any(id=id)).all() ] }, 200 else: return {n.MSG: "Artist not found"}, 404
def post(self): data = parser.parse_args() if not AlbumModel.find_by_id(data['album_id']): return {'message': 'No Such Album Exists'}, 404 if not GenreModel.find_by_id(data['genre_id']): return {'message': 'No Such Genre Exists'}, 404 if not MediaTypeModel.find_by_id(data['mediatype_id']): return {'message': 'No Such Media Type Exists'}, 404 if not data['name'] or data['name'].isspace(): return {'message': 'Please Enter Track Name'}, 400 if data['composer'].isspace(): return {'message': 'Please Enter Composer Name'}, 400 if TracksModel.find_song_already_created(data['name'], data['album_id'], data['genre_id'], data['mediatype_id']): return{'message': 'Track with all this information is already Exists'}, 403 _created_at = str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) try: new_track = TracksModel( name=data['name'], albumid=data['album_id'], mediatypeid=data['mediatype_id'], genreid=data['genre_id'], composer=data['composer'], created_at=_created_at ) new_track.save_to_db() new_id = new_track.trackid artist_id = AlbumModel.find_by_id(data['album_id']).artist_id artist_name = ArtistModel.find_by_id(artist_id).name return { 'message': 'New Track has been Added', 'data': { 'track_id': new_id, 'name': data['name'], 'composer': data['composer'], 'created_at': _created_at, 'track_info': { 'artist_name': artist_name, 'album': AlbumModel.find_by_id(data['album_id']).title, 'media_type': MediaTypeModel.find_by_id(data['mediatype_id']).name, 'genre': GenreModel.find_by_id(data['genre_id']).name } } },201 except: return { 'message': 'Something went wrong' }, 500
def put(self, track_id: int): data = parser.parse_args() if not AlbumModel.find_by_id(data['album_id']): return {'message': 'No Such Album Exists'}, 404 if not GenreModel.find_by_id(data['genre_id']): return {'message': 'No Such Genre Exists'}, 404 if not MediaTypeModel.find_by_id(data['mediatype_id']): return {'message': 'No Such Media Type Exists'}, 404 if not data['name'] or data['name'].isspace(): return {'message': 'Please Enter Track Name'}, 400 if data['composer'].isspace(): return {'message': 'Please Enter Composer Name'}, 400 track = TracksModel.find_by_id(track_id) if not track: return {'message': 'No Such Track found.'}, 404 if (TracksModel.find_song_already_created(data['name'], data['album_id'], data['genre_id'], data['mediatype_id']) and (data['composer'] is track.composer)): return {'message': 'Track with all this information is already Exists'}, 403 try: track.name = data['name'] track.albumid = data['album_id'] track.mediatypeid = data['mediatype_id'] track.genreid = data['genre_id'] track.composer = data['composer'] db.session.commit() artist_id = AlbumModel.find_by_id(data['album_id']).artist_id artist_name = ArtistModel.find_by_id(artist_id).name return { 'message': 'Track has been Updated', 'Track_Details': { 'track_id': track_id, 'name': data['name'], 'composer': data['composer'], 'created_at': track.created_at.strftime("%Y-%m-%d %H:%M:%S"), 'track_info': { 'artist_name': artist_name, 'album': AlbumModel.find_by_id(data['album_id']).title, 'media_type': MediaTypeModel.find_by_id(data['mediatype_id']).name, 'genre': GenreModel.find_by_id(data['genre_id']).name } } }, 200 except: return { 'message': 'Something went wrong' }, 500
def delete(self, artist_id: int): claims = get_jwt_claims() if not claims['is_admin']: return { 'message': 'Admin Privileges required' }, 401 artist_id = ArtistModel.find_by_id(artist_id) if not artist_id: return {'message': 'No Such Artist Exist'}, 404 try: artist_id.delete_from_db(artist_id) return { 'message': 'Artist has been deleted' } except IntegrityError as e: db.session.rollback() return dict(message=e._message())
def put(self, id): artist = ArtistModel.find_by_id(id) if artist: parser = reqparse.RequestParser( ) # create parameters parser from request # define al input parameters need and its type parser.add_argument(n.NM, type=str, required=True, help="Artistic name of the artist") parser.add_argument(n.CTRY, type=str, required=True, help="Country nationality of the artist") parser.add_argument(n.GNR, type=str, required=True, help="Main genre of the artist") data = parser.parse_args() with lock.lock: if data[n.GNR] not in genres: return {n.MSG: "Genre not allowed"}, 400 aux_artist = ArtistModel.find_by_name(data[n.NM]) if aux_artist and aux_artist.id != id: return { n.MSG: "Artist already in the data base with another ID" }, 409 try: artist.modify_from_db(data[n.NM], data[n.CTRY], data[n.GNR]) return artist.json(), 200 except: return {n.MSG: "Error Description"}, 500 else: return {n.MSG: "Artist not found"}, 404
def get(self, id): artist = ArtistModel.find_by_id(id) if artist: return artist.json(), 200 else: return {n.MSG: "Artist not found"}, 404