Exemple #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)
Exemple #2
0
def create_msg(session: DBSes, sender_id: int, msg: str,
               recipient_id: int) -> DBMsg:
    msg = DBMsg(
        sender_id=sender_id,
        message=msg,
        recipient_id=recipient_id,
    )

    session.add_model(msg)

    return msg
Exemple #3
0
def get_emp(session: DBSes, *, login: str = None, emp_id: int = None) -> DBEmp:
    db_emp = None

    if login is not None:
        db_emp = session.get_emp_by_login(login)
    elif emp_id is not None:
        db_emp = session.get_emp_by_id(emp_id)

    if db_emp is None:
        raise DBEmpNotExistException()

    return db_emp
Exemple #4
0
def delete_msg(session: DBSes, user_id: int, mid: int):
    db_msg = session.get_msg_by_id(mid=mid, user_id=user_id)

    if db_msg is None:
        raise DBMsgNotFoundException()

    setattr(db_msg, 'is_delete', True)
Exemple #5
0
def create_emp(session: DBSes, emp: ReqCreateEmpDto, hash_pwd: bytes) -> DBEmp:
    new_emp = DBEmp(
        login=emp.login,
        pwd=hash_pwd,
        first_name=emp.first_name,
        last_name=emp.last_name,
        position=emp.position,
        department=emp.department,
    )

    if session.get_emp_by_login(new_emp.login) is not None:
        raise DBEmpExistException()

    session.add_model(new_emp)

    return new_emp
Exemple #6
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)
Exemple #7
0
def patch_msg(session: DBSes, message: str, mid: int, user_id: int) -> DBMsg:

    db_msg = session.get_msg_by_id(mid, user_id)
    if db_msg is None:
        raise DBMsgNotFoundException()

    setattr(db_msg, 'message', message)

    return db_msg
Exemple #8
0
def patch_emp(session: DBSes, emp: ReqPatchEmpDto, eid: int) -> DBEmp:

    db_emp = session.get_emp_by_id(eid)

    for attr in emp.fields:
        if hasattr(emp, attr):
            setattr(db_emp, attr, getattr(emp, attr))

    return db_emp
Exemple #9
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)
Exemple #10
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())
Exemple #11
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)
Exemple #12
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())
Exemple #13
0
def get_msgs(session: DBSes, user_id: int) -> List[DBMsg]:
    return session.get_msg_all(user_id)
Exemple #14
0
def get_msg(session: DBSes, user_id: int, mid: int) -> DBMsg:
    db_msg = session.get_msg(user_id, mid)
    if db_msg is None:
        raise DBMsgNotFoundException()
    return db_msg
Exemple #15
0
def get_emps(session: DBSes) -> List[DBEmp]:
    return session.get_emp_all()
Exemple #16
0
def delete_emp(session: DBSes, emp_id: int) -> DBEmp:
    db_emp = session.get_emp_by_id(emp_id)
    db_emp.is_delete = True
    return db_emp