Skip to content

Commit

Permalink
feat: add users service
Browse files Browse the repository at this point in the history
  • Loading branch information
recepkefelii committed Mar 13, 2023
1 parent 8a077ef commit d90e44b
Show file tree
Hide file tree
Showing 6 changed files with 5,523 additions and 22 deletions.
4 changes: 1 addition & 3 deletions src/auth/change-password/change.password.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import { ChangePasswordService } from "./change.password.service";
export class ChangePasswordContreller {
constructor(private readonly changePasswordService: ChangePasswordService) { }
@Post('change-password')
async changePassword(@Body() password: ChangePassWordDto, @CurrentUser() user: UserdDto) {
console.log(user);

async changePassword(@Body() password: ChangePassWordDto, @CurrentUser() user: UserdDto) {
try {
await this.changePasswordService.changePassword(password, user);

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

export type UserDocument = User & Document;

export type UserDocument = Document & User;
@Schema()
export class User {

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

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

@Prop({ required: true })
password: string;

@Prop({ default: Date.now })
createdDate: Date;

@Prop({ default: Date.now })
updatedDate: Date;

@Prop({ required: true, unique: true })
email: string;
@Prop({ type: [{ type: SchemaTypes.ObjectId, ref: 'User' }] })
following: User[];

@Prop({ required: true })
password: string;
@Prop({ type: [{ type: SchemaTypes.ObjectId, ref: 'User' }] })
followers: User[];

@Prop({ default: Date.now })
createdDate: Date;
@Prop({default: 0})
followingCount: number

@Prop({ default: Date.now })
updatedDate: Date;
@Prop({default: 0})
followersCount: number
}

export const UserSchema = SchemaFactory.createForClass(User);
11 changes: 7 additions & 4 deletions src/users/users.controller.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Controller, Get, UseGuards } from '@nestjs/common';
import { CurrentUser } from 'src/common/decorators/auth.decorator';
import { AuthGuard } from 'src/common/guards/auth.guard';
import { UserdDto } from './dto/user.dto';
import { UsersService } from './users.service';

@UseGuards(AuthGuard)
@Controller('users')
export class UsersController {
@UseGuards(AuthGuard)
constructor(private readonly userService: UsersService) { }
@Get('profile')
async getUser() {
console.log("hello world");

async userProfile(@CurrentUser() user: UserdDto) {
return await this.userService.userProfile(user)
}

}
5 changes: 4 additions & 1 deletion src/users/users.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import { Module } 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';

@Module({
providers: [UsersService,JwtService
imports: [MongooseModule.forFeature([{ name: User.name, schema: UserSchema }])],
providers: [UsersService, JwtService
],
controllers: [UsersController]
})
Expand Down
13 changes: 12 additions & 1 deletion src/users/users.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { User, UserDocument } from 'src/auth/schema/user.schema';
import { UserdDto } from './dto/user.dto';

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

}
async userProfile(user: UserdDto) {
return await this.userModel.findById(user.id).select("-password")
}
}
Loading

0 comments on commit d90e44b

Please sign in to comment.