Skip to content

Commit

Permalink
feat: add published posts
Browse files Browse the repository at this point in the history
  • Loading branch information
Derlys committed Jan 23, 2024
1 parent 0db4e44 commit 3ed728e
Show file tree
Hide file tree
Showing 12 changed files with 222 additions and 12 deletions.
1 change: 1 addition & 0 deletions api-schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ type Query {
userFindManyIdentity(input: UserFindManyIdentityInput!): [Identity!]
userFindManyPayment(input: UserFindManyPaymentInput!): PaymentPaging!
userFindManyPost(input: UserFindManyPostInput!): PostPaging!
userFindManyPublishedPost(input: UserFindManyPostInput!): PostPaging!
userFindManyUser(input: UserFindManyUserInput!): UserPaging!
userFindOnePost(postId: String!): Post
userFindOneUser(username: String!): User
Expand Down
14 changes: 7 additions & 7 deletions libs/api/core/data-access/src/lib/api-core-provision-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export const provisionUsers: Prisma.UserCreateInput[] = [
{ title: 'Derlys Post 1', content: 'Hola!!', prices: { create: prices } },
{ title: 'Derlys Post 2', content: 'Hola!!', prices: { create: prices } },
{ title: 'Derlys Post 3', content: 'Hola!!', prices: { create: prices } },
{ title: 'Derlys Post 4', content: 'Hola!!', prices: { create: prices } },
{ title: 'Derlys Post 5', content: 'Hola!!', prices: { create: prices } },
{ title: 'Derlys Post 6', content: 'Hola!!', prices: { create: prices } },
{ title: 'Derlys Post 4', content: 'Hola!!' },
{ title: 'Derlys Post 5', content: 'Hola!!' },
{ title: 'Derlys Post 6', content: 'Hola!!' },
],
},
identities: {
Expand All @@ -34,10 +34,10 @@ export const provisionUsers: Prisma.UserCreateInput[] = [
developer: true,
posts: {
create: [
{ title: 'Alice Post 1', content: 'Hola from Alice!!', prices: { create: prices } },
{ title: 'Alice Post 2', content: 'Hola from Alice!!', prices: { create: prices } },
{ title: 'Alice Post 3', content: 'Hola from Alice!!', prices: { create: prices } },
{ title: 'Alice Post 4', content: 'Hola from Alice!!', prices: { create: prices } },
{ title: 'Alice Post 1', content: 'Hola from Alice!!' },
{ title: 'Alice Post 2', content: 'Hola from Alice!!' },
{ title: 'Alice Post 3', content: 'Hola from Alice!!' },
{ title: 'Alice Post 4', content: 'Hola from Alice!!' },
{ title: 'Alice Post 5', content: 'Hola from Alice!!', prices: { create: prices } },
{ title: 'Alice Post 6', content: 'Hola from Alice!!', prices: { create: prices } },
],
Expand Down
15 changes: 13 additions & 2 deletions libs/api/post/data-access/src/lib/api-user-post.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { UserUpdatePostInput } from './dto/user-update-post.input'
import { PostPaging } from './entity/post-paging.entity'
import { getUserPostWhereInput } from './helpers/get-user-post-where.input'
import { IdentityProvider } from '@connectamind/api-identity-data-access'
import { getUserPublishedPostWhereInput } from './helpers/get-user-published-post-where.input'

@Injectable()
export class ApiUserPostService {
Expand All @@ -30,8 +31,18 @@ export class ApiUserPostService {
return this.core.data.post
.paginate({
orderBy: { createdAt: 'desc' },
where: getUserPostWhereInput(input),
include: { author: true, payments: { where: { ownerId: userId } } },
where: getUserPostWhereInput(userId, input),
include: { author: true, prices: true },
})
.withPages({ limit: input.limit, page: input.page })
.then(([data, meta]) => ({ data, meta }))
}
async findManyPublishedPost(userId: string, input: UserFindManyPostInput) {
return this.core.data.post
.paginate({
orderBy: { createdAt: 'desc' },
where: getUserPublishedPostWhereInput(userId, input),
include: { author: true, payments: { where: { ownerId: userId } }, prices: true },
})
.withPages({ limit: input.limit, page: input.page })
.then(([data, meta]) => ({ data, meta }))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { Prisma } from '@prisma/client'
import { UserFindManyPostInput } from '../dto/user-find-many-post.input'

export function getUserPostWhereInput(input: UserFindManyPostInput): Prisma.PostWhereInput {
const where: Prisma.PostWhereInput = {}
export function getUserPostWhereInput(authorId: string, input: UserFindManyPostInput): Prisma.PostWhereInput {
const where: Prisma.PostWhereInput = {
//
authorId,
}

if (input.search) {
where.OR = [
{ id: { contains: input.search, mode: 'insensitive' } },
{ title: { contains: input.search, mode: 'insensitive' } },
{ content: { contains: input.search, mode: 'insensitive' } },
]
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { UserFindManyPostInput } from '@connectamind/api-post-data-access'
import { Prisma } from '@prisma/client'

export function getUserPublishedPostWhereInput(authorId: string, input: UserFindManyPostInput): Prisma.PostWhereInput {
const where: Prisma.PostWhereInput = {
prices: { some: {} }, //
}

if (input.search) {
where.OR = [
{ id: { contains: input.search, mode: 'insensitive' } },
{ title: { contains: input.search, mode: 'insensitive' } },
{ content: { contains: input.search, mode: 'insensitive' } },
]
}

return where
}
4 changes: 4 additions & 0 deletions libs/api/post/feature/src/lib/api-user-post.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export class ApiUserPostResolver {
userFindManyPost(@CtxUser() user: { id: string }, @Args('input') input: UserFindManyPostInput) {
return this.service.user.findManyPost(user.id, input)
}
@Query(() => PostPaging)
userFindManyPublishedPost(@CtxUser() user: { id: string }, @Args('input') input: UserFindManyPostInput) {
return this.service.user.findManyPublishedPost(user.id, input)
}

@Query(() => Post, { nullable: true })
userFindOnePost(@CtxUser() user: { id: string }, @Args('postId') postId: string) {
Expand Down
101 changes: 101 additions & 0 deletions libs/sdk/src/generated/graphql-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ export type Query = {
userFindManyIdentity?: Maybe<Array<Identity>>
userFindManyPayment: PaymentPaging
userFindManyPost: PostPaging
userFindManyPublishedPost: PostPaging
userFindManyUser: UserPaging
userFindOnePost?: Maybe<Post>
userFindOneUser?: Maybe<User>
Expand Down Expand Up @@ -415,6 +416,10 @@ export type QueryUserFindManyPostArgs = {
input: UserFindManyPostInput
}

export type QueryUserFindManyPublishedPostArgs = {
input: UserFindManyPostInput
}

export type QueryUserFindManyUserArgs = {
input: UserFindManyUserInput
}
Expand Down Expand Up @@ -1285,6 +1290,66 @@ export type UserFindManyPostQuery = {
}
}

export type UserFindManyPublishedPostQueryVariables = Exact<{
input: UserFindManyPostInput
}>

export type UserFindManyPublishedPostQuery = {
__typename?: 'Query'
paging: {
__typename?: 'PostPaging'
data: Array<{
__typename?: 'Post'
createdAt?: Date | null
id: string
title: string
content: string
updatedAt?: Date | null
authorId: string
author?: {
__typename?: 'User'
avatarUrl?: string | null
createdAt?: Date | null
developer?: boolean | null
id: string
name?: string | null
profileUrl?: string | null
publicKey?: string | null
role?: UserRole | null
status?: UserStatus | null
updatedAt?: Date | null
username?: string | null
} | null
prices?: Array<{
__typename?: 'Price'
createdAt?: Date | null
id: string
token: Token
amount: string
updatedAt?: Date | null
postId: string
}> | null
payment?: {
__typename?: 'Payment'
createdAt?: Date | null
id: string
signature: string
updatedAt?: Date | null
} | null
}>
meta: {
__typename?: 'PagingMeta'
currentPage: number
isFirstPage: boolean
isLastPage: boolean
nextPage?: number | null
pageCount?: number | null
previousPage?: number | null
totalCount?: number | null
}
}
}

export type UserFindOnePostQueryVariables = Exact<{
postId: Scalars['String']['input']
}>
Expand Down Expand Up @@ -2081,6 +2146,20 @@ export const UserFindManyPostDocument = gql`
${PostDetailsFragmentDoc}
${PagingMetaDetailsFragmentDoc}
`
export const UserFindManyPublishedPostDocument = gql`
query userFindManyPublishedPost($input: UserFindManyPostInput!) {
paging: userFindManyPublishedPost(input: $input) {
data {
...PostDetails
}
meta {
...PagingMetaDetails
}
}
}
${PostDetailsFragmentDoc}
${PagingMetaDetailsFragmentDoc}
`
export const UserFindOnePostDocument = gql`
query userFindOnePost($postId: String!) {
item: userFindOnePost(postId: $postId) {
Expand Down Expand Up @@ -2266,6 +2345,7 @@ const AdminCreatePostDocumentString = print(AdminCreatePostDocument)
const AdminUpdatePostDocumentString = print(AdminUpdatePostDocument)
const AdminDeletePostDocumentString = print(AdminDeletePostDocument)
const UserFindManyPostDocumentString = print(UserFindManyPostDocument)
const UserFindManyPublishedPostDocumentString = print(UserFindManyPublishedPostDocument)
const UserFindOnePostDocumentString = print(UserFindOnePostDocument)
const UserCreatePostDocumentString = print(UserCreatePostDocument)
const UserUpdatePostDocumentString = print(UserUpdatePostDocument)
Expand Down Expand Up @@ -2819,6 +2899,27 @@ export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper =
variables,
)
},
userFindManyPublishedPost(
variables: UserFindManyPublishedPostQueryVariables,
requestHeaders?: GraphQLClientRequestHeaders,
): Promise<{
data: UserFindManyPublishedPostQuery
errors?: GraphQLError[]
extensions?: any
headers: Headers
status: number
}> {
return withWrapper(
(wrappedRequestHeaders) =>
client.rawRequest<UserFindManyPublishedPostQuery>(UserFindManyPublishedPostDocumentString, variables, {
...requestHeaders,
...wrappedRequestHeaders,
}),
'userFindManyPublishedPost',
'query',
variables,
)
},
userFindOnePost(
variables: UserFindOnePostQueryVariables,
requestHeaders?: GraphQLClientRequestHeaders,
Expand Down
10 changes: 10 additions & 0 deletions libs/sdk/src/graphql/feature-post.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ query userFindManyPost($input: UserFindManyPostInput!) {
}
}
}
query userFindManyPublishedPost($input: UserFindManyPostInput!) {
paging: userFindManyPublishedPost(input: $input) {
data {
...PostDetails
}
meta {
...PagingMetaDetails
}
}
}

query userFindOnePost($postId: String!) {
item: userFindOnePost(postId: $postId) {
Expand Down
4 changes: 3 additions & 1 deletion libs/web/dashboard/feature/src/lib/dashboard-feature.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useAuth } from '@connectamind/web-auth-data-access'
import { UiContainer, UiDashboardGrid, UiDashboardItem, UiDebug } from '@pubkey-ui/core'
import { UiContainer, UiDashboardGrid, UiDashboardItem } from '@pubkey-ui/core'
import { IconBook, IconCurrencySolana, IconSettings, IconUser } from '@tabler/icons-react'
import { UserPublishedPostListFeature } from './user-published-post-list.feature'

const links: UiDashboardItem[] = [
// User Dashboard Links
Expand All @@ -18,6 +19,7 @@ export default function DashboardFeature() {
return (
<UiContainer>
<UiDashboardGrid links={links} />
<UserPublishedPostListFeature />
</UiContainer>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Group } from '@mantine/core'
import { UiPageLimit, UiSearchField } from '@connectamind/web-ui-core'
import { useUserFindManyPublishedPost } from '@connectamind/web-post-data-access'
import { UiDebug, UiDebugModal, UiInfo, UiLoader, UiPage } from '@pubkey-ui/core'

export function UserPublishedPostListFeature() {
const { items, pagination, query, setSearch } = useUserFindManyPublishedPost()

return (
<UiPage
title="Published Posts"
rightAction={
<Group>
<UiDebugModal data={items} />
</Group>
}
>
<Group>
<UiSearchField placeholder="Search post" setSearch={setSearch} />
<UiPageLimit limit={pagination.limit} setLimit={pagination.setLimit} setPage={pagination.setPage} />
</Group>

{query.isLoading ? <UiLoader /> : items?.length ? <UiDebug data={items} /> : <UiInfo message="No posts found" />}
</UiPage>
)
}
1 change: 1 addition & 0 deletions libs/web/post/data-access/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './lib/use-admin-find-many-post'
export * from './lib/use-admin-find-one-post'
export * from './lib/use-user-find-many-post'
export * from './lib/use-user-find-many-published-post'
export * from './lib/use-user-find-one-post'
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { UserFindManyPostInput } from '@connectamind/sdk'
import { useSdk } from '@connectamind/web-core-data-access'
import { useQuery } from '@tanstack/react-query'
import { useState } from 'react'

export function useUserFindManyPublishedPost(props?: Partial<UserFindManyPostInput>) {
const sdk = useSdk()
const [limit, setLimit] = useState(props?.limit ?? 10)
const [page, setPage] = useState(props?.page ?? 1)
const [search, setSearch] = useState<string>(props?.search ?? '')

const input: UserFindManyPostInput = { page, limit, search }
const query = useQuery({
queryKey: ['user', 'find-many-published-post', input],
queryFn: () => sdk.userFindManyPublishedPost({ input }).then((res) => res.data),
})
const total = query.data?.paging?.meta?.totalCount ?? 0
const items = query.data?.paging.data ?? []

return {
items,
query,
pagination: {
page,
setPage,
limit,
setLimit,
total,
},
setSearch,
}
}

0 comments on commit 3ed728e

Please sign in to comment.