Advertisement
Guest User

COMMAND

a guest
Jan 26th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1.  
  2. 'use strict'
  3. const { DefaultCommand } = use('App/Domain/Commands/default')
  4. const { DnaItensEntity } = use('App/Infrastructure/Entities/DnaItensEntity')
  5. const { Enums: { apiResponse } } = use('App/Helpers/enums')
  6. const { DnasRepository } = use('App/Infrastructure/Repositories/DnasRepository')
  7. const { DnasItensRepository } = use('App/Infrastructure/Repositories/DnasItensRepository')
  8.  
  9. class SimianCreateCommand extends DefaultCommand {
  10.  
  11. rules() {
  12. return {
  13. dna: 'required|array',
  14. 'dna.*': 'required|string|min:6|max:6',
  15. }
  16. }
  17.  
  18. async execute({ response, request }) {
  19. let result = this.resultDefault();
  20.  
  21. const inputs = request.all();
  22. const validation = await this.validator(inputs, this.rules());
  23.  
  24. if (validation != null)
  25. return this.responseJSON({ result: { body: validation, code: apiResponse.codes.unprocessed }, response })
  26.  
  27. try {
  28. const dnaItensEntity = new DnaItensEntity();
  29. const { isSimian, resultIsSimian } = dnaItensEntity.verifyIsSimian(inputs.dna);
  30.  
  31. if (!isSimian)
  32. return this.responseJSON({ result: { body: { message: apiResponse.errors.simian.forbidden }, code: apiResponse.codes.forbidden }, response })
  33.  
  34. const dnasItensRepository = new DnasItensRepository();
  35. const haveDna = await dnasItensRepository.whereInValue(inputs.dna);
  36.  
  37. const compareExists = dnaItensEntity.compare(haveDna, inputs.dna);
  38.  
  39. if (compareExists.length)
  40. return this.responseJSON({ result: { body: { message: apiResponse.errors.simian.hasDNA }, code: apiResponse.codes.badRequest }, response })
  41.  
  42. const dnaData = await new DnasRepository().create({ is_simian: isSimian });
  43.  
  44. const dnaItensDataInsert = dnaItensEntity.handleCreate(dnaData.id, resultIsSimian);
  45.  
  46. await new DnasItensRepository().create(dnaItensDataInsert);
  47.  
  48. result.body = { data: dnaItensDataInsert }
  49. result.code = apiResponse.codes.success;
  50.  
  51. } catch (error) {
  52. result.body = { message: apiResponse.errors.simian.getUnprocessed }
  53. result.code = apiResponse.codes.unprocessed;
  54.  
  55. }
  56.  
  57. return this.responseJSON({ result, response })
  58.  
  59. }
  60. }
  61.  
  62. module.exports = SimianCreateCommand
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement