Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Implement Messaging API #30

Open
kakasoo opened this issue Sep 14, 2024 · 0 comments
Open

Proposal: Implement Messaging API #30

kakasoo opened this issue Sep 14, 2024 · 0 comments

Comments

@kakasoo
Copy link
Contributor

kakasoo commented Sep 14, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant