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
14 changes: 14 additions & 0 deletions application/usecases/ContentCreatorSave.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ContentCreator } from "../../domain/entities/content-creator";
import { ContentCreatorRepository } from "../../domain/repositories/ContentCreatorRepository";

export class ContentCreatorSave {
constructor(
private readonly contentCreatorRepository: ContentCreatorRepository
) {}

async execute(
contentCreatorPayload: Omit<ContentCreator, "id">
): Promise<ContentCreator> {
return await this.contentCreatorRepository.save(contentCreatorPayload);
}
}
1 change: 1 addition & 0 deletions domain/repositories/ContentCreatorRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ import { ContentCreator } from '../entities/content-creator'

export interface ContentCreatorRepository {
getAll: () => Promise<ContentCreator[]>
save: (contentCreatorPayload: Omit<ContentCreator, 'id'>) => Promise<ContentCreator>
}
14 changes: 14 additions & 0 deletions infrastructure/driven-adapters/prisma/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { PrismaClient } from "@prisma/client"

export class PrismaDBClient {
private static _INSTANCE: PrismaClient

static getInstance(): PrismaClient {
if (this._INSTANCE === undefined) {
this._INSTANCE = new PrismaClient()
}

return this._INSTANCE
}
}

30 changes: 30 additions & 0 deletions infrastructure/driven-adapters/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions?
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init

generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}

model ContentCreator {
id String @id @default(auto()) @map("_id") @db.ObjectId
name String @unique
language String
logoUrl String
socialNetworks SocialNetworks @map("socialnetworks")
}

type SocialNetworks {
ig String[]
youtube String[]
x String[]
twitch String[]
github String[]
}
132 changes: 132 additions & 0 deletions infrastructure/driven-adapters/prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { PrismaDBClient } from ".";
import { ContentCreator } from "../../../domain/entities/content-creator";

const prismaClient = PrismaDBClient.getInstance();

async function main() {
const contentCreators: ContentCreator[] = [
{
name: "tuttodev",
language: "spanish",
logoUrl: "https://resources.creadoresapi.com/logos/tuttodev.png",
socialNetworks: {
ig: ["tuttodev"],
youtube: ["@tuttodev"],
twitch: [],
x: ["tuttodevv"],
github: ["tuttodev"],
},
},
{
name: "midudev",
language: "spanish",
logoUrl: "https://resources.creadoresapi.com/logos/midudev.png",
socialNetworks: {
ig: ["midu.dev"],
youtube: ["@midudev", "@midulive"],
twitch: ["midudev"],
x: ["midudev"],
github: ["midudev"],
},
},
{
name: "NetworkChuck",
language: "english",
logoUrl: "https://resources.creadoresapi.com/logos/networkchuck.png",
socialNetworks: {
ig: ["networkchuck"],
youtube: ["@NetworkChuck"],
twitch: ["networkchuck"],
x: ["networkchuck"],
github: [],
},
},
{
name: "Fazt",
language: "spanish",
logoUrl: "https://resources.creadoresapi.com/logos/fazt.png",
socialNetworks: {
ig: ["fazttech"],
youtube: ["@FaztCode", "@FaztTech"],
x: ["FaztTech"],
github: ["fazt", "faztcommunity", "FaztWeb"],
twitch: [],
},
},
{
name: "Programador X",
language: "spanish",
logoUrl: "https://resources.creadoresapi.com/logos/programador-x.png",
socialNetworks: {
ig: ["programador.x"],
youtube: ["@ProgramadorX"],
x: ["programador_x_"],
github: [],
twitch: [],
},
},
{
name: "MoureDev",
language: "spanish",
logoUrl: "https://resources.creadoresapi.com/logos/mouredev.png",
socialNetworks: {
ig: ["mouredev"],
youtube: ["@mouredevtv"],
x: ["MoureDev"],
twitch: ["mouredev"],
github: ["mouredev"],
},
},
{
name: "Traversy Media",
language: "english",
logoUrl: "https://resources.creadoresapi.com/logos/traversy-media.png",
socialNetworks: {
ig: ["traversymedia"],
youtube: ["@TraversyMedia"],
x: ["traversymedia"],
github: ["bradtraversy"],
twitch: [],
},
},
{
name: "developedbyed",
language: "english",
logoUrl: "https://resources.creadoresapi.com/logos/developedbyed.png",
socialNetworks: {
ig: ["developedbyed"],
youtube: ["@developedbyed"],
x: ["developedbyed"],
github: [],
twitch: [],
},
},
{
name: "munoncode",
language: "spanish",
logoUrl: "https://resources.creadoresapi.com/logos/munoncode.png",
socialNetworks: {
ig: ["munoncode"],
youtube: ["@munoncode"],
github: [],
twitch: [],
x: [],
},
},
] as unknown as any;

for (const creator of contentCreators) {
await prismaClient.contentCreator.create({
data: creator as any,
});
}
}

