|
|
@@ -0,0 +1,103 @@
|
|
|
+import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify'
|
|
|
+import { SmsTaskService } from '../services/sms-task.service'
|
|
|
+import { CreateSmsTaskBody, UpdateSmsTaskBody, ListSmsTaskQuery, GetSmsTaskParams } from '../dto/sms-task.dto'
|
|
|
+
|
|
|
+export class SmsTaskController {
|
|
|
+ private smsTaskService: SmsTaskService
|
|
|
+
|
|
|
+ constructor(app: FastifyInstance) {
|
|
|
+ this.smsTaskService = new SmsTaskService(app)
|
|
|
+ }
|
|
|
+
|
|
|
+ async create(request: FastifyRequest<{ Body: CreateSmsTaskBody }>, reply: FastifyReply) {
|
|
|
+ try {
|
|
|
+ const task = await this.smsTaskService.create(request.body)
|
|
|
+ return reply.code(201).send({
|
|
|
+ message: '创建成功',
|
|
|
+ data: task
|
|
|
+ })
|
|
|
+ } catch (error) {
|
|
|
+ return reply.code(500).send({
|
|
|
+ message: '创建失败',
|
|
|
+ error: error instanceof Error ? error.message : String(error)
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async list(request: FastifyRequest<{ Querystring: ListSmsTaskQuery }>, reply: FastifyReply) {
|
|
|
+ try {
|
|
|
+ const result = await this.smsTaskService.findAll(request.query)
|
|
|
+ return reply.send({
|
|
|
+ message: '查询成功',
|
|
|
+ ...result
|
|
|
+ })
|
|
|
+ } catch (error) {
|
|
|
+ return reply.code(500).send({
|
|
|
+ message: '查询失败',
|
|
|
+ error: error instanceof Error ? error.message : String(error)
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async getById(request: FastifyRequest<{ Params: GetSmsTaskParams }>, reply: FastifyReply) {
|
|
|
+ try {
|
|
|
+ const { id } = request.params
|
|
|
+ const task = await this.smsTaskService.findById(id)
|
|
|
+ return reply.send({
|
|
|
+ message: '查询成功',
|
|
|
+ data: task
|
|
|
+ })
|
|
|
+ } catch (error) {
|
|
|
+ if (error instanceof Error && error.message.includes('Could not find')) {
|
|
|
+ return reply.code(404).send({
|
|
|
+ message: '任务不存在'
|
|
|
+ })
|
|
|
+ }
|
|
|
+ return reply.code(500).send({
|
|
|
+ message: '查询失败',
|
|
|
+ error: error instanceof Error ? error.message : String(error)
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async update(request: FastifyRequest<{ Body: UpdateSmsTaskBody }>, reply: FastifyReply) {
|
|
|
+ try {
|
|
|
+ const { id, ...updateData } = request.body
|
|
|
+ const task = await this.smsTaskService.update(id, { id, ...updateData })
|
|
|
+ return reply.send({
|
|
|
+ message: '更新成功',
|
|
|
+ data: task
|
|
|
+ })
|
|
|
+ } catch (error) {
|
|
|
+ if (error instanceof Error && error.message.includes('Could not find')) {
|
|
|
+ return reply.code(404).send({
|
|
|
+ message: '任务不存在'
|
|
|
+ })
|
|
|
+ }
|
|
|
+ return reply.code(500).send({
|
|
|
+ message: '更新失败',
|
|
|
+ error: error instanceof Error ? error.message : String(error)
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async delete(request: FastifyRequest<{ Params: GetSmsTaskParams }>, reply: FastifyReply) {
|
|
|
+ try {
|
|
|
+ const { id } = request.params
|
|
|
+ await this.smsTaskService.delete(id)
|
|
|
+ return reply.send({
|
|
|
+ message: '删除成功'
|
|
|
+ })
|
|
|
+ } catch (error) {
|
|
|
+ if (error instanceof Error && error.message.includes('Could not find')) {
|
|
|
+ return reply.code(404).send({
|
|
|
+ message: '任务不存在'
|
|
|
+ })
|
|
|
+ }
|
|
|
+ return reply.code(500).send({
|
|
|
+ message: '删除失败',
|
|
|
+ error: error instanceof Error ? error.message : String(error)
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|