コード例 #1
0
    async def method_post(self, request: Request, body: dict, session, *args,
                          **kwargs) -> BaseHTTPResponse:

        request_model = RequestCreateEmployeeDto(body)

        try:
            hashed_password = generate_hash(request_model.password)
        except GeneratePasswordHashException as e:
            raise SanicPasswordHashException(str(e))

        try:
            db_employee = employee_queries.create_employee(
                session, request_model, hashed_password)
        except DBEmployeeExistsException:
            raise SanicEmployeeConflictException('Login is busy')

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

        response_model = ResponseEmployeeDto(db_employee)

        return await self.make_response_json(body=response_model.dump(),
                                             status=201)
コード例 #2
0
    async def method_get(self, request: Request, body: dict,
                         session: DBSession, token: dict, *args,
                         **kwargs) -> BaseHTTPResponse:

        if 'id' in request.args:
            eid = int(request.args['id'][0])
        elif 'login' in request.args:
            eid = employee_queries.get_employee_id_by_login(
                session, request.args['login'][0])
        else:
            raise SanicBadRequest('Bad request')

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

        try:
            db_employee = employee_queries.get_employee(session,
                                                        employee_id=eid)
        except DBEmployeeNotExistsException:
            raise SanicEmployeeNotFound('User not found')

        response_model = ResponseEmployeeDto(db_employee)

        return await self.make_response_json(body=response_model.dump(),
                                             status=202)
コード例 #3
0
    async def method_get(self, request: Request, body: dict, session: DBSession, *args,
                           **kwargs) -> BaseHTTPResponse:

        db_employee = employee_queries.get_employees(session)
        response_model = ResponseEmployeeDto(db_employee, many=True)

        return await self.make_response_json(status=200, body=response_model.dump())
コード例 #4
0
ファイル: employee.py プロジェクト: NailNaMeste/messenger
    async def method_get(self, request: Request, body: dict,
                         session: DBSession, eid: int, token: dict, *args,
                         **kwargs) -> BaseHTTPResponse:

        if token.get('eid') != eid:
            return await self.make_response_json(status=403)
        db_employee = employee_queries.get_employee(session, eid)
        response_model = ResponseEmployeeDto(db_employee)

        return await self.make_response_json(body=response_model.dump())
コード例 #5
0
    async def method_post(self, request: Request, body: dict, session, *args,
                          **kwargs) -> BaseHTTPResponse:

        request_model = RequestCreateEmployeeDto(body)

        db_employee = employee_queries.create_employee(session, request_model)
        session.commit_session()

        response_model = ResponseEmployeeDto(db_employee)

        return await self.make_response_json(body=response_model.dump(),
                                             status=201)
コード例 #6
0
    async def method_get(self, request: Request, body: dict,
                         session: DBSession, eid: int, token: dict, *args,
                         **kwargs) -> BaseHTTPResponse:

        try:
            employee = employee_queries.get_employee(session,
                                                     employee_id=int(
                                                         token.get('eid')))
        except DBEmployeeNotExistsException:
            raise SanicEmployeeNotFound('Employee not found')

        response_employee = ResponseEmployeeDto(employee)

        return await self.make_response_json(status=200,
                                             body=response_employee.dump())
コード例 #7
0
    async def method_patch(
            self, request: Request, body: dict, session: DBSession, eid: int, *args, **kwargs
    ) -> BaseHTTPResponse:

        request_model = RequestPatchEmployeeDto(body)

        try:
            employee = employee_queries.patch_employee(session, request_model, eid)
        except DBEmployeeNotExistsException:
            raise SanicEmployeeNotFound('Employee not found')

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

        response_model = ResponseEmployeeDto(employee)

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