Exemplo n.º 1
0
    async def method_post(self, request: Request, body: dict, session: DBSes,
                          token: dict, *args, **kwargs) -> BaseHTTPResponse:

        sender_id = token.get('eid')
        if not isinstance(sender_id, int):
            return await self.make_response_json(status=403)

        req_model = ReqCreateMsgDto(body)

        try:
            rec = emp_q.get_emp(session, login=req_model.recipient)
        except DBEmpNotExistException as e:
            raise SanicUserNotFoundException()

        msg = msg_q.create_msg(session, sender_id, req_model.message, rec.id)

        try:
            session.commit_session()
        except (DBIntegrityException, DBDataException) as e:
            raise SanicDBException(str(e))

        resp_model = RespMsgDto(msg)

        return await self.make_response_json(body=resp_model.dump(),
                                             status=201)
Exemplo n.º 2
0
    async def method_delete(self, request: Request, body: dict, session: DBSes,
                            eid: int, token: dict, *args,
                            **kwargs) -> BaseHTTPResponse:

        if token.get('eid') != eid:
            return await self.make_response_json(status=403)

        try:
            employee = emp_q.delete_emp(session, emp_id=eid)
        except DBEmpNotExistException as e:
            raise SanicEmpNotFoundException('emp not Found')

        try:
            session.commit_session()
        except (DBDataException, DBIntegrityException) as e:
            raise SanicDBException(str(e))

        return await self.make_response_json(status=204)
Exemplo n.º 3
0
    async def method_delete(self, request: Request, body: dict, session: DBSes,
                            mid: int, token: dict, *args,
                            **kwargs) -> BaseHTTPResponse:

        user_id = token.get('eid')
        if not isinstance(user_id, int):
            return await self.make_response_json(status=403)

        try:
            msg_q.delete_msg(session, user_id=user_id, mid=mid)
        except DBMsgNotFoundException as e:
            raise SanicMsgNotFoundException('Msg not Found')

        try:
            session.commit_session()
        except (DBDataException, DBIntegrityException) as e:
            raise SanicDBException(str(e))

        return await self.make_response_json(status=204)
Exemplo n.º 4
0
    async def method_get(self, request: Request, body: dict, session: DBSes,
                         eid: int, token: dict, *args,
                         **kwargs) -> BaseHTTPResponse:

        if token.get('eid') != eid:
            return await self.make_response_json(status=403)

        # req_model = ReqPatchEmpDto(body)

        try:
            emp = emp_q.get_emp(session, emp_id=eid)
            session.commit_session()
        except DBEmpNotExistException as e:
            raise SanicEmpNotFoundException('user not Found')
        except (DBIntegrityException, DBDataException) as e:
            raise SanicDBException(str(e))

        resp_model = RespEmpDto(emp)

        return await self.make_response_json(status=200,
                                             body=resp_model.dump())
Exemplo n.º 5
0
    async def method_post(self, request: Request, body: dict, session: DBSes,
                          *args, **kwargs) -> BaseHTTPResponse:

        req_model = ReqCreateEmpDto(body)

        try:
            hash_pwd = generate_hash(req_model.password)
        except GeneratePwdHashException as e:
            raise SanicPwdHashException(str(e))

        try:
            db_emp = emp_q.create_emp(session, req_model, hash_pwd)
        except DBEmpExistException as e:
            raise SanicEmpConflictException('Login is busy')
        try:
            session.commit_session()
        except (DBIntegrityException, DBDataException) as e:
            raise SanicDBException(str(e))

        resp_model = RespEmpDto(db_emp)

        return await self.make_response_json(body=resp_model.dump(),
                                             status=201)
Exemplo n.º 6
0
    async def method_patch(self, request: Request, body: dict, session: DBSes,
                           mid: int, token: dict, *args,
                           **kwargs) -> BaseHTTPResponse:

        user_id = token.get('eid')
        if not isinstance(user_id, int):
            return await self.make_response_json(status=403)

        req_model = ReqPatchMsgDto(body)

        try:
            msg = msg_q.patch_msg(session, req_model.message, mid, user_id)
        except DBMsgNotFoundException as e:
            raise SanicMsgNotFoundException('Msg not Found')

        try:
            session.commit_session()
        except (DBIntegrityException, DBDataException) as e:
            raise SanicDBException(str(e))

        resp_model = RespMsgDto(msg)

        return await self.make_response_json(status=200,
                                             body=resp_model.dump())