category: Tutorial
name: createEmployment
uri: /tutorials/employments
method: POST
routing_auto_generation_mode: true
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
      required:
        - firstName
        - lastName
        - email
        - salaryRequirements
commands:
  - command: script
    kwargs:
      code: |-
        entryNumber = await Counter.allocate("entryNumber")
        async with model.aiodb() as conn:
            cursor = await conn.execute(model.Employment.select().where(model.Employment.c.email==context.request.body.email))
            if cursor.rowcount != 0:
                raise Error(400, reason="E-mail address is already registered")

            await conn.execute(model.Employment.insert().values(entryNumber=entryNumber,
                                                                firstName=context.request.body.firstName,
                                                                lastName=context.request.body.lastName,
                                                                email=context.request.body.email,
                                                                salaryRequirements=context.request.body.salaryRequirements))
        context.session.finish({"entryNumber": entryNumber})
request_timeout: 60
connect_timeout: 60