Skip to content

Commit

Permalink
Merge pull request #19 from Roger13579/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
y0000ga authored May 12, 2024
2 parents 615d814 + 988c2ca commit 2c7df3c
Show file tree
Hide file tree
Showing 21 changed files with 300 additions and 1,479 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
FROM node:latest
ENV TZ=Asia/Taipei
RUN ln -snf /usr/share/zoneinfo/${TZ} /etc/localtime && echo ${TZ} > /etc/timezone
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
RUN npm run swagger
EXPOSE 3000
CMD ["npm","start"]
2 changes: 1 addition & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { router } from './routes/router';
import connection from './config/dbConnection';
import { globalMiddleware } from './middleware/globalMiddleware';
import swaggerUi from 'swagger-ui-express';
import swaggerFile from './swagger-output.json';
import swaggerFile from './dist/swagger-output.json';
import passport from 'passport';
import { AppError } from './utils/errorHandler';
import { DefaultException } from './utils/defaultException';
Expand Down
12 changes: 11 additions & 1 deletion src/controller/baseController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { CustomResponseType } from '../types/customResponseType';
import { ResponseObject } from '../utils/responseObject';
import { Request } from 'express';
import { validationResult } from 'express-validator';

export abstract class BaseController {
public formatResponse(
Expand All @@ -12,7 +14,15 @@ export abstract class BaseController {
message: message,
data: data,
};

return new ResponseObject(options);
}
public paramVerify(req: Request): ResponseObject | void {
const result = validationResult(req);
if (!result.isEmpty()) {
return this.formatResponse(
result.array()[0].msg,
CustomResponseType.FORMAT_ERROR,
);
}
}
}
26 changes: 26 additions & 0 deletions src/controller/groupController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Request } from 'express';
import { BaseController } from './baseController';
import { CustomResponseType } from '../types/customResponseType';
import { ResponseObject } from '../utils/responseObject';
import { CreateGroupDto } from '../dto/createGroupDto';
import { GroupService } from '../service/groupService';
import { IGroup } from '../models/group';

export class GroupController extends BaseController {
private readonly groupService = new GroupService();

public createGroup = async (req: Request): Promise<ResponseObject> => {
this.paramVerify(req);
const createGroupDto = new CreateGroupDto(req);
const group = (await this.groupService.createGroup(
createGroupDto,
)) as IGroup;
return this.formatResponse(
CustomResponseType.OK_MESSAGE,
CustomResponseType.OK,
{
groupId: group.id,
},
);
};
}
11 changes: 0 additions & 11 deletions src/controller/indexController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { NextFunction, Request, Response } from 'express';
import { BaseController } from './baseController';
import { ResponseObject } from '../utils/responseObject';
import { CustomResponseType } from '../types/customResponseType';
import { validationResult } from 'express-validator';
import bcrypt from 'bcrypt';
import { LoginVo } from '../vo/loginVo';
import { UserService } from '../service/userService';
Expand Down Expand Up @@ -106,16 +105,6 @@ class IndexController extends BaseController {
);
}
};

private paramVerify(req: Request) {
const result = validationResult(req);
if (!result.isEmpty()) {
return this.formatResponse(
result.array()[0].msg,
CustomResponseType.FORMAT_ERROR,
);
}
}
}

export default IndexController;
2 changes: 1 addition & 1 deletion src/controller/userController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NextFunction, Request, Response } from 'express';
import { Request } from 'express';
import { BaseController } from './baseController';
import { CustomResponseType } from '../types/customResponseType';
import { ResponseObject } from '../utils/responseObject';
Expand Down
27 changes: 27 additions & 0 deletions src/dto/createGroupDto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { parseDate } from '../utils/common';
import { Types } from 'mongoose';
import { GroupStatus, TCreateGroupReq } from '../types/group.type';

export class CreateGroupDto {
private readonly userId: Types.ObjectId;
private readonly title: string;
private readonly theater: string;
private readonly movieTitle: string;
private readonly time: Date;
private readonly amount: number;
private readonly haveTicket: boolean;
private readonly content?: string;
private readonly status: string;

constructor(req: TCreateGroupReq) {
this.userId = new Types.ObjectId((req.user as any).id as string);
this.title = req.body.title;
this.theater = req.body.theater;
this.movieTitle = req.body.movieTitle;
this.time = parseDate('time', req.body.time.toString()) as Date;
this.amount = req.body.amount;
this.haveTicket = req.body.haveTicket;
this.content = req.body.content;
this.status = GroupStatus.ongoing;
}
}
5 changes: 5 additions & 0 deletions src/models/baseModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Schema } from 'mongoose';

export interface BaseModel extends Document {
id: Schema.Types.ObjectId;
}
7 changes: 3 additions & 4 deletions src/models/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { Schema, model } from 'mongoose';
import { ITimestamp } from '../types/common.type';
import { schemaOption } from '../utils/constants';
import { GroupStatus } from '../types/group.type';
import { BaseModel } from './baseModel';

interface IGroup extends Document, ITimestamp {
export interface IGroup extends Document, ITimestamp, BaseModel {
userId: Schema.Types.ObjectId;
title: string;
theater: string;
Expand Down Expand Up @@ -69,6 +70,4 @@ const schema = new Schema<IGroup>(
schemaOption,
);

const GroupModel = model<IGroup>('Group', schema);

export default GroupModel;
export const GroupModel = model<IGroup>('Group', schema);
8 changes: 8 additions & 0 deletions src/repository/groupRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { CreateGroupDto } from '../dto/createGroupDto';
import { GroupModel, IGroup } from '../models/group';

export class GroupRepository {
public async createGroup(createGroupDto: CreateGroupDto): Promise<IGroup> {
return GroupModel.create(new GroupModel(createGroupDto));
}
}
6 changes: 6 additions & 0 deletions src/routes/baseRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ResponseObject } from '../utils/responseObject';
import { BaseController } from '../controller/baseController';
import { HttpStatus } from '../types/responseType';
import log4js from '../config/log4js';
import { PipeBase } from '../validator/pipe.base';
const logger = log4js.getLogger(`BaseRoute`);

export abstract class BaseRoute {
Expand All @@ -23,6 +24,11 @@ export abstract class BaseRoute {
return this.prefix;
}

protected usePipe(prototype: any): any[] {
const pipe = new prototype();
return (pipe as PipeBase).transform();
}

protected responseHandler(
method: (
req: Request,
Expand Down
47 changes: 47 additions & 0 deletions src/routes/groupRoute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { BaseRoute } from './baseRoute';
import { GroupController } from '../controller/groupController';
import { UserVerify } from '../middleware/userVerify';
import { CreateGroupPipe } from '../validator/createGroup.pipe';

export class GroupRoute extends BaseRoute {
protected controller!: GroupController;

constructor() {
super();
this.initial();
}

protected initial(): void {
this.controller = new GroupController();
this.setRouters();
}

protected setRouters(): void {
this.router.post(
'/v1/group',
/**
* #swagger.tags = ['Group']
* #swagger.summary = '建立揪團'
*/
/*
#swagger.parameters['obj'] ={
in:'body',
description:'欲建立的揪團資料',
schema:{
$ref:"#/definitions/CustomCreateGroupObj"
}
}
*/
/**
#swagger.responses[200] = {
description: 'OK',
schema: {
$ref: '#/definitions/CreateGroupSuccess' }
}
*/
UserVerify,
this.usePipe(CreateGroupPipe),
this.responseHandler(this.controller.createGroup),
);
}
}
2 changes: 2 additions & 0 deletions src/routes/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { IndexRoute } from './indexRoute';
import { UserRoute } from './userRoute';
import { BaseRoute } from './baseRoute';
import { ProductRoute } from './productRoute';
import { GroupRoute } from './groupRoute';

export const router: Array<BaseRoute> = [
new IndexRoute(),
new UserRoute(),
new ProductRoute(),
new GroupRoute(),
];
23 changes: 23 additions & 0 deletions src/service/groupService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { CustomResponseType } from '../types/customResponseType';
import { throwError } from '../utils/errorHandler';
import log4js from '../config/log4js';
import { CreateGroupDto } from '../dto/createGroupDto';
import { GroupRepository } from '../repository/groupRepository';
import { IGroup } from '../models/group';
const logger = log4js.getLogger(`GroupService`);

export class GroupService {
private readonly groupRepository: GroupRepository = new GroupRepository();

public async createGroup(
createGroupDto: CreateGroupDto,
): Promise<IGroup | void> {
return this.groupRepository.createGroup(createGroupDto).catch((err) => {
logger.error('create user error', err);
throwError(
CustomResponseType.INSERT_ERROR_MESSAGE,
CustomResponseType.INSERT_ERROR,
);
});
}
}
Loading

0 comments on commit 2c7df3c

Please sign in to comment.