當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript IReply.default方法代碼示例

本文整理匯總了TypeScript中hapi.IReply.default方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript IReply.default方法的具體用法?TypeScript IReply.default怎麽用?TypeScript IReply.default使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在hapi.IReply的用法示例。


在下文中一共展示了IReply.default方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: handler

export default async function handler(req: Request, reply: IReply) {
  const { hackId: hackid } = req.params

  const hack = await HackModel
    .findOne({ hackid }, 'hackid challenges')
    .populate('challenges', 'challengeid name')
    .exec()

  if (hack === null) {
    reply(Boom.notFound('Hack not found'))
    return
  }

  const challenges = hack.challenges.map((challenge): JSONApi.ResourceIdentifierObject => ({
    type: 'challenges',
    id: challenge.challengeid,
  }))

  const includedChallenges = hack.challenges.map((challenge): ChallengeResource.ResourceObject => ({
    links: { self: `/challenges/${challenge.challengeid}` },
    type: 'challenges',
    id: challenge.challengeid,
    attributes: { name: challenge.name },
  }))

  const challengesResponse: HackChallengesRelationship.TopLevelDocument = {
    links: { self: `/hacks/${encodeURIComponent(hack.hackid)}/challenges` },
    data: challenges,
    included: includedChallenges,
  }

  reply(challengesResponse)
}
開發者ID:TechNottingham,項目名稱:Hack24-API,代碼行數:33,代碼來源:get-hack-challenges.ts

示例2: handler

export default async function handler(req: Request, reply: IReply) {
  const requestDoc: ChallengeResource.TopLevelDocument = req.payload

  const challenge = new ChallengeModel({
    challengeid: slugify(requestDoc.data.attributes.name),
    name: requestDoc.data.attributes.name,
    members: [],
  })

  try {
    await challenge.save()
  } catch (err) {
    if (err.code === MongoDBErrors.E11000_DUPLICATE_KEY) {
      reply(Boom.conflict('Challenge already exists'))
      return
    }
    throw err
  }

  const challengeResponse: ChallengeResource.TopLevelDocument = {
    links: {
      self: `/challenges/${encodeURIComponent(challenge.challengeid)}`,
    },
    data: {
      type: 'challenges',
      id: challenge.challengeid,
      attributes: {
        name: challenge.name,
      },
    },
  }

  reply(challengeResponse).code(201)
}
開發者ID:TechNottingham,項目名稱:Hack24-API,代碼行數:34,代碼來源:create-challenge.ts

示例3: handler

export default async function handler(req: Request, reply: IReply) {
  const { hackId: hackid } = req.params

  const hack = await HackModel
    .findOne({ hackid }, 'hackid name')
    .exec()

  if (hack === null) {
    reply(Boom.notFound('Hack not found'))
    return
  }

  const hackResponse: HackResource.TopLevelDocument = {
    links: { self: `/hacks/${encodeURIComponent(hack.hackid)}` },
    data: {
      type: 'hacks',
      id: hack.hackid,
      attributes: {
        name: hack.name,
      },
    },
  }

  reply(hackResponse)
}
開發者ID:TechNottingham,項目名稱:Hack24-API,代碼行數:25,代碼來源:get-hack.ts

示例4: handler

export default async function handler(req: Request, reply: IReply) {
  const { challengeId: challengeid } = req.params

  const challenge = await ChallengeModel
    .findOne({ challengeid }, 'challengeid name')
    .exec()

  if (challenge === null) {
    reply(Boom.notFound('Challenge not found'))
    return
  }

  const challengeResponse: ChallengeResource.TopLevelDocument = {
    links: { self: `/challenges/${encodeURIComponent(challenge.challengeid)}` },
    data: {
      type: 'challenges',
      id: challenge.challengeid,
      attributes: {
        name: challenge.name,
      },
    },
  }

  reply(challengeResponse)
}
開發者ID:TechNottingham,項目名稱:Hack24-API,代碼行數:25,代碼來源:get-challenge.ts

示例5: handler

