mirror of
https://github.com/yangshun/tech-interview-handbook.git
synced 2025-07-28 20:52:00 +08:00
[offers][refactor] refactor random name generator code
This commit is contained in:
@ -268,22 +268,7 @@ export const offersProfileRouter = createRouter()
|
|||||||
.digest('hex');
|
.digest('hex');
|
||||||
|
|
||||||
// Generate random name until unique
|
// Generate random name until unique
|
||||||
let uniqueName: string = generateRandomName();
|
const uniqueName: string = await generateRandomName();
|
||||||
|
|
||||||
let sameNameProfiles = await ctx.prisma.offersProfile.findMany({
|
|
||||||
where: {
|
|
||||||
profileName: uniqueName
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
while (sameNameProfiles.length !== 0) {
|
|
||||||
uniqueName = generateRandomName();
|
|
||||||
sameNameProfiles = await ctx.prisma.offersProfile.findMany({
|
|
||||||
where: {
|
|
||||||
profileName: uniqueName
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const profile = await ctx.prisma.offersProfile.create({
|
const profile = await ctx.prisma.offersProfile.create({
|
||||||
data: {
|
data: {
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
import type { Config } from 'unique-names-generator';
|
import type { Config } from 'unique-names-generator';
|
||||||
import { adjectives, animals,colors, uniqueNamesGenerator } from 'unique-names-generator';
|
import { adjectives, animals,colors, uniqueNamesGenerator } from 'unique-names-generator';
|
||||||
|
import { PrismaClient } from '@prisma/client';
|
||||||
|
|
||||||
|
const prisma = new PrismaClient()
|
||||||
|
|
||||||
const customConfig: Config = {
|
const customConfig: Config = {
|
||||||
dictionaries: [adjectives, colors, animals],
|
dictionaries: [adjectives, colors, animals],
|
||||||
@ -8,6 +11,23 @@ const customConfig: Config = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
export default function generateRandomName(): string {
|
export default async function generateRandomName(): Promise<string> {
|
||||||
return uniqueNamesGenerator(customConfig)
|
let uniqueName: string = uniqueNamesGenerator(customConfig);
|
||||||
|
|
||||||
|
let sameNameProfiles = await prisma.offersProfile.findMany({
|
||||||
|
where: {
|
||||||
|
profileName: uniqueName
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
while (sameNameProfiles.length !== 0) {
|
||||||
|
uniqueName = uniqueNamesGenerator(customConfig);
|
||||||
|
sameNameProfiles = await prisma.offersProfile.findMany({
|
||||||
|
where: {
|
||||||
|
profileName: uniqueName
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return uniqueName
|
||||||
}
|
}
|
Reference in New Issue
Block a user