Skip to content

Commit cbeb52b

Browse files
committed
update
1 parent fd17a1f commit cbeb52b

File tree

9 files changed

+120
-3
lines changed

9 files changed

+120
-3
lines changed

server/src/constants/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ export const ENDPOINTS = {
2525
SINGLE: '/upload/single',
2626
MULTIPLE: '/upload/multiple',
2727
},
28+
CHANNEL: '/channel',
2829
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { ChannelService } from "@/services/channels.service";
2+
import { catchAsync } from "@/utils/catch-async";
3+
import { StatusCodes } from "http-status-codes";
4+
import Container from "typedi";
5+
6+
export class ChannelController {
7+
public channelService = Container.get(ChannelService);
8+
9+
public createChannel = catchAsync(async (req, res) => {
10+
const data = await this.channelService.create(req.body);
11+
res.status(StatusCodes.OK).json({
12+
message: 'Tạo channel thành công',
13+
data,
14+
});
15+
});
16+
17+
}

server/src/database/schema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ export const channels = pgTable(
5959
.primaryKey(),
6060
contactId: text('contact_id').unique().notNull(),
6161
contactName: text('contact_name').notNull(),
62-
credentials: json('credentials'),
63-
active: boolean('active').default(true),
62+
credentials: text('credentials'),
63+
active: boolean('active'),
6464
channelTypeId: integer('channel_type_id').notNull(),
6565
createdAt: timestamp('created_at').defaultNow(),
6666
updatedAt: timestamp('update_at').defaultNow(),

server/src/database/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,7 @@ export const ROLES = z.enum(schema.roles.enumValues);
4040
export type TNewUser = typeof schema.users.$inferInsert;
4141

4242
export type TUpdateUser = Partial<TNewUser>;
43+
44+
export type TNewChannel = typeof schema.channels.$inferInsert;
45+
46+
export type TUpdateChannel = Partial<TNewChannel>;

server/src/dtos/channels.dto.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { IsBoolean, IsNotEmpty, IsNumber, IsString } from "class-validator";
2+
3+
export class channelDTO {
4+
@IsString()
5+
@IsNotEmpty({
6+
message: 'ContactID không được bỏ trống',
7+
})
8+
contactId: string;
9+
10+
@IsString()
11+
@IsNotEmpty({
12+
message: 'Contact name không được bỏ trống',
13+
})
14+
contactName: string;
15+
16+
@IsNumber()
17+
@IsNotEmpty({
18+
message: 'Channel type không được bỏ trống',
19+
})
20+
channelTypeId: number;
21+
22+
@IsString()
23+
credentials: string;
24+
25+
@IsBoolean()
26+
active: boolean | false;
27+
}

server/src/interfaces/channels.interface.ts

Whitespace-only changes.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { ENDPOINTS } from '@/constants';
2+
import { ChannelController } from '@/controllers/channels.controller';
3+
import { channelDTO } from '@/dtos/channels.dto';
4+
import { validate } from '@/middlewares/validation.middleware';
5+
import type { Routes } from '@interfaces/routes.interface';
6+
import { Router } from 'express';
7+
export class ChannelRoute implements Routes {
8+
public router: Router = Router();
9+
public controller = new ChannelController();
10+
11+
constructor() {
12+
this.initializeRoutes();
13+
}
14+
initializeRoutes() {
15+
this.router.post(
16+
ENDPOINTS.CHANNEL,
17+
validate(channelDTO, 'body'),
18+
this.controller.createChannel
19+
);
20+
}
21+
}

server/src/server.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ import { App } from '@/app';
22
import { AuthRoute } from '@routes/auth.route';
33
import { ValidateEnv } from '@/utils/validate-env';
44
import { UploadRoute } from './routes/upload.route';
5+
import { ChannelRoute } from './routes/channels.route';
56

67
ValidateEnv();
78

8-
const app = new App([new AuthRoute(), new UploadRoute()]);
9+
const app = new App([
10+
new AuthRoute(),
11+
new UploadRoute(),
12+
new ChannelRoute(),
13+
]);
914

1015
app.listen();
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { db } from "@/database/db";
2+
import { channels } from "@/database/schema";
3+
import { TNewChannel, TUpdateChannel } from "@/database/types";
4+
import { tryCatch } from "bullmq";
5+
import { eq } from "drizzle-orm";
6+
import { Service } from "typedi";
7+
8+
@Service()
9+
export class ChannelService {
10+
public async create(fields: TNewChannel) {
11+
try {
12+
console.log("start create channel")
13+
console.log(fields);
14+
fields.contactId = fields.contactId && fields.contactId.trim() || null;
15+
const channel = await db.insert(channels).values(fields).returning;
16+
console.log(channel[0]);
17+
18+
return channel[0];
19+
}
20+
catch (error) {
21+
console.log(error);
22+
}
23+
}
24+
25+
public async findOneById(id: number) {
26+
const channel = await db.query.channels.findFirst({
27+
where: eq(channels.id, id),
28+
});
29+
30+
return channel;
31+
}
32+
33+
public async updateById(id: number, fields: TUpdateChannel) {
34+
const channel = await db
35+
.update(channels)
36+
.set(fields)
37+
.where(eq(channels.id, id))
38+
.returning();
39+
40+
return channel[0];
41+
}
42+
}

0 commit comments

Comments
 (0)