Skip to content

Commit

Permalink
Merge pull request #31 from recepkefelii/aws-s3
Browse files Browse the repository at this point in the history
Aws s3
  • Loading branch information
recepkefelii authored Mar 19, 2023
2 parents 9a93fb7 + 4f42ef9 commit c9a7e92
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 38 deletions.
1 change: 0 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { JwtModule } from '@nestjs/jwt';
import { MongooseModule, } from '@nestjs/mongoose';
import { AuthModule } from './auth/auth.module';
import { LikeModule } from './like/like.module';
Expand Down
2 changes: 0 additions & 2 deletions src/auth/change-password/change.password.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Module } from "@nestjs/common";
import { APP_GUARD } from "@nestjs/core";
import { JwtService } from "@nestjs/jwt";
import { MongooseModule } from "@nestjs/mongoose";
import { AuthGuard } from "src/common/guards/auth.guard";
import { MailModule } from "src/common/mail/mail.module";
import { User, UserSchema } from "../schema/user.schema";
import { ChangePasswordContreller } from "./change.password.controller";
Expand Down
4 changes: 3 additions & 1 deletion src/auth/change-password/change.password.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { User, UserDocument } from "../schema/user.schema";
import * as bcrypt from 'bcrypt';
import { UserdDto } from "src/users/dto/user.dto";
import { ChangePassWordDto } from "../dto/change.password.dto";
import { deflateRawSync } from "zlib";

@Injectable()
export class ChangePasswordService {
Expand Down Expand Up @@ -60,4 +61,5 @@ export class ChangePasswordService {
Logger.error('[MailService] Change Password: Send Mail Failed!', err);
}
}
}
}

32 changes: 19 additions & 13 deletions src/auth/schema/user.schema.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, SchemaTypes } from 'mongoose';
import { Document, Types } from 'mongoose';

export type UserDocument = User & Document;
export type UserDocument = Document & User;

