literakl / mezinamiridici

Traffic forum
htttps://www.mezinamiridici.cz
MIT License
4 stars 4 forks source link

Slug is not unique #166

Closed literakl closed 3 years ago

literakl commented 3 years ago

Slug is the last part of URL, it is not unique.

{ "_id" : "1f2kd8dp73gk", "info" : { "slug" : "third-question" } } { "_id" : "1f2kd8dq73y4", "info" : { "slug" : "today-blog" } } { "_id" : "1f2kd8dqnrxo", "info" : { "slug" : "1-day-ago-blog" } }

http://localhost:8080/p/akbar/b/hello-world-blog-create-validation

Saving the blog with the same slug will result in an error (not visible in UI). Create api.js#getSlug(dbClient, String) method that will generate slug for the input and seek it as a prefix between all slugs. If it exists, it will either create 'slug-1', or slug-${last + 1}

literakl commented 3 years ago

Optimistic locking algorithm:

getSlug(dbClient, caption) { const slug = generate from caption; const item = dbclient.items.findOne({slug}, {projection: {_id: 1}}); if (!item) return slug; else { const items = dbClient.items.find({ slug with ${slug} prefix },{projection: {_id: 1}}).toArray; items.forEach(foundSlug => { extract numeric part and find the largest value }); return ${slug}-${biggestId + 1} } }

literakl commented 3 years ago

works fine