\update: the slugify method
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { Column, DeleteDateColumn, Entity, ManyToOne, OneToMany } from "typeorm";
|
||||
import { Column, DeleteDateColumn, Entity, Index, ManyToOne, OneToMany } from "typeorm";
|
||||
|
||||
import { BlogCategory } from "./blog-category.entity";
|
||||
import { BlogComment } from "./blog-comment.entity";
|
||||
@@ -8,9 +8,11 @@ import { User } from "../../users/entities/user.entity";
|
||||
@Entity()
|
||||
export class Blog extends BaseEntity {
|
||||
@Column({ type: "varchar", length: 250, unique: true })
|
||||
@Index()
|
||||
title: string;
|
||||
|
||||
@Column({ type: "varchar", length: 250, unique: true })
|
||||
@Index()
|
||||
slug: string;
|
||||
|
||||
@Column({ type: "text" })
|
||||
@@ -32,18 +34,23 @@ export class Blog extends BaseEntity {
|
||||
tags: string[];
|
||||
|
||||
@Column({ type: "boolean", default: true })
|
||||
@Index()
|
||||
isActive: boolean;
|
||||
|
||||
@Column({ type: "int", default: 0 })
|
||||
@Index()
|
||||
viewCount: number;
|
||||
|
||||
@Column({ type: "boolean", default: false })
|
||||
@Index()
|
||||
isPinned: boolean;
|
||||
|
||||
@ManyToOne(() => User, (user) => user.blogs, { onDelete: "CASCADE", nullable: false })
|
||||
@Index()
|
||||
author: User;
|
||||
|
||||
@ManyToOne(() => BlogCategory, (category) => category.blogs, { nullable: false })
|
||||
@Index()
|
||||
category: BlogCategory;
|
||||
|
||||
@OneToMany(() => BlogComment, (comment) => comment.blog)
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
import slugify from "slugify";
|
||||
import { Not } from "typeorm";
|
||||
|
||||
import { BlogMessage, CommonMessage } from "../../../common/enums/message.enum";
|
||||
@@ -106,12 +105,13 @@ export class BlogsService {
|
||||
const category = await this.findCategoryById(createBlogDto.categoryId);
|
||||
|
||||
const slug = this.generateSlug(createBlogDto.slug);
|
||||
console.log(slug);
|
||||
|
||||
const blog = this.blogsRepository.create({
|
||||
...createBlogDto,
|
||||
slug,
|
||||
author: { id: authorId },
|
||||
category,
|
||||
slug,
|
||||
});
|
||||
|
||||
await this.blogsRepository.save(blog);
|
||||
@@ -140,10 +140,13 @@ export class BlogsService {
|
||||
if (updateBlogDto.slug) {
|
||||
const existBlog = await this.blogsRepository.findOne({ where: { slug: updateBlogDto.slug, id: Not(id) } });
|
||||
if (existBlog) throw new BadRequestException(BlogMessage.BLOG_ALREADY_EXISTS);
|
||||
blog.slug = this.generateSlug(updateBlogDto.slug);
|
||||
}
|
||||
|
||||
await this.blogsRepository.save({ ...blog, ...updateBlogDto });
|
||||
await this.blogsRepository.save({
|
||||
...blog,
|
||||
...updateBlogDto,
|
||||
slug: this.generateSlug(updateBlogDto.slug ?? blog.slug),
|
||||
});
|
||||
|
||||
return {
|
||||
message: CommonMessage.UPDATE_SUCCESS,
|
||||
@@ -258,8 +261,16 @@ export class BlogsService {
|
||||
}
|
||||
|
||||
//*********************************** */
|
||||
private generateSlug(title: string): string {
|
||||
return slugify(title, { lower: true, strict: true });
|
||||
private generateSlug(slug: string): string {
|
||||
if (!slug) return "";
|
||||
|
||||
// Replace spaces and special characters with hyphens, preserve Farsi characters
|
||||
return slug
|
||||
.trim()
|
||||
.replace(/[&\\#,+()$~%.'":*?<>{}]/g, "") // Remove special characters
|
||||
.replace(/\s+/g, "-") // Replace spaces with hyphens
|
||||
.replace(/-+/g, "-") // Replace multiple hyphens with single hyphen
|
||||
.trim();
|
||||
}
|
||||
|
||||
//*********************************** */
|
||||
|
||||
@@ -122,7 +122,7 @@ export class DanakServicesRepository extends Repository<DanakService> {
|
||||
// "averageRating",
|
||||
// )
|
||||
.andWhere("service.id = :serviceId", { serviceId })
|
||||
.groupBy("service.id,category.id, images.id, subscriptionPlans.id, reviews.id, user.id");
|
||||
.groupBy("service.id,category.id, images.id, subscriptionPlans.id, reviews.id, user.id, directDiscount.id");
|
||||
|
||||
if (!isAdmin) {
|
||||
serviceQueryBuilder
|
||||
|
||||
Reference in New Issue
Block a user