Listing_SuggestPrice/backend/app/controllers/notifications_controller.ts

42 lines
1.5 KiB
TypeScript

import type { HttpContext } from '@adonisjs/core/http'
import NotificationService from '#services/notification_service'
import type { NotificationType } from '#models/notification'
export default class NotificationsController {
/** GET /api/notifications?type=&isRead=&page=&perPage= */
async index({ request }: HttpContext) {
const page = Number(request.input('page', 1))
const perPage = Number(request.input('perPage', 25))
const type = request.input('type') as NotificationType | undefined
// isRead: 'true' | 'false' | undefined (không lọc)
const rawRead = request.input('isRead')
const isRead = rawRead === undefined ? undefined : rawRead === 'true' || rawRead === true
return NotificationService.list({ page, perPage, type, isRead })
}
/** GET /api/notifications/unread-count */
async unreadCount() {
return { count: await NotificationService.unreadCount() }
}
/** PATCH /api/notifications/:id/read */
async markRead({ params }: HttpContext) {
return NotificationService.markRead(Number(params.id))
}
/** PATCH /api/notifications/read-all?type= */
async markAllRead({ request }: HttpContext) {
const type = request.input('type') as NotificationType | undefined
const updated = await NotificationService.markAllRead(type)
return { updated }
}
/** DELETE /api/notifications/:id */
async destroy({ params, response }: HttpContext) {
await NotificationService.destroy(Number(params.id))
return response.noContent()
}
}