Commit 3b77a902 authored by soheib's avatar soheib
Browse files

add weakly lottory code

parent c017535c
export default {
addToStoryData : [
{
username: "soma.ye2103",
count: 5
},
{
username: "zahra_51512",
count: 1
},
{
username: "nafas3633m",
count: 1
},
{
username: "jafar.amr",
count: 1
},
{
username: "sougool5382",
count: 1
},
{
username: "maryam.maryammy",
count: 1
},
{
username: "toktam.yobi80",
count: 1
},
{
username: "roghayeh_b8082",
count: 1
},
{
username: "turky.yyy",
count: 1
},
{
username: "liligoli",
count: 1
},
{
username: "soh.ila50",
count: 1
},
{
username: "_mrs_kz_z",
count: 1
},
]
}
\ No newline at end of file
......@@ -8,7 +8,12 @@ export class AppController {
@Post('get-score')
async getUserResults(@Body('username') username: string) {
return await this.appService.getUserLikes(username);
return await this.appService.getUserScore(username);
}
@Get('calculate-score')
async getFollowers() {
return await this.appService.addResultsToDB();
}
}
......
......@@ -5,11 +5,12 @@ import { AppController } from './app.controller';
import { AppService } from './app.service';
import { LikeSchema } from './models/like.schema';
import { CommentSchema } from './models/comment.schema';
import { LottoryResultSchema } from './models/LottoryResult.schema';
@Module({
imports: [
MongooseModule.forRoot(
'mongodb://azadi:azadi%404010@185.231.180.248:27017/?serverSelectionTimeoutMS=5000&connectTimeoutMS=10000&authSource=admin&authMechanism=SCRAM-SHA-256',
'mongodb://netware:Netware%40408009@185.231.180.248:27017/instagram-lottry?serverSelectionTimeoutMS=5000&connectTimeoutMS=10000&authSource=admin',
),
MongooseModule.forFeature([
{ name: 'Follower', schema: FollowerSchema },
......@@ -20,6 +21,9 @@ import { CommentSchema } from './models/comment.schema';
MongooseModule.forFeature([
{ name: 'Comment', schema: CommentSchema },
]),
MongooseModule.forFeature([
{ name: 'LottryResult', schema: LottoryResultSchema },
]),
],
controllers: [AppController],
providers: [AppService],
......
......@@ -18,8 +18,10 @@ import FollowerPrivateData from './followers_data';
import { CleanedComments, MentionDocument } from './interface/IcleandComment';
import { CommentStatus, UserAllMention } from './interface/UserAllMentions';
import { ResultDocument } from './result.schema';
import { LottoryResultDocument } from './LottoryResult.schema';
import { LottoryResultDocument } from './models/LottoryResult.schema';
import { FollowerDocument } from './models/follower.schema';
import { LikeDocument } from './models/like.schema';
import addToStoryData from './add-to-story-data'
@Injectable()
export class AppService implements OnApplicationBootstrap {
......@@ -29,27 +31,128 @@ export class AppService implements OnApplicationBootstrap {
@InjectModel('Follower')
private followerModel: Model<FollowerDocument>,
@InjectModel('Like')
private likeModel: Model<FollowerDocument>,
private likeModel: Model<LikeDocument>,
@InjectModel('Comment')
private commentModel: Model<FollowerDocument>,
private commentModel: Model<CommentDocument>,
@InjectModel('LottryResult')
private lottryResultModel: Model<LottoryResultDocument>,
) {}
) { }
async onApplicationBootstrap() {
// this.client = await this.login('shahriarvelayat', 'shve8864@@');
this.client = null;
}
async getUserLikes(username:string){
let foundLikes = await this.likeModel.find({username})
console.log(foundLikes);
async getUserLikesScore(username: string) {
// let foundFollower = await this.followerModel.findOne({ username })
// if (foundFollower) {
let foundLikes = await this.likeModel.find({ username })
return foundLikes.length
// }
// else {
// throw new HttpException(`you are not a follower of this page`, 404)
// }
}
async getUserCommentsScore(username: string) {
let foundUserPosts = await this.commentModel.distinct('post_short_code')
let userCommentScore = new Array<{ post: string, post_score: number }>()
foundUserPosts.forEach((post) => {
userCommentScore.push({
post,
post_score: 0
})
})
let foundUserComments = await this.commentModel.find({ owner_username: username })
for await (const comment of foundUserComments) {
userCommentScore.forEach(element => {
if (comment.post_short_code === element.post && element.post_score < 3) {
element.post_score++
}
})
}
let score = 0
userCommentScore.forEach(element => {
score += element.post_score
})
return score
}
async getTagsScore(username: string) {
let addToStoryScore = 0
let foundUser = addToStoryData.addToStoryData.find((user) => {
return user.username === username
})
if (foundUser) {
addToStoryScore = foundUser.count
}
return addToStoryScore
}
async calculateUserScore(username: string) {
let likesScore = await this.getUserLikesScore(username)
let commentScore = await this.getUserCommentsScore(username)
let addToStoryScore = await this.getTagsScore(username)
return likesScore + commentScore + addToStoryScore
}
async getUserScore(username: string) {
let likesScore = await this.getUserLikesScore(username)
let commentScore = await this.getUserCommentsScore(username)
let addToStoryScore = await this.getTagsScore(username)
return {likesScore , commentScore , addToStoryScore, totalScore:likesScore + commentScore + addToStoryScore }
}
async getUserComments(username:string){
let foundComments = await this.commentModel.find({username})
console.log(foundComments);
return foundComments.length
async addResultsToDB() {
await this.lottryResultModel.deleteMany({})
let foundUsernameInComment = await this.commentModel.distinct('owner_username')
let foundUsernameInLike = await this.likeModel.distinct('username')
let foundUsernames = new Array<string>()
foundUsernameInComment.forEach((username) => {
if (!foundUsernames.includes(username)) {
foundUsernames.push(username)
}
})
foundUsernameInLike.forEach((username) => {
if (!foundUsernames.includes(username)) {
foundUsernames.push(username)
}
})
console.log(foundUsernames);
const comptitionArray = new Array<any>();
let index = 1000;
let totalItems = foundUsernames.length
let count = 0
for await (const username of foundUsernames) {
let userScore = await this.calculateUserScore(username)
for (let u = 0; u < userScore; u++) {
comptitionArray.push({
index,
username: username,
})
index++;
console.log(`username:${username} , index: ${index}, score:${userScore}`);
}
console.log(`user index:${count} , total items: ${totalItems}`);
count++
}
await this.lottryResultModel.insertMany(comptitionArray);
return 'successfull';
}
}
......
......@@ -9,8 +9,8 @@ export class LottoryResult {
index: number;
@Prop()
username: string;
@Prop()
tagged_user: string;
// @Prop()
// tagged_user: string;
@Prop()
status: string;
}
......
......@@ -23,6 +23,9 @@ export class Comment {
@Prop()
date: number;
@Prop()
post_short_code: string;
@Prop({ type: Object })
comment_object: Object;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment