Skip to content

Commit

Permalink
feat: 🎸 add tag api
Browse files Browse the repository at this point in the history
  • Loading branch information
yeukfei02 committed Apr 20, 2022
1 parent 255a85d commit 15d2966
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { AppService } from './app.service';
import { UserModule } from './user/user.module';
import { LocationModule } from './location/location.module';
import { PostModule } from './post/post.module';
import { TagModule } from './tag/tag.module';

@Module({
imports: [UserModule, LocationModule, PostModule],
imports: [UserModule, LocationModule, PostModule, TagModule],
controllers: [AppController],
providers: [AppService],
})
Expand Down
4 changes: 2 additions & 2 deletions src/post/post.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ export class PostController {

@Post()
async createPost(@Body() createPostDto: CreatePostDto): Promise<any> {
const posts = await this.postService.createPost(createPostDto);
const post = await this.postService.createPost(createPostDto);

const response = { message: 'createpost', posts: posts };
const response = { message: 'createPost', post: post };
return response;
}

Expand Down
4 changes: 2 additions & 2 deletions src/post/post.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class PostService {
constructor(private readonly prisma: PrismaService) {}

async createPost(createPostDto: CreatePostDto): Promise<post> {
const posts = await this.prisma.post.create({
const post = await this.prisma.post.create({
data: {
text: createPostDto.text,
image: createPostDto.image,
Expand All @@ -31,7 +31,7 @@ export class PostService {
comments: true,
},
});
return posts;
return post;
}

async getPosts(): Promise<any> {
Expand Down
4 changes: 4 additions & 0 deletions src/tag/dto/createTag.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class CreateTagDto {
name: string;
post_id: string;
}
18 changes: 18 additions & 0 deletions src/tag/tag.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { TagController } from './tag.controller';

describe('TagController', () => {
let controller: TagController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [TagController],
}).compile();

controller = module.get<TagController>(TagController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
24 changes: 24 additions & 0 deletions src/tag/tag.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Controller, Post, Get, Body } from '@nestjs/common';
import { TagService } from './tag.service';
import { CreateTagDto } from './dto/createTag.dto';

@Controller('tag')
export class TagController {
constructor(private readonly tagService: TagService) {}

@Post()
async createTag(@Body() createTagDto: CreateTagDto): Promise<any> {
const tag = await this.tagService.createTag(createTagDto);

const response = { message: 'createTag', tag: tag };
return response;
}

@Get()
async getTags(): Promise<any> {
const tags = await this.tagService.getTags();

const response = { message: 'getTags', tags: tags };
return response;
}
}
11 changes: 11 additions & 0 deletions src/tag/tag.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { TagController } from './tag.controller';
import { TagService } from './tag.service';
import { PrismaService } from '../prisma.service';

@Module({
imports: [],
controllers: [TagController],
providers: [TagService, PrismaService],
})
export class TagModule {}
18 changes: 18 additions & 0 deletions src/tag/tag.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { TagService } from './tag.service';

describe('TagService', () => {
let service: TagService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [TagService],
}).compile();

service = module.get<TagService>(TagService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
31 changes: 31 additions & 0 deletions src/tag/tag.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Injectable } from '@nestjs/common';
import { CreateTagDto } from './dto/createTag.dto';
import { PrismaService } from '../prisma.service';
import { tag } from '@prisma/client';

@Injectable()
export class TagService {
constructor(private readonly prisma: PrismaService) {}

async createTag(createTagDto: CreateTagDto): Promise<tag> {
const tag = await this.prisma.tag.create({
data: {
name: createTagDto.name,
post_id: createTagDto.post_id,
},
include: {
post: true,
},
});
return tag;
}

async getTags(): Promise<any> {
const tags = await this.prisma.tag.findMany({
include: {
post: true,
},
});
return tags;
}
}

0 comments on commit 15d2966

Please sign in to comment.