Skip to main content

Create an API endpoint

Marblism already generates all the basic API endpoints you would need for each entity in your database:

  • findMany
  • findOne
  • save
  • update
  • delete

Let's see how to add an endpoint that returns the number of likes a tweet has.

1. Define the route in the controller

api/src/modules/tweet/application/tweet.controller.ts

@Controller('/v1/tweets')
export class TweetController {
@Get('/:tweetId/likesNumber')
async countLikes(@Param('tweetId') tweetId: string, @Req() request: Request) {
const likesNumber = await this.likeDomainFacade.countByTweetId(tweetId)

return likesNumber
}
}

2. Create the query

api/src/modules/like/domain/like.domain.facade.ts

export class LikeDomainFacade {
async countbyTweetId(tweetId: string): Promise<number> {
const numberLikes = this.repository.countBy({
tweetId: tweet.id,
})

return numberlikes
}
}

3. Register your newly created endpoint in the front-end

/web/src/modules/domain/tweet/tweet.api.ts

export class TweetApi {
export function countLikes(
tweetId: string,
): Promise<number> {
return HttpService.api.get(
`/v1/tweets/${tweetId}/likesNumber`,
)
}
}

To use it in the front-end:

const numberLikes = await Api.Tweet.countLikes(tweetId)