Skip to content

Commit

Permalink
feat: ✨ add task resolvers logic
Browse files Browse the repository at this point in the history
  • Loading branch information
zhumeisongsong committed Dec 7, 2024
1 parent 48646b7 commit 934ee65
Show file tree
Hide file tree
Showing 12 changed files with 193 additions and 11 deletions.
2 changes: 1 addition & 1 deletion libs/tasks/interface-adapters/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './lib/tasks-interface-adapters';
export * from './lib/tasks.module';
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { CreateUserTaskDto } from './create-user-task.dto';

describe('CreateUserTaskDto', () => {
it('should create a CreateUserTaskDto instance', () => {
const id = '123';
const createdAt = new Date();

const dto = new CreateUserTaskDto(id, createdAt);

expect(dto).toBeDefined();
expect(dto.id).toBe(id);
expect(dto.createdAt).toBe(createdAt);
});
});
15 changes: 15 additions & 0 deletions libs/tasks/interface-adapters/src/lib/dto/create-user-task.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Field, InputType } from '@nestjs/graphql';

@InputType()
export class CreateUserTaskDto {
@Field(() => String)
id: string;

@Field(() => Date)
createdAt: Date;

constructor(id: string, createdAt: Date) {
this.id = id;
this.createdAt = createdAt;
}
}
29 changes: 29 additions & 0 deletions libs/tasks/interface-adapters/src/lib/dto/task.dto.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { TaskDto } from './task.dto';

describe('TaskDto', () => {
it('should create a TaskDto instance', () => {
const id = '123';
const title = 'Test Task';
const description = 'Test Description';
const categories = ['category1', 'category2'];

const dto = new TaskDto(id, title, description, categories);

expect(dto).toBeDefined();
expect(dto.id).toBe(id);
expect(dto.title).toBe(title);
expect(dto.description).toBe(description);
expect(dto.categories).toEqual(categories);
});

it('should allow null description', () => {
const id = '123';
const title = 'Test Task';
const description = null;
const categories = ['category1'];

const dto = new TaskDto(id, title, description, categories);

expect(dto.description).toBeNull();
});
});
30 changes: 30 additions & 0 deletions libs/tasks/interface-adapters/src/lib/dto/task.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Field, ID, ObjectType } from '@nestjs/graphql';
import { IsOptional } from 'class-validator';

@ObjectType()
export class TaskDto {
@Field(() => ID)
id: string;

@Field()
title: string;

@Field(() => String, { nullable: true })
@IsOptional()
description: string | null;

@Field(() => [String])
categories: string[];

constructor(
id: string,
title: string,
description: string | null,
categories: string[],
) {
this.id = id;
this.title = title;
this.description = description;
this.categories = categories;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { UpdateUserTaskDto } from './update-user-task.dto';

describe('UpdateUserTaskDto', () => {
it('should create an UpdateUserTaskDto instance', () => {
const id = '123';
const updatedAt = new Date();

const dto = new UpdateUserTaskDto(id, updatedAt);

expect(dto).toBeDefined();
expect(dto.id).toBe(id);
expect(dto.updatedAt).toBe(updatedAt);
});
});
15 changes: 15 additions & 0 deletions libs/tasks/interface-adapters/src/lib/dto/update-user-task.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Field, InputType } from '@nestjs/graphql';

@InputType()
export class UpdateUserTaskDto {
@Field(() => String)
id: string;

@Field(() => Date)
updatedAt: Date;

constructor(id: string, updatedAt: Date) {
this.id = id;
this.updatedAt = updatedAt;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
import { TasksService } from '@tasks/application';

import { TaskDto } from '../dto/task.dto';
import { CreateUserTaskDto } from '../dto/create-user-task.dto';
import { UpdateUserTaskDto } from '../dto/update-user-task.dto';

@Resolver(() => TaskDto)
export class TasksResolver {
constructor(private tasksService: TasksService) {}

@Query(() => TaskDto, { nullable: true })
async getTasks(): Promise<TaskDto[]> {
return this.tasksService.findAll();
}

@Mutation(() => String)
async createUserTasks(
@Args('userId') userId: string,
@Args('tasks') tasks: CreateUserTaskDto[],
): Promise<string> {
return this.tasksService.createUserTasks(userId, tasks);
}

@Mutation(() => String)
async updateUserTasks(
@Args('userId') userId: string,
@Args('userTasks') userTasks: UpdateUserTaskDto[],
): Promise<string> {
return this.tasksService.updateUserTasks(userId, userTasks);
}
}
32 changes: 32 additions & 0 deletions libs/tasks/interface-adapters/src/lib/resolver/tasks.resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
import { TasksService } from '@tasks/application';

import { TaskDto } from '../dto/task.dto';
import { CreateUserTaskDto } from '../dto/create-user-task.dto';
import { UpdateUserTaskDto } from '../dto/update-user-task.dto';

@Resolver(() => TaskDto)
export class TasksResolver {
constructor(private tasksService: TasksService) {}

@Query(() => TaskDto, { nullable: true })
async getTasks(): Promise<TaskDto[]> {
return this.tasksService.findAll();
}

@Mutation(() => String)
async createUserTasks(
@Args('userId') userId: string,
@Args('tasks') tasks: CreateUserTaskDto[],
): Promise<string> {
return this.tasksService.createUserTasks(userId, tasks);
}

@Mutation(() => String)
async updateUserTasks(
@Args('userId') userId: string,
@Args('userTasks') userTasks: UpdateUserTaskDto[],
): Promise<string> {
return this.tasksService.updateUserTasks(userId, userTasks);
}
}

This file was deleted.

This file was deleted.

11 changes: 11 additions & 0 deletions libs/tasks/interface-adapters/src/lib/tasks.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { TasksService } from '@tasks/application';

import { TasksResolver } from './resolver/tasks.resolver';

@Module({
imports: [],
providers: [TasksResolver, TasksService],
exports: [TasksService],
})
export class TasksModule {}

0 comments on commit 934ee65

Please sign in to comment.