|
|
@@ -0,0 +1,194 @@
|
|
|
+import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify'
|
|
|
+import { MessagesService } from '../services/messages.service'
|
|
|
+import { CreateMessageBody, CreateMessagesBody, ListMessagesQuery, UpdateMessageBody } from '../dto/messages.dto'
|
|
|
+
|
|
|
+export class MessagesController {
|
|
|
+ private messagesService: MessagesService
|
|
|
+
|
|
|
+ constructor(app: FastifyInstance) {
|
|
|
+ this.messagesService = new MessagesService(app)
|
|
|
+ }
|
|
|
+
|
|
|
+ async create(request: FastifyRequest<{ Body: CreateMessageBody }>, reply: FastifyReply) {
|
|
|
+ try {
|
|
|
+ const { userId, fishId, targetId, message } = request.body
|
|
|
+
|
|
|
+ if (!userId || !fishId || !targetId || !message) {
|
|
|
+ return reply.code(400).send({ message: '用户ID、鱼类ID、目标ID和消息内容不能为空' })
|
|
|
+ }
|
|
|
+
|
|
|
+ if (message.trim().length === 0) {
|
|
|
+ return reply.code(400).send({ message: '消息内容不能为空' })
|
|
|
+ }
|
|
|
+
|
|
|
+ await this.messagesService.create(userId, fishId, targetId, message)
|
|
|
+
|
|
|
+ return reply.code(201).send({
|
|
|
+ message: '消息创建成功'
|
|
|
+ })
|
|
|
+ } catch (error) {
|
|
|
+ return reply.code(500).send({ message: '创建消息失败', error: (error as Error).message })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async createBatch(request: FastifyRequest<{ Body: CreateMessagesBody }>, reply: FastifyReply) {
|
|
|
+ try {
|
|
|
+ const { messages } = request.body
|
|
|
+
|
|
|
+ if (!messages || !Array.isArray(messages) || messages.length === 0) {
|
|
|
+ return reply.code(400).send({ message: '消息列表不能为空' })
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证每个消息的必填字段
|
|
|
+ for (const msg of messages) {
|
|
|
+ if (!msg.userId || !msg.fishId || !msg.targetId || !msg.message) {
|
|
|
+ return reply.code(400).send({ message: '每个消息必须包含用户ID、鱼类ID、目标ID和消息内容' })
|
|
|
+ }
|
|
|
+ if (msg.message.trim().length === 0) {
|
|
|
+ return reply.code(400).send({ message: '消息内容不能为空' })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ const messageEntities = await this.messagesService.createBatch(messages)
|
|
|
+
|
|
|
+ return reply.code(201).send({
|
|
|
+ message: `成功创建 ${messageEntities.length} 条消息`
|
|
|
+ })
|
|
|
+ } catch (error) {
|
|
|
+ return reply.code(500).send({ message: '批量创建消息失败', error: (error as Error).message })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async list(request: FastifyRequest<{ Querystring: ListMessagesQuery }>, reply: FastifyReply) {
|
|
|
+ try {
|
|
|
+ const { page = 0, size = 20, userId, fishId, targetId } = request.query
|
|
|
+
|
|
|
+ const result = await this.messagesService.list(page, size, userId, fishId, targetId)
|
|
|
+
|
|
|
+ return reply.send({
|
|
|
+ message: '获取消息列表成功',
|
|
|
+ data: result
|
|
|
+ })
|
|
|
+ } catch (error) {
|
|
|
+ return reply.code(500).send({ message: '获取消息列表失败', error: (error as Error).message })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async getById(request: FastifyRequest<{ Params: { id: number } }>, reply: FastifyReply) {
|
|
|
+ try {
|
|
|
+ const { id } = request.params
|
|
|
+
|
|
|
+ const message = await this.messagesService.findById(id)
|
|
|
+
|
|
|
+ return reply.send({
|
|
|
+ message: '获取消息详情成功',
|
|
|
+ data: {
|
|
|
+ id: message.id,
|
|
|
+ userId: message.userId,
|
|
|
+ fishId: message.fishId,
|
|
|
+ targetId: message.targetId,
|
|
|
+ message: message.message,
|
|
|
+ createdAt: message.createdAt,
|
|
|
+ updatedAt: message.updatedAt
|
|
|
+ }
|
|
|
+ })
|
|
|
+ } catch (error) {
|
|
|
+ if ((error as Error).name === 'EntityNotFoundError') {
|
|
|
+ return reply.code(404).send({ message: '消息不存在' })
|
|
|
+ }
|
|
|
+ return reply.code(500).send({ message: '获取消息详情失败', error: (error as Error).message })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async update(request: FastifyRequest<{ Body: UpdateMessageBody }>, reply: FastifyReply) {
|
|
|
+ try {
|
|
|
+ const { id, message } = request.body
|
|
|
+
|
|
|
+ if (!id) {
|
|
|
+ return reply.code(400).send({ message: '消息ID不能为空' })
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!message || message.trim().length === 0) {
|
|
|
+ return reply.code(400).send({ message: '消息内容不能为空' })
|
|
|
+ }
|
|
|
+
|
|
|
+ const updatedMessage = await this.messagesService.update(id, message)
|
|
|
+
|
|
|
+ return reply.send({
|
|
|
+ message: '消息更新成功',
|
|
|
+ data: {
|
|
|
+ id: updatedMessage.id,
|
|
|
+ userId: updatedMessage.userId,
|
|
|
+ fishId: updatedMessage.fishId,
|
|
|
+ targetId: updatedMessage.targetId,
|
|
|
+ message: updatedMessage.message,
|
|
|
+ createdAt: updatedMessage.createdAt,
|
|
|
+ updatedAt: updatedMessage.updatedAt
|
|
|
+ }
|
|
|
+ })
|
|
|
+ } catch (error) {
|
|
|
+ if ((error as Error).name === 'EntityNotFoundError') {
|
|
|
+ return reply.code(404).send({ message: '消息不存在' })
|
|
|
+ }
|
|
|
+ return reply.code(500).send({ message: '更新消息失败', error: (error as Error).message })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async delete(request: FastifyRequest<{ Params: { id: number } }>, reply: FastifyReply) {
|
|
|
+ try {
|
|
|
+ const { id } = request.params
|
|
|
+
|
|
|
+ // 先检查消息是否存在
|
|
|
+ await this.messagesService.findById(id)
|
|
|
+
|
|
|
+ await this.messagesService.delete(id)
|
|
|
+
|
|
|
+ return reply.send({
|
|
|
+ message: '消息删除成功'
|
|
|
+ })
|
|
|
+ } catch (error) {
|
|
|
+ if ((error as Error).name === 'EntityNotFoundError') {
|
|
|
+ return reply.code(404).send({ message: '消息不存在' })
|
|
|
+ }
|
|
|
+ return reply.code(500).send({ message: '删除消息失败', error: (error as Error).message })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async getByUserId(request: FastifyRequest<{ Params: { userId: number } }>, reply: FastifyReply) {
|
|
|
+ try {
|
|
|
+ const { userId } = request.params
|
|
|
+
|
|
|
+ const messages = await this.messagesService.findByUserId(userId)
|
|
|
+
|
|
|
+ return reply.send({
|
|
|
+ message: '获取用户消息成功',
|
|
|
+ data: messages
|
|
|
+ })
|
|
|
+ } catch (error) {
|
|
|
+ return reply.code(500).send({ message: '获取用户消息失败', error: (error as Error).message })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async getByFishId(request: FastifyRequest<{ Params: { fishId: number } }>, reply: FastifyReply) {
|
|
|
+ try {
|
|
|
+ const { fishId } = request.params
|
|
|
+ } catch (error) {
|
|
|
+ return reply.code(500).send({ message: '获取鱼类消息失败', error: (error as Error).message })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async getByTargetId(request: FastifyRequest<{ Params: { targetId: number } }>, reply: FastifyReply) {
|
|
|
+ try {
|
|
|
+ const { targetId } = request.params
|
|
|
+
|
|
|
+ const messages = await this.messagesService.findByTargetId(targetId)
|
|
|
+
|
|
|
+ return reply.send({
|
|
|
+ message: '获取目标消息成功',
|
|
|
+ data: messages
|
|
|
+ })
|
|
|
+ } catch (error) {
|
|
|
+ return reply.code(500).send({ message: '获取目标消息失败', error: (error as Error).message })
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|