Skip to content

Commit

Permalink
Merge pull request IT-Academy-BCN#201 from IT-Academy-BCN/190-be-crea…
Browse files Browse the repository at this point in the history
…te-new-unique-column-slug-on-topics-and-resources

190 be create new unique column slug on topics and resources
  • Loading branch information
danimorera authored May 2, 2023
2 parents 540ebc5 + e12fa7f commit c975a5e
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 1 deletion.
2 changes: 2 additions & 0 deletions back/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,8 @@ paths:
type: string
title:
type: string
slug:
type: string
description:
type: string
url:
Expand Down
37 changes: 37 additions & 0 deletions back/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions back/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"koa-body": "6.0.1",
"koa-helmet": "7.0.1",
"koa2-swagger-ui": "^5.7.0",
"slugify": "^1.6.6",
"ts-node": "^10.9.1",
"yaml": "^2.2.2",
"yamljs": "^0.3.0",
Expand Down
5 changes: 5 additions & 0 deletions back/prisma/data/resources.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
import { Prisma } from '@prisma/client'
import slugify from 'slugify'

export const resources: Omit<
Prisma.ResourceCreateArgs['data'],
'userId'
>[] = [
{
title: 'My resource in React',
slug: slugify('My resource in React', { lower: true }),
description: 'Lorem ipsum',
url: 'http://www.example.com/resource/React.html',
resourceType: 'BLOG',
},
{
title: 'My resource in Node',
slug: slugify('My resource in Node', { lower: true }),
description: 'Lorem ipsum',
url: 'http://www.example.com/resource/Node.html',
resourceType: 'BLOG',
},
{
title: 'My second resource in React',
slug: slugify('My second resource in React', { lower: true }),
description: 'Lorem ipsum',
url: 'http://www.example.com/resource/React2.html',
resourceType: 'BLOG',
},
{
title: 'My resource in Javascript',
slug: slugify('My resource in Javascript', { lower: true }),
description: 'Lorem ipsum',
url: 'http://www.example.com/resource/Javascript.html',
resourceType: 'BLOG',
Expand Down
5 changes: 5 additions & 0 deletions back/prisma/data/topics.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import { Prisma } from '@prisma/client'
import slugify from 'slugify'

export const topics: Omit<
Prisma.TopicCreateArgs['data'],
'categoryId'
>[] = [
{
name: "Components",
slug: slugify("Components", { lower: true })
},
{
name: "Eventos",
slug: slugify("Eventos", { lower: true })
},
{
name: "Listas",
slug: slugify("Listas", { lower: true })
},
{
name: "Estilos",
slug: slugify("Estilos", { lower: true })
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
Warnings:
- A unique constraint covering the columns `[slug]` on the table `resource` will be added. If there are existing duplicate values, this will fail.
- A unique constraint covering the columns `[slug]` on the table `topic` will be added. If there are existing duplicate values, this will fail.
- Added the required column `slug` to the `resource` table without a default value. This is not possible if the table is not empty.
- Added the required column `slug` to the `topic` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "resource" ADD COLUMN "slug" TEXT NOT NULL;

-- AlterTable
ALTER TABLE "topic" ADD COLUMN "slug" TEXT NOT NULL;

-- CreateIndex
CREATE UNIQUE INDEX "resource_slug_key" ON "resource"("slug");

-- CreateIndex
CREATE UNIQUE INDEX "topic_slug_key" ON "topic"("slug");
2 changes: 2 additions & 0 deletions back/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ enum USER_ROLE {
model Resource {
id String @id @default(cuid())
title String
slug String @unique
description String?
url String
resourceType RESOURCE_TYPE @map("resource_type")
Expand Down Expand Up @@ -73,6 +74,7 @@ model Category {
model Topic {
id String @id @default(cuid())
name String
slug String @unique
category Category @relation(fields: [categoryId], references: [id])
categoryId String @map("category_id")
resources TopicsOnResources[]
Expand Down
3 changes: 2 additions & 1 deletion back/src/__tests__/resources/resources.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('Testing resources endpoint', () => {
test('should create a new resource with topics', async () => {
const newResource = {
title: 'New Resource',
slug:'new-resource',
description: 'This is a new resource',
url: 'https://example.com/resource',
resourceType: 'BLOG',
Expand Down Expand Up @@ -60,7 +61,7 @@ describe('Testing resources endpoint', () => {
.set('Cookie', authToken)
.send(newResource)

expect(response.status).toBe(204)
expect(response.status).toBe(422)
})

test('should fail with wrong resource type', async () => {
Expand Down
1 change: 1 addition & 0 deletions back/src/schemas/resourceCreateSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { resourceSchema } from './resourceSchema'

export const resourceCreateSchema = resourceSchema.omit({
id: true,
slug: true,
createdAt: true,
updatedAt: true,
userId: true
Expand Down
1 change: 1 addition & 0 deletions back/src/schemas/resourceSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { z } from '../openapi/zod'
export const resourceSchema = z.object({
id: z.string(),
title: z.string(),
slug: z.string().optional(),
description: z.string().optional(),
url: z.string().url(),
resourceType: z.enum(['BLOG', 'VIDEO', 'TUTORIAL']),
Expand Down
1 change: 1 addition & 0 deletions back/src/schemas/topicSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { z } from '../openapi/zod'
export const topicSchema = z.object({
id: z.string(),
topic: z.string(),
slug: z.string().optional(),
createdAt: z.date(),
updatedAt: z.date()
});

0 comments on commit c975a5e

Please sign in to comment.