main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prismaClient.$disconnect();
});
19 changes: 17 additions & 2 deletions infrastructure/entry/express/routes/v1/content-creator/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
import { Request, Response } from 'express'
import Router from 'express-promise-router'
import { ContentCreatorGetter } from '../../../../../../application/usecases/ContentCreatorGetter'
import { JSONFileContentCreatorRepository } from '../../../../../persistence/json/JSONFileContentCreatorRepository'
import { MongoContentCreatorRepository } from '../../../../../persistence/mongodb/MongoContentCreatorRepository'
import { ContentCreatorSave } from '../../../../../../application/usecases/ContentCreatorSave'
import { contentCreatorSchema } from '../../../utils/contentCreatorSchema'

const router = Router()

// eslint-disable-next-line @typescript-eslint/no-misused-promises
router.get('/', async (_req: Request, res: Response): Promise<void> => {
const repository = new JSONFileContentCreatorRepository()
const repository = new MongoContentCreatorRepository()
const usecase = new ContentCreatorGetter(repository)

const contentCreators = await usecase.execute()

res.json(contentCreators)
})

router.post("/", async (req: Request, res: Response): Promise<void> => {
const repository = new MongoContentCreatorRepository()
const usecase = new ContentCreatorSave(repository)

const contentCreatorPayloadValidated = contentCreatorSchema.safeParse(req.body)
if (contentCreatorPayloadValidated.success) {
const newContentCreator = await usecase.execute(contentCreatorPayloadValidated.data)
res.status(200).json(newContentCreator)
} else {
res.status(400).json(contentCreatorPayloadValidated.error)
}
})

export default router
14 changes: 14 additions & 0 deletions infrastructure/entry/express/utils/contentCreatorSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { z } from "zod";

export const contentCreatorSchema = z.object({
name: z.string(),
language: z.string(),
logoUrl: z.string(),
socialNetworks: z.object({
ig: z.array(z.string()).nullable(),
youtube: z.array(z.string()).nullable(),
x: z.array(z.string()).nullable(),
twitch: z.array(z.string()).nullable(),
github: z.array(z.string()).nullable(),
}),
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ export class JSONFileContentCreatorRepository implements ContentCreatorRepositor
}
))
}

async save(): Promise<ContentCreator> {
return {} as ContentCreator
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { ContentCreator } from "../../../domain/entities/content-creator";
import { ContentCreatorRepository } from "../../../domain/repositories/ContentCreatorRepository";
import { PrismaDBClient } from "../../driven-adapters/prisma";

export class MongoContentCreatorRepository implements ContentCreatorRepository {
private _prismaClient = PrismaDBClient.getInstance();

async getAll(): Promise<ContentCreator[]> {
const contentCreators = await this._prismaClient.contentCreator.findMany();
console.log("Esto viene desde el Cluster Mongo DB Atlas");

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this console.log

console.log(contentCreators);
return contentCreators.map((contentCreator) => {
return {
id: contentCreator.id,
name: contentCreator.name,
language: contentCreator.language,
logoUrl: contentCreator.logoUrl,
socialNetworks: {
ig: contentCreator.socialNetworks.ig,
youtube: contentCreator.socialNetworks.youtube,
x: contentCreator.socialNetworks.x,
twitch: contentCreator.socialNetworks.twitch,
github: contentCreator.socialNetworks.github,
},
};
});
}

async save(
contentCreatorPayload: Omit<ContentCreator, "id">
): Promise<ContentCreator> {
const savedContentCreator: ContentCreator =
await this._prismaClient.contentCreator.create({
data: {
...contentCreatorPayload,
socialNetworks: {
ig:
contentCreatorPayload.socialNetworks.ig === null
? []
: contentCreatorPayload.socialNetworks.ig,
youtube:
contentCreatorPayload.socialNetworks.youtube === null
? []
: contentCreatorPayload.socialNetworks.youtube,
x:
contentCreatorPayload.socialNetworks.x === null
? []
: contentCreatorPayload.socialNetworks.x,
twitch:
contentCreatorPayload.socialNetworks.twitch === null
? []
: contentCreatorPayload.socialNetworks.twitch,
github:
contentCreatorPayload.socialNetworks.github === null
? []
: contentCreatorPayload.socialNetworks.github,
},
},
});

return savedContentCreator;
}
}
Loading