Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 0 additions & 37 deletions server/src/shared/Entities/categories.entity.ts

This file was deleted.

34 changes: 34 additions & 0 deletions server/src/shared/Entities/tags.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Other dependencies
import {
Column,
Entity,
ObjectID,
ObjectIdColumn,
CreateDateColumn,
UpdateDateColumn,
} from 'typeorm'

@Entity('Tags')
export class TagsEntity {
constructor(partial: Partial<TagsEntity>) {
Object.assign(this, partial)
}

@ObjectIdColumn()
_id: ObjectID

@Column({ type: 'string' })
name: string

@Column({ type: 'int' })
total_title: number

@Column({ type: 'double' })
popularity_ratio: number

@CreateDateColumn({ type: 'date' })
created_at: Date

@UpdateDateColumn({ type: 'date' })
updated_at: Date
}
5 changes: 1 addition & 4 deletions server/src/shared/Entities/titles.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ export class TitlesEntity {
@ObjectIdColumn()
id: ObjectID

@Column({ type: 'string' })
category_id: string

@Column({ type: 'array' })
category_ancestors: string[]
tags: string[]

@Column({
unique: true,
Expand Down
143 changes: 0 additions & 143 deletions server/src/shared/Repositories/categories.repository.ts

This file was deleted.

89 changes: 89 additions & 0 deletions server/src/shared/Repositories/tags.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Other dependencies
import { Repository, EntityRepository } from 'typeorm'

// Local files
import { TagsEntity } from '../Entities/tags.entity'

@EntityRepository(TagsEntity)
export class TagsRepository extends Repository<TagsEntity> {

async getTrendingTags(): Promise<{ tags: TagsEntity[], count: number }> {
const [tags, total] = await this.findAndCount({
where: {
updated_at: {
// Last 24 hours
$gte: new Date(new Date().setDate(new Date().getDate() - 1))
}
},
order: {
popularity_ratio: 'DESC',
updated_at: 'DESC'
},
take: 30,
skip: 0,
})

return { tags, count: total }
}

async searchTag(searchValue: string): Promise<{ tags: TagsEntity[] }> {
const [tags] = await this.findAndCount({
where: {
name: new RegExp(searchValue, 'i')
},
take: 25,
order: {
popularity_ratio: 'DESC'
}
})

return { tags }
}

async tagActionOnTitleCreateOrUpdate(tagName: string): Promise<void> {
const tag = await this.findOne({ name: tagName })

if (!tag) {
this.save({
name: tagName,
popularity_ratio: 0.01,
total_title: 1
})
}

else {
// @ts-ignore
const diff = new Date().getTime() - tag.updated_at
const diffAsMin = Math.round((diff / 1000) / 60)

if (diffAsMin <= 1440 && tag.popularity_ratio < 1.00) tag.popularity_ratio += 0.01
else tag.popularity_ratio = 0.02

tag.total_title++
this.save(tag)
}

return
}

async updateTagWhenRemovedFromTitle(tagName: string): Promise<void> {
const tag = await this.findOne({ name: tagName })
if (!tag) return

if (tag.total_title > 1) {
tag.total_title--
if (tag.popularity_ratio > 0.01) (tag.popularity_ratio -= 0.01).toFixed(1)
tag.updated_at = tag.updated_at
this.save(tag)
}
else this.deleteTag(tag)

return
}

async deleteTag(tag: TagsEntity): Promise<void> {
await this.delete(tag)
return
}

}
Loading