category: Tutorial
name: updateEmployment
uri: '/tutorials/employments/{entryNumber}'
method: PUT
routing_auto_generation_mode: true
request_timeout: 60
connect_timeout: 60
spec:
  response:
    normal:
      codes:
        - 200
  request:
    headers:
      type: object
      properties:
        Content-Type:
          type: string
          default: application/json
      required:
        - Content-Type
    body:
      type: object
      properties:
        firstName:
          type: string
          pattern: '[a-zA-Z]'
        lastName:
          type: string
          pattern: '[a-zA-Z]'
        email:
          type: string
          format: email
        salaryRequirements:
          type: integer
          minimum: 0
          maximum: 99999999
    resources:
      type: object
      properties:
        entryNumber:
          type: string
      required:
        - entryNumber
commands:
  - command: script
    kwargs:
      code: |-
        async with model.aiodb() as conn:
            if not context.request.resources.entryNumber:
                raise Error(400, reason="entryNumber is not specified")

            cursor = await conn.execute(model.Employment.select().where(model.Employment.c.entryNumber==context.request.resources.entryNumber))
            if cursor.rowcount == 0:
                raise Error(404, reason="Could not found employment")

            row = await cursor.fetchone()
            employment = rowtodict(row)
            employment.update(context.request.body.dictionary)
            await conn.execute(model.Employment.update().where(model.Employment.c.entryNumber==context.request.resources.entryNumber).values(**employment))
            context.session.finish({"entryNumber": context.request.resources.entryNumber})