@Schema()
export class User {
@Prop({ required: true, })
@Prop({ required: true })
name: string;

@Prop({ required: true, unique: true })
username: string
username: string;

@Prop({ required: true, unique: true })
email: string;

@Prop({unique: true})
@Prop({ unique: true })
profil_photo_url: string;

@Prop({unique: true})
@Prop({ unique: true })
banner_url: string;

@Prop({ required: true })
Expand All @@ -29,17 +29,23 @@ export class User {
@Prop({ default: Date.now })
updatedDate: Date;

@Prop({ type: [{ type: SchemaTypes.ObjectId, ref: 'User' }] })
following: User[];
@Prop({ type: [{ type: Types.ObjectId, ref: 'User' }] })
following: Types.ObjectId[];

@Prop({ type: [{ type: Types.ObjectId, ref: 'User' }] })
followers: Types.ObjectId[];

@Prop({ type: [{ type: SchemaTypes.ObjectId, ref: 'User' }] })
followers: User[];
@Prop({ default: 0 })
followingCount: number;

@Prop({ default: 0 })
followingCount: number
followersCount: number;

@Prop({ type: [{ type: Types.ObjectId, ref: 'Post' }] })
posts: Types.ObjectId[];

@Prop({ default: 0 })
followersCount: number
postsCount: number
}

export const UserSchema = SchemaFactory.createForClass(User);
export const UserSchema = SchemaFactory.createForClass(User);
2 changes: 2 additions & 0 deletions src/like/like.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class LikeService {
const newLike = new this.likeModel({ post: postId, user: user.id });
await newLike.save();
post.likes++;
post.isLiked = true
await post.save();

return post;
Expand All @@ -49,6 +50,7 @@ export class LikeService {

const post = await this.postModel.findById(postId);
post.likes -= 1;
post.isLiked = false
await post.save();

return post;
Expand Down
3 changes: 2 additions & 1 deletion src/post/create-post/create.post.module.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Module } from "@nestjs/common";
import { JwtService } from "@nestjs/jwt";
import { MongooseModule } from "@nestjs/mongoose";
import { User, UserSchema } from "src/auth/schema/user.schema";
import { AwsS3Module } from "src/aws-s3/aws-s3.module";
import { Post, PostSchema } from "../schemas/post.schema";
import { CreatePostController } from "./create.post.controller";
import { CreatePostService } from "./create.post.service";

@Module({
imports: [
MongooseModule.forFeature([{ name: Post.name, schema: PostSchema }]),
MongooseModule.forFeature([{ name: Post.name, schema: PostSchema }, { name: User.name, schema: UserSchema }]),
AwsS3Module
],
providers: [CreatePostService, JwtService],
Expand Down
13 changes: 10 additions & 3 deletions src/post/create-post/create.post.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,35 @@ import { Model } from "mongoose";
import { UserdDto } from "src/users/dto/user.dto";
import { CreatePostDto } from "./dto/create.post.dto";
import { AwsS3Service } from "src/aws-s3/aws-s3.service";
import { User, UserDocument } from "src/auth/schema/user.schema";

@Injectable()
export class CreatePostService {
constructor(
@InjectModel(Post.name) private postModel: Model<PostDocument>,
@InjectModel(User.name) private userModel: Model<UserDocument>,
private readonly awsS3Service: AwsS3Service
) { }

async createPost(file: Express.Multer.File, post: CreatePostDto, author: UserdDto): Promise<Post> {
// const user = await this.userModel.findById(author.id)
let photo_url = null
if (file) {
const uploadedFile = await this.awsS3Service.uploadFile(file.buffer, file.originalname);
photo_url = uploadedFile.url;
}

const createPost = await this.postModel.create({
content: post.content,
title: post.title,
author: author,
author: author.id,
photo_url: photo_url
});
const user = await this.userModel.findById(author.id);
user.postsCount++
user.posts.push(createPost._id);
await user.save();
return createPost;
}

}
7 changes: 5 additions & 2 deletions src/post/post.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { Controller, Get, Param } from '@nestjs/common';
import { Controller, Get, Param, UseGuards } from '@nestjs/common';
import { CurrentUser } from 'src/common/decorators/auth.decorator';
import { AuthGuard } from 'src/common/guards/auth.guard';
import { UserdDto } from 'src/users/dto/user.dto';
import { PostService } from './post.service';

@Controller('post')
Expand All @@ -10,7 +13,7 @@ export class PostController {
}

@Get(":id")
async getPostById(@Param('id') id: string){
async getPostById(@Param('id') id: string) {
return this.postService.getPostById(id)
}
}
6 changes: 3 additions & 3 deletions src/post/post.module.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Module } from "@nestjs/common";
import { CreatePostModule } from "./create-post/create.post.module";
import { UpdatePostController } from "./update-post/update.post.controller";
import { UpdatePostModule } from "./update-post/update.post.module";
import { PostService } from './post.service';
import { PostController } from './post.controller';
import { MongooseModule } from "@nestjs/mongoose";
import { Post, PostSchema } from "./schemas/post.schema";
import { JwtService } from "@nestjs/jwt";

@Module({
imports: [MongooseModule.forFeature([{ name: Post.name, schema: PostSchema }]),
CreatePostModule, UpdatePostModule],
providers: [PostService],
CreatePostModule, UpdatePostModule],
providers: [PostService, JwtService],
controllers: [PostController]
})
export class PostModule { }
8 changes: 5 additions & 3 deletions src/post/post.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { BadRequestException, HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { HttpErrorByCode } from '@nestjs/common/utils/http-error-by-code.util';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { UserdDto } from 'src/users/dto/user.dto';
import { Post, PostDocument } from './schemas/post.schema';

@Injectable()
export class PostService {
constructor(@InjectModel(Post.name) private postModel: Model<PostDocument>) { }

async getAllPost(): Promise<Post[]> {
try {
return await this.postModel.find()
Expand All @@ -15,11 +16,12 @@ export class PostService {
}
}

async getPostById(id:string): Promise<Post>{
async getPostById(id: string): Promise<Post> {
try {
return await this.postModel.findById(id)
} catch (error) {
throw new HttpException(`a post with this ${id} was not found`,HttpStatus.NOT_FOUND,)
throw new HttpException(`a post with this ${id} was not found`, HttpStatus.NOT_FOUND,)
}
}

}
5 changes: 4 additions & 1 deletion src/post/schemas/post.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class Post {
@Prop({ required: true })
content: string;

@Prop({ type: Types.ObjectId, ref: "User", required: true })
@Prop({ type: Types.ObjectId, ref: 'User', required: true })
author: Types.ObjectId;

@Prop({ default: Date.now })
Expand All @@ -25,6 +25,9 @@ export class Post {

@Prop({ default: 0 })
likes: number;

@Prop()
isLiked: boolean
}

export const PostSchema = SchemaFactory.createForClass(Post);
2 changes: 0 additions & 2 deletions src/users/photo-upload/photo.upload.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Controller, Post, UploadedFile, UseGuards, UseInterceptors } from "@nestjs/common";
import { FileInterceptor } from "@nestjs/platform-express";
import { diskStorage } from "multer";
import { extname } from "path";
import { CurrentUser } from "src/common/decorators/auth.decorator";
import { AuthGuard } from "src/common/guards/auth.guard";
import { UserdDto } from "../dto/user.dto";
Expand Down
2 changes: 0 additions & 2 deletions src/users/photo-upload/photo.upload.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Module } from "@nestjs/common";
import { JwtService } from "@nestjs/jwt";
import { MongooseModule } from "@nestjs/mongoose";
import { MulterModule } from "@nestjs/platform-express";
import { User, UserSchema } from "src/auth/schema/user.schema";
import { AwsS3Module } from "src/aws-s3/aws-s3.module";
import { PhotoUploadController } from "./photo.upload.controller";
Expand All @@ -10,7 +9,6 @@ import { PhotoUploadService } from "./photo.upload.service";
@Module({
imports: [
MongooseModule.forFeature([{ name: User.name, schema: UserSchema }]),
MulterModule.register({ dest: './upload' }),
AwsS3Module
],
controllers: [PhotoUploadController],
Expand Down
5 changes: 3 additions & 2 deletions src/users/users.module.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { Module } from '@nestjs/common';
import { Module, Post } from '@nestjs/common';
import { UsersService } from './users.service';
import { UsersController } from './users.controller';
import { JwtService } from '@nestjs/jwt';
import { MongooseModule } from '@nestjs/mongoose';
import { User, UserSchema } from 'src/auth/schema/user.schema';
import { PhotoUploadModule } from './photo-upload/photo.upload.module';
import { UploadBannerModule } from './banner-upload/upload.banner.module';
import { PostSchema } from 'src/post/schemas/post.schema';

@Module({
imports: [
MongooseModule.forFeature([{ name: User.name, schema: UserSchema }]),
MongooseModule.forFeature([{ name: User.name, schema: UserSchema }, { name: Post.name, schema: PostSchema }]),
PhotoUploadModule,
UploadBannerModule
],
Expand Down
10 changes: 8 additions & 2 deletions src/users/users.service.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import { Injectable } from '@nestjs/common';
import { Injectable, NotFoundException, Post } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { User, UserDocument } from 'src/auth/schema/user.schema';
import { PostDocument } from 'src/post/schemas/post.schema';
import { UploadBannerService } from './banner-upload/upload.banner.service';
import { UserdDto } from './dto/user.dto';

@Injectable()
export class UsersService {
constructor(
@InjectModel(User.name) private userModel: Model<UserDocument>,
@InjectModel(Post.name) private postModel: Model<PostDocument>,
private readonly uploadBannerServive: UploadBannerService
) {

}
async userProfile(user: UserdDto) {
return await this.userModel.findById(user.id).select("-password")
const profile = await this.userModel
.findOne({ id: user.id })
.select("-password")
.populate("posts");
return profile;
}
}

0 comments on commit c9a7e92

Please sign in to comment.