export default async function handler(req: Request, reply: IReply) {
  const { teamId: teamid } = req.params

  const team = await TeamModel
    .findOne({ teamid }, 'teamid entries')
    .populate('entries', 'hackid name')
    .exec()

  if (team === null) {
    reply(Boom.notFound('Team not found'))
    return
  }

  const entries = team.entries.map((hack): JSONApi.ResourceIdentifierObject => ({
    type: 'hacks',
    id: hack.hackid,
  }))

  const includedHacks = team.entries.map((hack): HackResource.ResourceObject => ({
    links: { self: `/hacks/${hack.hackid}` },
    type: 'hacks',
    id: hack.hackid,
    attributes: { name: hack.name },
  }))

  const entriesResponse: TeamEntriesRelationship.TopLevelDocument = {
    links: { self: `/teams/${encodeURIComponent(team.teamid)}/entries` },
    data: entries,
    included: includedHacks,
  }

  reply(entriesResponse)
}
開發者ID:TechNottingham,項目名稱:Hack24-API,代碼行數:33,代碼來源:get-team-entries.ts

示例6: handler

export default async function handler(req: Request, reply: IReply) {
  const { teamId: teamid } = req.params

  const team = await TeamModel
    .findOne({ teamid }, 'teamid members')
    .populate('members', 'userid name')
    .exec()

  if (team === null) {
    reply(Boom.notFound('Team not found'))
    return
  }

  const members = team.members.map((member): JSONApi.ResourceIdentifierObject => ({
    type: 'users',
    id: member.userid,
  }))

  const includedUsers = team.members.map((member): UserResource.ResourceObject => ({
    links: { self: `/users/${member.userid}` },
    type: 'users',
    id: member.userid,
    attributes: { name: member.name },
  }))

  const membersResponse: TeamMembersRelationship.TopLevelDocument = {
    links: { self: `/teams/${encodeURIComponent(team.teamid)}/members` },
    data: members,
    included: includedUsers,
  }

  reply(membersResponse)
}
開發者ID:TechNottingham,項目名稱:Hack24-API,代碼行數:33,代碼來源:get-team-members.ts

示例7: handler

export default async function handler(req: Request, reply: IReply) {
  const { teamId: teamid } = req.params
  const requestDoc: TeamEntriesRelationship.TopLevelDocument = req.payload

  const team = await TeamModel
    .findOne({ teamid }, 'teamid name entries')
    .populate('entries', 'hackid')
    .exec()

  if (team === null) {
    reply(Boom.notFound('Team not found'))
    return
  }

  const hackIdsToAdd = requestDoc.data.map((hack) => hack.id)
  const existingHackIds = hackIdsToAdd.filter((hackIdToAdd) => team.entries.some((actualhack) => actualhack.hackid === hackIdToAdd))

  if (existingHackIds.length > 0) {
    reply(Boom.badRequest('One or more hacks are already entries of this team'))
    return
  }

  const hacks = await HackModel
    .find({ hackid: { $in: hackIdsToAdd } }, 'hackid name')
    .exec()

  if (hacks.length !== hackIdsToAdd.length) {
    reply(Boom.badRequest('One or more of the specified hacks could not be found'))
    return
  }

  const hackObjectIds = hacks.map((hack) => hack._id)

  const teams = await TeamModel
    .find({ entries: { $in: hackObjectIds } }, 'teamid')
    .exec()

  if (teams.length > 0) {
    reply(Boom.badRequest('One or more of the specified hacks are already in a team'))
    return
  }

  team.entries = team.entries.concat(hacks.map((hack) => hack._id))

  await team.save()

  const eventBroadcaster: EventBroadcaster = req.server.app.eventBroadcaster
  hacks.forEach((hack) => {
    eventBroadcaster.trigger('teams_update_entries_add', {
      teamid: team.teamid,
      name: team.name,
      entry: {
        hackid: hack.hackid,
        name: hack.name,
      },
    }, req.logger)
  })

  reply()
}
開發者ID:TechNottingham,項目名稱:Hack24-API,代碼行數:60,代碼來源:add-team-entries.ts

示例8: handler

export default async function handler(req: Request, reply: IReply) {
  const { teamId: teamid } = req.params
  const requestDoc: TeamMembersRelationship.TopLevelDocument = req.payload

  const team = await TeamModel
    .findOne({ teamid }, 'teamid name members')
    .populate('members', 'userid')
    .exec()

  if (team === null) {
    reply(Boom.notFound('Team not found'))
    return
  }

  const userIdsToAdd = requestDoc.data.map((user) => user.id)
  const existingUserIds = userIdsToAdd.filter((userIdToAdd) => team.members.some((actualMember) => actualMember.userid === userIdToAdd))

  if (existingUserIds.length > 0) {
    reply(Boom.badRequest('One or more users are already members of this team'))
    return
  }

  const users = await UserModel
    .find({ userid: { $in: userIdsToAdd } }, 'userid name')
    .exec()

  if (users.length !== userIdsToAdd.length) {
    reply(Boom.badRequest('One or more of the specified users could not be found'))
    return
  }

  const userObjectIds = users.map((user) => user._id)

  const teams = await TeamModel
    .find({ members: { $in: userObjectIds } }, 'teamid')
    .exec()

  if (teams.length > 0) {
    reply(Boom.badRequest('One or more of the specified users are already in a team'))
    return
  }

  team.members = team.members.concat(users.map((user) => user._id))

  await team.save()

  const eventBroadcaster: EventBroadcaster = req.server.app.eventBroadcaster
  users.forEach((user) => {
    eventBroadcaster.trigger('teams_update_members_add', {
      teamid: team.teamid,
      name: team.name,
      member: {
        userid: user.userid,
        name: user.name,
      },
    }, req.logger)
  })

  reply()
}
開發者ID:TechNottingham,項目名稱:Hack24-API,代碼行數:60,代碼來源:add-team-members.ts

示例9: handler

export default async function handler(request: Request, reply: IReply) {
  const requestDoc: AttendeeResource.TopLevelDocument = request.payload

  const attendee = new AttendeeModel({
    attendeeid: requestDoc.data.id,
  })

  try {
    await attendee.save()
  } catch (err) {
    if (err.code === MongoDBErrors.E11000_DUPLICATE_KEY) {
      reply(Boom.conflict('Attendee already exists'))
      return
    }
    throw err
  }

  const attendeeResponse: AttendeeResource.TopLevelDocument = {
    links: { self: `/attendees/${encodeURIComponent(attendee.attendeeid)}` },
    data: {
      type: 'attendees',
      id: attendee.attendeeid,
    },
  }

  reply(attendeeResponse).code(201)
}
開發者ID:TechNottingham,項目名稱:Hack24-API,代碼行數:27,代碼來源:create-attendee.ts

示例10: reply

export async function updateAccount(server: Server, request: Request, reply: IReply)
{
    const id = request.params["id"];
    const apiKey = request.auth.credentials.apiKey;
    const accountToUpdate: Account = request.payload;
    let dbAccount: Account;

    // Get the original account
    try
    {
        dbAccount = await Accounts.findByApiKey(apiKey);

        // Only update the account if the ApiKey and Id match.
        if (!dbAccount || dbAccount._id !== id || dbAccount.apiKey !== apiKey)
        {
            return reply(notFound("No account find with that id and API key combination."));
        }
    }
    catch (e)
    {
        console.error("Failed to retrieve account when updating.", e);

        return reply(boom(e));
    }

    // Transfer update props to the dbAccount
    for (let prop in accountToUpdate)
    {
        dbAccount[prop] = accountToUpdate[prop];
    }

    // Never update the account's ID or apiKey
    dbAccount._id = id;
    dbAccount.apiKey = apiKey;

    try
    {
        const update = await Accounts.Database.put(accountToUpdate);

        if (!update.ok)
        {
            console.error("Failed to update account.", update);

            return reply(expectationFailed("Failed to update account."));
        }
        
        dbAccount._rev = update.rev;
    }
    catch (e)
    {
        console.error("Failed to update account.", e);

        return reply(boom(e));
    }

    return reply(dbAccount);
}
開發者ID:nozzlegear,項目名稱:stages-api,代碼行數:57,代碼來源:accounts-routes.ts


注:本文中的hapi.IReply.default方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。