Skip to content

Commit

Permalink
Merge pull request #100 from toomio-official/feature/queue
Browse files Browse the repository at this point in the history
Implemented the module for sqs creation
  • Loading branch information
pasandevin authored Jul 28, 2024
2 parents 01e6473 + 4683589 commit 0881d06
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 43 deletions.
6 changes: 3 additions & 3 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: 2
updates:
- package-ecosystem: "npm"
directory: "/"
- package-ecosystem: 'npm'
directory: '/'
schedule:
interval: "weekly"
interval: 'weekly'
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"singleQuote": true,
"trailingComma": "all"
}
}
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { SMPostsModule } from './posts/smPosts.module';
import { LikesModule } from './likes/likes.module';
import { UsersModule } from './auth/users/users.module';
import { CommentsModule } from './comments/comments.module';
import { AwsSqsModule } from './aws-sqs/aws-sqs.module';

@Module({
imports: [
Expand All @@ -24,6 +25,7 @@ import { CommentsModule } from './comments/comments.module';
LikesModule,
UsersModule,
CommentsModule,
AwsSqsModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
2 changes: 1 addition & 1 deletion src/auth/users/dto/userCreate.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IsEmail, IsNotEmpty } from 'class-validator';
import mongoose from "mongoose";
import mongoose from 'mongoose';

export class UserCreateDto {
_id: mongoose.Types.ObjectId;
Expand Down
16 changes: 8 additions & 8 deletions src/auth/users/dto/userFollow.dto.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { IsEmail, IsNotEmpty } from "class-validator";
import { IsEmail, IsNotEmpty } from 'class-validator';

export class UserFollowDto {
@IsNotEmpty()
@IsEmail()
followingUserEmail: string;
@IsNotEmpty()
@IsEmail()
followerUserEmail: string;
}
@IsNotEmpty()
@IsEmail()
followingUserEmail: string;
@IsNotEmpty()
@IsEmail()
followerUserEmail: string;
}
12 changes: 8 additions & 4 deletions src/auth/users/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { UserCreateDto } from './dto/userCreate.dto';

@Injectable()
export class UserRepository {
constructor(@InjectModel(User.name) private userModel: Model<UserDocument>) { }
constructor(@InjectModel(User.name) private userModel: Model<UserDocument>) {}

async createUser(userCreateDto: UserCreateDto): Promise<User> {
return await new this.userModel(userCreateDto).save();
Expand All @@ -19,8 +19,12 @@ export class UserRepository {
}

async updateUser(followerUser: User): Promise<User> {
return await this.userModel.findByIdAndUpdate(followerUser._id, followerUser, {
new: true,
});
return await this.userModel.findByIdAndUpdate(
followerUser._id,
followerUser,
{
new: true,
},
);
}
}
21 changes: 14 additions & 7 deletions src/auth/users/users.controller.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { Controller, Post, UsePipes, Put, ValidationPipe, Body } from '@nestjs/common';
import {
Controller,
Post,
UsePipes,
Put,
ValidationPipe,
Body,
} from '@nestjs/common';
import { UsersService } from './users.service';
import { UserFollowDto } from './dto/userFollow.dto';
import { User } from './user.schema';

@Controller('/users')
export class UsersController {
constructor(private userService: UsersService) { }
constructor(private userService: UsersService) {}

@Put('/follow')
@UsePipes(ValidationPipe)
async FollowUser(@Body() userFollowDto: UserFollowDto): Promise<User> {
return await this.userService.FollowUser(userFollowDto);
}
@Put('/follow')
@UsePipes(ValidationPipe)
async FollowUser(@Body() userFollowDto: UserFollowDto): Promise<User> {
return await this.userService.FollowUser(userFollowDto);
}
}
48 changes: 29 additions & 19 deletions src/auth/users/users.service.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
import { ConflictException, Injectable, NotFoundException } from '@nestjs/common';
import {
ConflictException,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { UserFollowDto } from './dto/userFollow.dto';
import { User } from './user.schema';
import { UserRepository } from './user.repository';

@Injectable()
export class UsersService {
constructor(private userRepository: UserRepository) { }
constructor(private userRepository: UserRepository) {}

async FollowUser(userFollowDto: UserFollowDto): Promise<User> {
const followerUserEmail = userFollowDto.followerUserEmail;
const followingUserEmail = userFollowDto.followingUserEmail;
async FollowUser(userFollowDto: UserFollowDto): Promise<User> {
const followerUserEmail = userFollowDto.followerUserEmail;
const followingUserEmail = userFollowDto.followingUserEmail;

const followerUser = await this.userRepository.findAUser(followerUserEmail);
const followingUser = await this.userRepository.findAUser(followingUserEmail);
const followerUser = await this.userRepository.findAUser(followerUserEmail);
const followingUser = await this.userRepository.findAUser(
followingUserEmail,
);

if (!followerUser) {
throw new NotFoundException('Follower User Not Found');
}
if (!followingUser) {
throw new NotFoundException('Following User Not Found');
}
if (!followerUser) {
throw new NotFoundException('Follower User Not Found');
}
if (!followingUser) {
throw new NotFoundException('Following User Not Found');
}

if (followerUser.following.find(user => user._id.toString() === followingUser._id.toString())) {
throw new ConflictException('User Already Followed');
}
followerUser.following.push(followingUser);
return await this.userRepository.updateUser(followerUser);
if (
followerUser.following.find(
(user) => user._id.toString() === followingUser._id.toString(),
)
) {
throw new ConflictException('User Already Followed');
}

followerUser.following.push(followingUser);
return await this.userRepository.updateUser(followerUser);
}
}
7 changes: 7 additions & 0 deletions src/aws-sqs/aws-sqs.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Module } from '@nestjs/common';
import { AwsSqsService } from './aws-sqs.service';

@Module({
providers: [AwsSqsService],
})
export class AwsSqsModule {}
29 changes: 29 additions & 0 deletions src/aws-sqs/aws-sqs.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Injectable } from '@nestjs/common';
import { CreateQueueCommand, SQSClient } from '@aws-sdk/client-sqs';

@Injectable()
export class AwsSqsService {
private readonly client: SQSClient;

constructor() {
this.client = new SQSClient({});
}

async createQueue(sqsQueueName: string) {
const command = new CreateQueueCommand({
QueueName: sqsQueueName,
Attributes: {
DelaySeconds: '60',
MessageRetentionPeriod: '86400',
},
});

try {
const response = await this.client.send(command);
return response;
} catch (error) {
console.error(error);
throw error;
}
}
}
2 changes: 2 additions & 0 deletions src/posts/smPosts.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { UserRepository } from 'src/auth/users/user.repository';
import { CommentsService } from 'src/comments/comments.service';
import { CommentRepository } from 'src/comments/comment.repository';
import { Comment, CommentSchema } from 'src/comments/comment.schema';
import { AwsSqsService } from 'src/aws-sqs/aws-sqs.service';

@Module({
imports: [
Expand All @@ -29,6 +30,7 @@ import { Comment, CommentSchema } from 'src/comments/comment.schema';
UserRepository,
CommentsService,
CommentRepository,
AwsSqsService
],
})
export class SMPostsModule {}

0 comments on commit 0881d06

Please sign in to comment.