Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: api crud generates user/public services #141

Merged
merged 1 commit into from
Apr 23, 2021
Merged
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
export * from './lib/dto/admin-create-<%= dasherize(modelName) %>.input'
export * from './lib/dto/admin-list-<%= dasherize(modelName) %>.input'
export * from './lib/dto/admin-update-<%= dasherize(modelName) %>.input'
export * from './lib/dto/user-create-<%= dasherize(modelName) %>.input'
export * from './lib/dto/user-list-<%= dasherize(modelName) %>.input'
export * from './lib/dto/user-update-<%= dasherize(modelName) %>.input'
export * from './lib/models/<%= dasherize(modelName) %>.model'
export * from './lib/<%= projectName %>.module'
export * from './lib/<%= projectName %>.service'
export * from './lib/<%= projectName %>-admin.service'
export * from './lib/<%= projectName %>-public.service'
export * from './lib/<%= projectName %>-user.service'
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { AdminList<%= classify(modelName) %>Input } from './dto/admin-list-<%= d
import { AdminUpdate<%= classify(modelName) %>Input } from './dto/admin-update-<%= dasherize(modelName) %>.input'

@Injectable()
export class <%= classify(projectName) %>Service {
export class <%= classify(projectName) %>AdminService {
constructor(private readonly data: <%= classify(apiAppName) %>CoreDataAccessService) {}

admin<%= classify(pluralModelName) %>(adminId: string, input?: AdminList<%= classify(modelName) %>Input) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Injectable } from '@nestjs/common'
import { <%= classify(apiAppName) %>CoreDataAccessService } from '@<%= npmScope %>/<%= apiAppName %>/core/data-access'

@Injectable()
export class <%= classify(projectName) %>PublicService {
constructor(private readonly data: <%= classify(apiAppName) %>CoreDataAccessService) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Injectable } from '@nestjs/common'
import { <%= classify(apiAppName) %>CoreDataAccessService, CorePaging, CorePagingInput } from '@<%= npmScope %>/<%= apiAppName %>/core/data-access'

import { UserCreate<%= classify(modelName) %>Input } from './dto/user-create-<%= dasherize(modelName) %>.input'
import { UserList<%= classify(modelName) %>Input } from './dto/user-list-<%= dasherize(modelName) %>.input'
import { UserUpdate<%= classify(modelName) %>Input } from './dto/user-update-<%= dasherize(modelName) %>.input'

@Injectable()
export class <%= classify(projectName) %>UserService {
constructor(private readonly data: <%= classify(apiAppName) %>CoreDataAccessService) {}

user<%= classify(pluralModelName) %>(userId: string, input?: UserList<%= classify(modelName) %>Input) {
return this.data.<%= camelize(modelName) %>.findMany({
take: input?.limit,
skip: input?.skip,
})
}

async userCount<%= classify(pluralModelName) %>(userId: string, input?: UserList<%= classify(modelName) %>Input): Promise<CorePaging> {
const total = await this.data.<%= camelize(modelName) %>.count()
return {
limit: input?.limit,
skip: input?.skip,
total,
}
}

user<%= classify(modelName) %>(userId: string, <%= camelize(modelName) %>Id) {
return this.data.<%= camelize(modelName) %>.findUnique({ where: { id: <%= camelize(modelName) %>Id } })
}

userCreate<%= classify(modelName) %>(userId: string, input: UserCreate<%= classify(modelName) %>Input) {
return this.data.<%= camelize(modelName) %>.create({
data: { <%= nameField %>: input.<%= nameField %> },
})
}

userUpdate<%= classify(modelName) %>(userId: string, <%= camelize(modelName) %>Id, input: UserUpdate<%= classify(modelName) %>Input) {
return this.data.<%= camelize(modelName) %>.update({
where: { id: <%= camelize(modelName) %>Id },
data: { <%= nameField %>: input.<%= nameField %> },
})
}

userDelete<%= classify(modelName) %>(userId: string, <%= camelize(modelName) %>Id) {
return this.data.<%= camelize(modelName) %>.delete({ where: { id: <%= camelize(modelName) %>Id } })
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Module } from '@nestjs/common'
import { <%= classify(apiAppName) %>CoreDataAccessModule } from '@<%= npmScope %>/<%= apiAppName %>/core/data-access'

import { <%= classify(projectName) %>Service } from './<%= projectName %>.service'
import { <%= classify(projectName) %>AdminService } from './<%= projectName %>-admin.service'
import { <%= classify(projectName) %>PublicService } from './<%= projectName %>-public.service'
import { <%= classify(projectName) %>UserService } from './<%= projectName %>-user.service'

@Module({
imports: [<%= classify(apiAppName) %>CoreDataAccessModule],
providers: [<%= classify(projectName) %>Service],
exports: [<%= classify(projectName) %>Service],
providers: [<%= classify(projectName) %>AdminService, <%= classify(projectName) %>PublicService, <%= classify(projectName) %>UserService],
exports: [<%= classify(projectName) %>AdminService, <%= classify(projectName) %>PublicService, <%= classify(projectName) %>UserService],
})
export class <%= classify(projectName) %>Module {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Field, InputType } from '@nestjs/graphql'

@InputType()
export class UserCreate<%= classify(modelName) %>Input {
@Field()
<%= nameField %>: string
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Field, InputType } from '@nestjs/graphql'
import { CorePagingInput } from '@<%= npmScope %>/<%= apiAppName %>/core/data-access'

@InputType()
export class UserList<%= classify(modelName) %>Input extends CorePagingInput {
@Field({ nullable: true })
<%= nameField %>?: string
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Field, InputType } from '@nestjs/graphql'

@InputType()
export class UserUpdate<%= classify(modelName) %>Input {
@Field({ nullable: true })
<%= nameField %>?: string
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
AdminCreate<%= classify(modelName) %>Input,
AdminList<%= classify(modelName) %>Input,
AdminUpdate<%= classify(modelName) %>Input,
<%= classify(apiAppName) %><%= classify(modelName) %>DataAccessService,
<%= classify(apiAppName) %><%= classify(modelName) %>DataAccessAdminService,
<%= classify(modelName) %>,
} from '@<%= npmScope %>/<%= apiAppName %>/<%= dasherize(modelName) %>/data-access'
import { CorePaging } from '@<%= npmScope %>/<%= apiAppName %>/core/data-access'
Expand All @@ -17,7 +17,7 @@ import { User } from '@<%= npmScope %>/<%= apiAppName %>/user/data-access'
@Resolver()
@UseGuards(GqlAuthAdminGuard)
export class <%= classify(projectName) %>AdminResolver {
constructor(private readonly service: <%= classify(apiAppName) %><%= classify(modelName) %>DataAccessService) {}
constructor(private readonly service: <%= classify(apiAppName) %><%= classify(modelName) %>DataAccessAdminService) {}

@Query(() => [<%= classify(modelName) %>], { nullable: true })
admin<%= classify(pluralModelName) %>(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Resolver } from '@nestjs/graphql'
import { <%= classify(apiAppName) %><%= classify(modelName) %>DataAccessService, <%= classify(modelName) %> } from '@<%= npmScope %>/<%= apiAppName %>/<%= dasherize(modelName) %>/data-access'
import { <%= classify(apiAppName) %><%= classify(modelName) %>DataAccessPublicService, <%= classify(modelName) %> } from '@<%= npmScope %>/<%= apiAppName %>/<%= dasherize(modelName) %>/data-access'

@Resolver(() => <%= classify(modelName) %>)
export class <%= classify(projectName) %>PublicResolver {
constructor(private readonly service: <%= classify(apiAppName) %><%= classify(modelName) %>DataAccessService) {}
constructor(private readonly service: <%= classify(apiAppName) %><%= classify(modelName) %>DataAccessPublicService) {}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,61 @@
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql'
import { UseGuards } from '@nestjs/common'
import { Resolver } from '@nestjs/graphql'
import { <%= classify(apiAppName) %><%= classify(modelName) %>DataAccessService } from '@<%= npmScope %>/<%= apiAppName %>/<%= dasherize(modelName) %>/data-access'
import { GqlAuthGuard } from '@<%= npmScope %>/<%= apiAppName %>/auth/util'
import {
UserCreate<%= classify(modelName) %>Input,
UserList<%= classify(modelName) %>Input,
UserUpdate<%= classify(modelName) %>Input,
<%= classify(apiAppName) %><%= classify(modelName) %>DataAccessUserService,
<%= classify(modelName) %>,
} from '@<%= npmScope %>/<%= apiAppName %>/<%= dasherize(modelName) %>/data-access'
import { CorePaging } from '@<%= npmScope %>/<%= apiAppName %>/core/data-access'
import {
CtxUser,
GqlAuthGuard,
} from '@<%= npmScope %>/<%= apiAppName %>/auth/util'
import { User } from '@<%= npmScope %>/<%= apiAppName %>/user/data-access'

@Resolver()
@UseGuards(GqlAuthGuard)
export class <%= classify(projectName) %>UserResolver {
constructor(private readonly service: <%= classify(apiAppName) %><%= classify(modelName) %>DataAccessService) {}
constructor(private readonly service: <%= classify(apiAppName) %><%= classify(modelName) %>DataAccessUserService) {}

@Query(() => [<%= classify(modelName) %>], { nullable: true })
user<%= classify(pluralModelName) %>(
@CtxUser() user: User,
@Args({ name: 'input', type: () => UserList<%= classify(modelName) %>Input, nullable: true }) input?: UserList<%= classify(modelName) %>Input,
) {
return this.service.user<%= classify(pluralModelName) %>(user.id, input)
}

@Query(() => CorePaging, { nullable: true })
userCount<%= classify(pluralModelName) %>(
@CtxUser() user: User,
@Args({ name: 'input', type: () => UserList<%= classify(modelName) %>Input, nullable: true }) input?: UserList<%= classify(modelName) %>Input,
) {
return this.service.userCount<%= classify(pluralModelName) %>(user.id, input)
}

@Query(() => <%= classify(modelName) %>, { nullable: true })
user<%= classify(modelName) %>(@CtxUser() user: User, @Args('<%= camelize(modelName) %>Id') <%= camelize(modelName) %>Id: string) {
return this.service.user<%= classify(modelName) %>(user.id, <%= camelize(modelName) %>Id)
}

@Mutation(() => <%= classify(modelName) %>, { nullable: true })
userCreate<%= classify(modelName) %>(@CtxUser() user: User,@Args('input') input: UserCreate<%= classify(modelName) %>Input,) {
return this.service.userCreate<%= classify(modelName) %>(user.id, input)
}

@Mutation(() => <%= classify(modelName) %>, { nullable: true })
userUpdate<%= classify(modelName) %>(
@CtxUser() user: User,
@Args('<%= camelize(modelName) %>Id') <%= camelize(modelName) %>Id: string,
@Args('input') input: UserUpdate<%= classify(modelName) %>Input,
) {
return this.service.userUpdate<%= classify(modelName) %>(user.id, <%= camelize(modelName) %>Id, input)
}

@Mutation(() => <%= classify(modelName) %>, { nullable: true })
userDelete<%= classify(modelName) %>(@CtxUser() user: User, @Args('<%= camelize(modelName) %>Id') <%= camelize(modelName) %>Id: string) {
return this.service.userDelete<%= classify(modelName) %>(user.id, <%= camelize(modelName) %>Id)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,42 @@ mutation AdminDelete<%= classify(modelName) %>($<%= camelize(modelName) %>Id: St
...<%= classify(modelName) %>Details
}
}

query User<%= classify(pluralModelName) %>($input: UserList<%= classify(modelName) %>Input) {
items: user<%= classify(pluralModelName) %>(input: $input) {
...<%= classify(modelName) %>Details
}
count: userCount<%= classify(pluralModelName) %>(input: $input) {
...CorePagingDetails
}
}

query UserCount<%= classify(pluralModelName) %>($input: UserList<%= classify(modelName) %>Input) {
count: userCount<%= classify(pluralModelName) %>(input: $input) {
...CorePagingDetails
}
}

query User<%= classify(modelName) %>($<%= camelize(modelName) %>Id: String!) {
item: user<%= classify(modelName) %>(<%= camelize(modelName) %>Id: $<%= camelize(modelName) %>Id) {
...<%= classify(modelName) %>Details
}
}

mutation UserCreate<%= classify(modelName) %>($input: UserCreate<%= classify(modelName) %>Input!) {
created: userCreate<%= classify(modelName) %>(input: $input) {
...<%= classify(modelName) %>Details
}
}

mutation UserUpdate<%= classify(modelName) %>($<%= camelize(modelName) %>Id: String!, $input: UserUpdate<%= classify(modelName) %>Input!) {
updated: userUpdate<%= classify(modelName) %>(<%= camelize(modelName) %>Id: $<%= camelize(modelName) %>Id, input: $input) {
...<%= classify(modelName) %>Details
}
}

mutation UserDelete<%= classify(modelName) %>($<%= camelize(modelName) %>Id: String!) {
deleted: userDelete<%= classify(modelName) %>(<%= camelize(modelName) %>Id: $<%= camelize(modelName) %>Id) {
...<%= classify(modelName) %>Details
}
}