Commit df70e6f0 authored by soheib's avatar soheib
Browse files

add lottory results

parent 866f7637
...@@ -4,37 +4,48 @@ import { GetUserScore } from './dto/get-user-score'; ...@@ -4,37 +4,48 @@ import { GetUserScore } from './dto/get-user-score';
@Controller() @Controller()
export class AppController { export class AppController {
constructor(private readonly appService: AppService) {} constructor(private readonly appService: AppService) { }
@Get('get-comments') @Get('get-comments')
async getCommentsFromIG() { async getCommentsFromIG() {
return await this.appService.getComments(); return await this.appService.getComments();
} }
@Get('get-followers') @Get('get-followers')
async getFollowers() { async getFollowers() {
return await this.appService.getFollowers(); return await this.appService.getFollowers();
} }
@Post() @Post()
async getUserScore(@Body() getUserScoreDto: GetUserScore) { async getUserScore(@Body() getUserScoreDto: GetUserScore) {
return await this.appService.calculateUserScore(getUserScoreDto.username); return await this.appService.calculateUserScore(getUserScoreDto.username);
} }
@Get('calculate-result') @Get('calculate-result')
async calculateResult() { async calculateResult() {
return await this.appService.getResults() return await this.appService.getResults()
} }
@Get('get-results') @Get('get-results')
async getResults() { async getResults() {
return await this.appService.getFinalResults(); return await this.appService.getFinalResults();
} }
@Post('search') @Post('search')
async getUserResults(@Body('username') username: string) { async getUserResults(@Body('username') username: string) {
return await this.appService.getUserResult(username); return await this.appService.getUserResult(username);
} }
@Get('shuffle')
async shuffle() {
return await this.appService.getShuffleData()
}
@Get('add-lottory-result')
async addResultDb() {
return await this.appService.addResultsToDB()
}
} }
...@@ -4,6 +4,7 @@ import { AccountFollowersSchema } from './account.followers'; ...@@ -4,6 +4,7 @@ import { AccountFollowersSchema } from './account.followers';
import { AppController } from './app.controller'; import { AppController } from './app.controller';
import { AppService } from './app.service'; import { AppService } from './app.service';
import { CommentSchema } from './comment.schema'; import { CommentSchema } from './comment.schema';
import { LottoryResultSchema } from './comptition.schema';
import { RequestSchema } from './request.schema'; import { RequestSchema } from './request.schema';
import { ResultSchema } from './result.schema'; import { ResultSchema } from './result.schema';
...@@ -13,6 +14,7 @@ import { ResultSchema } from './result.schema'; ...@@ -13,6 +14,7 @@ import { ResultSchema } from './result.schema';
MongooseModule.forFeature([{ name: 'Request', schema: RequestSchema }]), MongooseModule.forFeature([{ name: 'Request', schema: RequestSchema }]),
MongooseModule.forFeature([{ name: 'Comment', schema: CommentSchema }]), MongooseModule.forFeature([{ name: 'Comment', schema: CommentSchema }]),
MongooseModule.forFeature([{ name: 'Result', schema: ResultSchema }]), MongooseModule.forFeature([{ name: 'Result', schema: ResultSchema }]),
MongooseModule.forFeature([{ name: 'LottoryResult', schema: LottoryResultSchema }]),
MongooseModule.forFeature([{ name: 'AccountFollower', schema: AccountFollowersSchema }]), MongooseModule.forFeature([{ name: 'AccountFollower', schema: AccountFollowersSchema }]),
], ],
controllers: [AppController], controllers: [AppController],
......
...@@ -10,10 +10,10 @@ const { username, password } = process.env ...@@ -10,10 +10,10 @@ const { username, password } = process.env
import * as _ from "lodash" import * as _ from "lodash"
import FollowerPrivateData from './followers_data'; import FollowerPrivateData from './followers_data';
import { CleanedComments, MentionDocument } from './interface/IcleandComment'; import { CleanedComments, MentionDocument } from './interface/IcleandComment';
import { AccountFollowers, AccountFollowersDocument } from './account.followers'; import { AccountFollowersDocument } from './account.followers';
import { CommentStatus, UserAllMention } from './interface/UserAllMentions'; import { CommentStatus, UserAllMention } from './interface/UserAllMentions';
import { json } from 'express';
import { ResultDocument } from './result.schema'; import { ResultDocument } from './result.schema';
import { LottoryResultDocument } from './comptition.schema';
@Injectable() @Injectable()
export class AppService implements OnApplicationBootstrap { export class AppService implements OnApplicationBootstrap {
...@@ -26,7 +26,9 @@ export class AppService implements OnApplicationBootstrap { ...@@ -26,7 +26,9 @@ export class AppService implements OnApplicationBootstrap {
@InjectModel('AccountFollower') @InjectModel('AccountFollower')
private followerModel: Model<AccountFollowersDocument>, private followerModel: Model<AccountFollowersDocument>,
@InjectModel('Result') @InjectModel('Result')
private resultModel: Model<ResultDocument> private resultModel: Model<ResultDocument>,
@InjectModel('LottoryResult')
private lotoryResultModel: Model<LottoryResultDocument>
) { ) {
} }
...@@ -555,6 +557,62 @@ export class AppService implements OnApplicationBootstrap { ...@@ -555,6 +557,62 @@ export class AppService implements OnApplicationBootstrap {
} }
} }
shuffle(array) {
var currentIndex = array.length, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
return array;
}
async getShuffleData() {
let comptitionArray = new Array<string>()
let foundUsernames = await this.resultModel.find()
console.log(foundUsernames);
let score = 0
for await (const user of foundUsernames) {
score += user.score
for (let index = 0; index < user.score; index++) {
comptitionArray.push(user.username)
}
}
let res = this.shuffle(comptitionArray)
console.log("score is",score);
return res
}
async addResultsToDB(){
await this.lotoryResultModel.deleteMany()
let comptitionArray = new Array<any>()
let foundUsernames = await this.resultModel.find().sort({ score: -1 })
console.log(foundUsernames);
let index = 1
for await (const user of foundUsernames) {
for (let u = 0; u < user.score; u++) {
comptitionArray.push({username: user.username, index})
index++
}
}
await this.lotoryResultModel.insertMany(comptitionArray)
return "successfull"
}
} }
export class ResultResponse { export class ResultResponse {
......
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
import { Document, Types } from 'mongoose'
export type LottoryResultDocument = LottoryResult & Document
@Schema({timestamps:true})
export class LottoryResult {
@Prop()
username: string
@Prop()
index: number
}
export const LottoryResultSchema = SchemaFactory.createForClass(LottoryResult)
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