Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

get.js 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. 'use strict'
  2. const Joi = require('joi')
  3. const apiSchema = require('../../schemas/api')
  4. const errorSchema = require('../../schemas/errors')
  5. const pluginConfig = {
  6. handlerType: 'profile',
  7. docs: {
  8. description: 'Returns a single profile with tags',
  9. notes: 'returns from the Profiles Table',
  10. },
  11. }
  12. const responseSchemas = {
  13. profile: Joi.object({
  14. profile_id: Joi.number(),
  15. user_id: Joi.number(),
  16. user_name: Joi.string(),
  17. responses: Joi.array().items(),
  18. tags: Joi.array().items(),
  19. user_media: Joi.string(),
  20. user_type: Joi.any(),
  21. user: Joi.object(),
  22. }),
  23. error: errorSchema.single,
  24. }
  25. const validators = {
  26. params: Joi.object({ profile_id: Joi.number() }),
  27. }
  28. module.exports = {
  29. method: 'GET',
  30. path: '/{profile_id}',
  31. options: {
  32. ...pluginConfig.docs,
  33. tags: ['api'],
  34. /** Protect this route with authentication? */
  35. auth: false,
  36. cors: true,
  37. handler: async function (request, h) {
  38. const { profile_id } = request.params
  39. const { profileService } = request.server.services()
  40. const res = {
  41. ok: true,
  42. handler: pluginConfig.handlerType,
  43. data: null,
  44. }
  45. res.data = await profileService.getProfile(profile_id)
  46. try {
  47. return h.response(res).code(200)
  48. } catch (err) {
  49. return h
  50. .response({
  51. ok: false,
  52. handler: pluginConfig.handlerType,
  53. data: { error: `${err}` },
  54. })
  55. .code(409)
  56. }
  57. },
  58. /** Validate based on validators object */
  59. validate: {
  60. ...validators,
  61. failAction: 'log',
  62. },
  63. /** Validate the server response */
  64. response: {
  65. status: {
  66. 200: apiSchema.single.append({
  67. data: responseSchemas.profile,
  68. }),
  69. 409: apiSchema.single.append({
  70. data: responseSchemas.error,
  71. }),
  72. },
  73. },
  74. },
  75. }