async def method_post( self, request: Request, body: dict, session: DBSession, token: dict, *args, **kwargs ) -> BaseHTTPResponse: request_model = RequestCreateMessageDto(body) try: message_queries.create_message(session=session, message=request_model, sender=token.get('id_auth')) session.commit_session() except DBUserNotExistsException: raise SanicUserNotFound('User not found') except(DBDataError, DBIntegrityError) as error: raise SanicDBException(str(error)) return await self.make_response_json(body={}, status=201)
async def method_post(self, request: Request, body: dict, session: DBSession, token: dict, *args, **kwargs) -> BaseHTTPResponse: request_model = RequestCreateMessageDto(body) try: recipient = message_queries.get_user(session=session, login=request_model.recipient) except DBUserNotExistsException: raise SanicUserNotFound('Recipient not found') uid = token.get('uid') try: sender = message_queries.get_user(session=session, user_id=uid) except DBUserNotExistsException: raise SanicUserNotFound('Sender not found') converse_id = {'sender_id': sender.id, 'recipient_id': recipient.id} db_message = message_queries.create_message(session, request_model, converse_id) try: session.commit_session() except (DBDataException, DBIntegrityException) as e: return await self.make_response_json(status=500, message=str(e)) response_model = ResponseMessageDto(db_message) return await self.make_response_json(body=response_model.dump(), status=201)
async def method_post(self, request: Request, body: dict, session: DBSession, token: dict, *args, **kwargs) -> BaseHTTPResponse: """ Создание сообщения """ user_id = token.get('user_id') request_model = RequestCreateMessageDto(body) try: db_message = message_queries.create_message(session, request_model, user_id=user_id) except DBUserNotExistsException: raise SanicUserNotFound('User not found') try: session.commit_session() except (DBDataException, DBIntegrityException) as e: raise SanicDBException(str(e)) response_model = ResponseMessageDto(db_message) return await self.make_response_json(body=response_model.dump(), status=201)
async def method_post(self, request: Request, body: dict, session, *args, **kwargs) -> BaseHTTPResponse: request_model = RequestCreateMessageDto(body) sender_id = body.get('eid') db_messages = message_queries.create_message(session, request_model, sender_id) session.commit_session() response_model = ResponseCreateMessageDto(db_messages) return await self.make_response_json(body=response_model.dump(), status=201)
async def method_post(self, request: Request, body: dict, session, token: dict, *args, **kwargs) -> BaseHTTPResponse: request_model = RequestCreateMessageDto(body) try: message = message_queries.create_message(session, request_model, token.get("eid")) except DBEmployeeNotExistsException: raise SanicEmployeeNotFound("Employee not found") try: session.commit_session() except (DBDataException, DBIntegrityException) as e: raise SanicDBException(str(e)) response_model = ResponseMessageDto(message) return await self.make_response_json(status=200, body=response_model.dump())
async def method_post( self, request: Request, body: dict, session: DBSession, token: dict, *args, **kwargs, ) -> BaseHTTPResponse: """ Send message to recipient by login :param request: :param body: :param session: :param token: :param args: :param kwargs: :return: """ request_model = RequestCreateMessageDto(body) try: recipient = get_user_by_login(session, login=request_model.recipient) except DBUserNotExists as e: raise SanicUserNotFound( f"Recipient {request_model.recipient} not found") from e db_message = create_message( session, message=request_model.message, recipient_id=recipient.id, sender_id=token["sub"], ) try: session.commit_session() except (DBDataException, DBIntegrityException) as e: raise SanicDBException(str(e)) response_model = ResponseMessageDto(db_message) return await self.make_response_json(status=200, body=response_model.dump())
async def method_post(self, request: Request, body: dict, session, token: dict, *args, **kwargs) -> BaseHTTPResponse: request = RequestCreateMessageDto(body) try: db_message = message.create_message(session, request, token.get("uid"), request.recipient) except DBUserNotExistsException: raise SanicUserNotFound('User not found') try: session.commit_session() except (DBDataException, DBIntegrityException) as e: raise SanicDBException(str(e)) response = ResponseMessageDto(db_message) return await self.make_response_json(response.dump(), status=201)
async def method_post(self, request: Request, body: dict, session, token, *args, **kwargs) -> BaseHTTPResponse: request_model = RequestCreateMessageDto(body) sender_id = token.get('uid') try: db_message = message_queries.create_message( session, request_model, sender_id) except DBUserNotExistsException: raise SanicUserConflictException('Recipient not found.') try: session.commit_session() except (DBDataException, DBIntegrityException) as e: raise SanicDBException(str(e)) response_model = ResponseMessageDto(db_message) return await self.make_response_json(body=response_model.dump(), status=201)
async def method_post(self, request: Request, body: dict, session: DBSession, token: dict, *args, **kwargs) -> BaseHTTPResponse: # получение DTO объекта request_model = RequestCreateMessageDto(body) # проверяем, существуют ли два пользователя в БД (sender and recipient) # проверяем, что оба пользователя не удалены из БД try: recipient = user_queries.get_user(session=session, login=request_model.login) user_queries.get_user(session=session, user_id=token['id']) except DBUserNotFoundException: raise SanicUserNotFoundException('User not found') except DBUserDeletedException: raise SanicUserDeletedException('User deleted') user_queries.update_messages_stats(session, login=request_model.login, role='recipient', context=self.context) user_queries.update_messages_stats(session, user_id=token['id'], role='sender', context=self.context) # коммитим данные в БД try: db_message = message_queries.create_message( session, request_model, token, recipient_id=recipient.id) session.commit_session() except (DBIntegrityException, DBDataException) as error: raise SanicDBException(str(error)) response_model = ResponseGetCreatedMessageDto(db_message) return await self.make_response_json( body=response_model.dump(), status=201, )
async def method_post(self, request: Request, body: dict, session, *args, **kwargs) -> BaseHTTPResponse: request_model = RequestCreateMessageDto(body) uid = read_token(request.headers['authorization'])['uid'] try: db_message = message_queries.create_message(session, request_model, uid) except DBUserNotExistsException as e: return await self.make_response_json(status=400, message="No such recipient") except DBMessageCreateException as e: raise SanicDBException(str(e)) try: session.commit_session() except (DBDataException, DBIntegrityException) as e: raise SanicDBException(str(e)) response_model = ResponseMessageDto(db_message) return await self.make_response_json(body=response_model.dump(), status=201)
async def method_post(self, request: Request, body: dict, session: DBSession, *args, **kwargs) -> BaseHTTPResponse: request_model = RequestCreateMessageDto(body) try: message = message_queries.create_message(session, request_model, body['uid']) except DBUserNotExistsException: raise SanicUserNotFound('Recipient not found') try: session.commit_session() except (DBDataException, DBIntegrityException) as e: raise SanicDBException(str(e)) try: if hasattr(request_model, 'file_ids'): for file_id in request_model.file_ids: file_queries.create_msg_file_relation( session=session, user_id=body['uid'], msg_id=message.id, file_id=file_id ) except DBFileNotExistsException: raise SanicFileNotFound('File not found') except DBResourceOwnerException: raise SanicUserConflictException("This is not your file") try: session.commit_session() except (DBDataException, DBIntegrityException) as e: raise SanicDBException(str(e)) message.file_ids = [] if hasattr(request_model, 'file_ids'): message.file_ids = request_model.file_ids response_model = ResponseCreateMessageDto(message) return await self.make_response_json(body=response_model.dump(), status=201)