async def save_word(self, arg_json): try: word_inst = Word.init_form_json( validate_json(Word.get_json_schema(), arg_json)) if await self.db_layer.word.record_is_exists(word_inst.word): return Error( "The dictionary already contains this word. Word - {}". format(word_inst.word)) await self.db_layer.word.save(vars(word_inst)) await self.producer.send_message(WordKafkaMessage(word_inst.word)) except (TypeError, ValidationError) as exp: return Error(exp) except JSONDecodeError: return Error("Not valid JSON was passed.") except Exception as exp: return self.__unexpected_exception__(exp) else: return "Word was added!!!"
async def update_word(self, arg_json): try: json_dict = validate_json(Word.get_json_schema(), arg_json) word = json_dict['word'] new_json_dict = exclude_json_fields( json_dict, fields=['translation', 'phrase', 'synonyms']) if new_json_dict: result = await self.db_layer.word.find_one_and_update( word, new_json_dict) if result is None: return Error("Word not found!!!") except (TypeError, ValidationError) as exp: return Error(exp) except JSONDecodeError: return Error("Not valid JSON was passed.") except Exception as exp: return self.__unexpected_exception__(exp) else: return "Word was updated!!!"