def create_tag(tag: RequestTagObject) -> ResultWithData[str]: tag_id = short_unique_id() tag_created = Tag(tag_id=tag_id, name=tag.name, pretty_name_sv=tag.pretty_name_sv, pretty_name_en=tag.pretty_name_en) return get_result_with_data(tag_created.tag_id)
def remove_tag(tag_id: str) -> ResultWithData: tag = Tag.get(tag_id=tag_id) if tag is None: return get_result_with_error(TAG_ID_NOT_EXIST) else: tag.delete() return get_result_with_data("The tag was successfully deleted")
def validate_song(song: Dict) -> ResultWithData[RequestSongObject]: title_res = validate_required_str(song, 'title') if title_res.is_error: return get_result_with_error(title_res.message) melody_res = validate_str(song, 'melody') if melody_res.is_error: return get_result_with_error(melody_res.message) author_res = validate_str(song, 'author') if author_res.is_error: return get_result_with_error(author_res.message) text_res = validate_required_str(song, 'text') if text_res.is_error: return get_result_with_error(text_res.message) tags_res = validate_list(song, 'tags') if tags_res.is_error: return get_result_with_error(tags_res.message) tag_ids = [] for tag in tags_res.data: tag_id_res = validate_short_id(tag) if tag_id_res.is_error: return get_result_with_error(tag_id_res.message) tag_ids.append(tag_id_res.data) return get_result_with_data( RequestSongObject(song_id=None, title=title_res.data, melody=melody_res.data, author=author_res.data, text=text_res.data, tags=tag_ids))
def validate_dict(json: Dict, key: str) -> ResultWithData[Dict]: if key not in json: return get_result_with_error(f"Missing {key}") value = json[key] if type(value) is not dict: return get_result_with_error(f"{key} must be an object") return get_result_with_data(value)
def validate_short_id(id_str: str) -> ResultWithData[str]: if len(id_str) != 4: return get_result_with_error(ID_NOT_LENGTH_FOUR) for c in id_str: if c not in string.ascii_lowercase: return get_result_with_error(ID_NOT_LOWERCASE) return get_result_with_data(id_str)
def validate_str(json: Dict, key: str) -> ResultWithData[str]: if key not in json: return get_result_with_error(f"Missing {key}") str_str = json[key] if isinstance(str_str, str): return get_result_with_data(str_str) else: return get_result_with_error( f"Error in {key}: {str_str} is not a string")
def validate_bool(json: Dict, key: str) -> ResultWithData[bool]: if key not in json: return get_result_with_error(f"Missing {key}") bool_str = json[key] try: val = bool(bool_str) return get_result_with_data(val) except ValueError: return get_result_with_error( f"Error in {key}: {bool_str} is not a valid boolean")
def validate_int(json: Dict, key: str) -> ResultWithData[int]: if key not in json: return get_result_with_error(f"Missing {key}") int_str = json[key] try: val = int(int_str) return get_result_with_data(val) except ValueError: return get_result_with_error( f"Error in {key}: {int_str} is not a valid integer")
def create_song(song: RequestSongObject) -> ResultWithData[str]: song_id = short_unique_id() song_numbers = list(select(s.number for s in Song)) if song_numbers: highest_song_nbr = max(song_numbers) + 1 else: highest_song_nbr = 1 song_created = Song( song_id=song_id, number=highest_song_nbr, title=song.title, melody=song.melody, author=song.author, text=song.text ) return get_result_with_data(song_created.song_id)
def validate_song_update(song: Dict, song_id: str) -> ResultWithData[RequestSongObject]: id_res = validate_short_id(song_id) if id_res.is_error: return get_result_with_error(id_res.message) valid_song_res = validate_song(song) if valid_song_res.is_error: return get_result_with_error(valid_song_res.message) valid_song = valid_song_res.data return get_result_with_data( RequestSongObject(song_id=id_res.data, title=valid_song.title, melody=valid_song.melody, author=valid_song.author, text=valid_song.text, tags=valid_song.tags))
def get_song_by_name(title: str) -> ResultWithData[SongObject]: song = Song.get(title=title) if song is None: return get_result_with_error(SONG_TITLE_NOT_EXIST) else: return get_result_with_data(db_song_to_song_object(song))
def get_tag_by_id(tag_id: str) -> ResultWithData[TagObject]: tag = Tag.get(tag_id=tag_id) if tag is None: return get_result_with_error(TAG_ID_NOT_EXIST) else: return get_result_with_data(db_tag_to_tag_object(tag))
def remove_song(song_id: str) -> ResultWithData: Song.get(song_id=song_id).delete() return get_result_with_data({})
def update_song(song: RequestSongObject) -> ResultWithData: db_song = Song[song.song_id] db_song.set(title=song.title, melody=song.melody, author=song.author, text=song.text) return get_result_with_data({})
def get_song_by_id(song_id: str) -> ResultWithData[SongObject]: song = Song.get(song_id=song_id) if song is None: return get_result_with_error(SONG_ID_NOT_EXIST) else: return get_result_with_data(db_song_to_song_object(song))
def remove_songtotag(songtotag: SongToTagObject) -> ResultWithData: SongToTag.get(song=songtotag.song, tag=songtotag.tag).delete() return get_result_with_data({})
def create_songtotag(stt: SongToTagObject) -> ResultWithData[UUID]: stt_id = SongToTag(song=stt.song, tag=stt.tag) return get_result_with_data(stt_id)