Skip to content

Proposal: Implement Messaging API #30

Description

@kakasoo

Proposal for Messaging Feature

Overview

Implement an API for messaging between users. This feature will allow users to send and receive private messages within the platform.

Controller Implementation

The MessagesController will handle HTTP requests related to messages, providing endpoints to send and retrieve messages.

Service Implementation

The MessagesService will contain the business logic for sending and retrieving messages from the database.

DTOs and Models

  • CreateMessageDto: Defines the data structure for creating a new message.

Example Code

Controller

import { Controller, Post, Body, Get, Param, UseGuards } from '@nestjs/common';
import { JwtGuard } from '../auth/guards/jwt.guard';
import { MessagesService } from '../providers/messages.service';
import { CreateMessageDto } from '../models/dtos/create-message.dto';

@UseGuards(JwtGuard)
@Controller('api/v1/messages')
export class MessagesController {
  constructor(private readonly messagesService: MessagesService) {}

  @Post()
  async sendMessage(@Body() createMessageDto: CreateMessageDto) {
    return this.messagesService.sendMessage(createMessageDto);
  }

  @Get(':userId')
  async getMessages(@Param('userId') userId: number) {
    return this.messagesService.getMessages(userId);
  }
}

Service

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { MessagesRepository } from '../models/repositories/messages.repository';
import { CreateMessageDto } from '../models/dtos/create-message.dto';

@Injectable()
export class MessagesService {
  constructor(
    @InjectRepository(MessagesRepository)
    private readonly messagesRepository: MessagesRepository,
  ) {}

  async sendMessage(createMessageDto: CreateMessageDto) {
    const message = this.messagesRepository.create(createMessageDto);
    return this.messagesRepository.save(message);
  }

  async getMessages(userId: number) {
    return this.messagesRepository.find({ where: { recipientId: userId } });
  }
}

DTO

export class CreateMessageDto {
  senderId: number;
  recipientId: number;
  content: string;
}

Conclusion

This proposal outlines the basic implementation for a messaging feature. Further enhancements could include real-time messaging and read receipts.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions