-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix models, interfaces, create new controllers for number and phone_t…
…ypes, add services
- Loading branch information
Showing
18 changed files
with
466 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { IDeletedMongoose, IControllers, INumberModel, INumberResponse } from '../interfaces'; | ||
import { OK, INTERNAL_SERVER_ERROR, UNPROCESSABLE_ENTITY, getStatusText } from 'http-status-codes'; | ||
import { createError } from '../utils/error_log'; | ||
import { Request, Response } from 'express'; | ||
|
||
import NumberService from '../services/number'; | ||
|
||
class NumberController { | ||
private NumberService: NumberService = new NumberService(); | ||
public findNumber = async (req: Request, res: Response) => { | ||
try { | ||
if (!req.query.Number) { | ||
return res | ||
.status(UNPROCESSABLE_ENTITY) | ||
.json(createError(getStatusText(UNPROCESSABLE_ENTITY), 'Param Id undefined!')); | ||
} | ||
const PersonCollection = await this.NumberService.findByNumber(req.query.Number); // get number with person data | ||
return res.status(OK).json(PersonCollection); | ||
} catch (error) { | ||
return res | ||
.status(INTERNAL_SERVER_ERROR) | ||
.json(createError(getStatusText(INTERNAL_SERVER_ERROR), error.message, error)); | ||
} | ||
}; | ||
|
||
public create = async (req: Request, res: Response): Promise<Response> => { | ||
try { | ||
const PersonRequest: INumberModel = req.body; | ||
const PersonCollection = await this.NumberService.create(PersonRequest); | ||
return res.status(OK).json(PersonCollection); | ||
} catch (error) { | ||
return res | ||
.status(INTERNAL_SERVER_ERROR) | ||
.json(createError(getStatusText(INTERNAL_SERVER_ERROR), error.message, error)); | ||
} | ||
}; | ||
|
||
public update = async (req: Request, res: Response): Promise<Response> => { | ||
try { | ||
const PersonRequest: INumberModel = req.body; | ||
if (req.params.id) { | ||
PersonRequest._id = req.params.id; | ||
} | ||
const PersonCollection = await this.NumberService.update(PersonRequest); | ||
return res.status(OK).json(PersonCollection); | ||
} catch (error) { | ||
return res | ||
.status(INTERNAL_SERVER_ERROR) | ||
.json(createError(getStatusText(INTERNAL_SERVER_ERROR), error.message, error)); | ||
} | ||
}; | ||
|
||
public delete = async (req: Request, res: Response): Promise<Response> => { | ||
try { | ||
if (!req.params.id) { | ||
return res | ||
.status(UNPROCESSABLE_ENTITY) | ||
.json(createError(getStatusText(UNPROCESSABLE_ENTITY), 'Param Id undefined!')); | ||
} | ||
const deletedRes: IDeletedMongoose = await this.NumberService.delete(req.params.id); | ||
if (deletedRes.n !== 1 || deletedRes.ok !== 1) { | ||
return res | ||
.status(UNPROCESSABLE_ENTITY) | ||
.json(createError(getStatusText(UNPROCESSABLE_ENTITY), 'Wrong data request!')); | ||
} | ||
return res.status(OK).json({ message: 'Deleted!', Id: req.params.id }); | ||
} catch (error) { | ||
return res | ||
.status(INTERNAL_SERVER_ERROR) | ||
.json(createError(getStatusText(INTERNAL_SERVER_ERROR), error.message, error)); | ||
} | ||
}; | ||
} | ||
|
||
export default new NumberController(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { IDeletedMongoose, IControllers, IPhoneTypeModel } from '../interfaces'; | ||
import { OK, INTERNAL_SERVER_ERROR, UNPROCESSABLE_ENTITY, getStatusText } from 'http-status-codes'; | ||
import { createError } from '../utils/error_log'; | ||
import { Request, Response } from 'express'; | ||
|
||
import PhoneTypeService from '../services/phone_types'; | ||
|
||
class PhoneTypeController { | ||
private PhoneTypeService: PhoneTypeService = new PhoneTypeService(); | ||
|
||
public getAll = async (req: Request, res: Response): Promise<Response> => { | ||
try { | ||
const PersonCollection = await this.PhoneTypeService.getAll(); | ||
return res.status(OK).json(PersonCollection); | ||
} catch (error) { | ||
return res | ||
.status(INTERNAL_SERVER_ERROR) | ||
.json(createError(getStatusText(INTERNAL_SERVER_ERROR), error.message, error)); | ||
} | ||
}; | ||
|
||
public create = async (req: Request, res: Response): Promise<Response> => { | ||
try { | ||
const PersonRequest: IPhoneTypeModel = req.body; | ||
const PersonCollection = await this.PhoneTypeService.create(PersonRequest); | ||
return res.status(OK).json(PersonCollection); | ||
} catch (error) { | ||
return res | ||
.status(INTERNAL_SERVER_ERROR) | ||
.json(createError(getStatusText(INTERNAL_SERVER_ERROR), error.message, error)); | ||
} | ||
}; | ||
|
||
public update = async (req: Request, res: Response): Promise<Response> => { | ||
try { | ||
const PersonRequest: IPhoneTypeModel = req.body; | ||
if (req.params.id) { | ||
PersonRequest._id = req.params.id; | ||
} | ||
const PersonCollection = await this.PhoneTypeService.update(PersonRequest); | ||
// this.PersonResponse.Person = this.PersonCollection || []; | ||
return res.status(OK).json(PersonCollection); | ||
} catch (error) { | ||
return res | ||
.status(INTERNAL_SERVER_ERROR) | ||
.json(createError(getStatusText(INTERNAL_SERVER_ERROR), error.message, error)); | ||
} | ||
}; | ||
|
||
public delete = async (req: Request, res: Response): Promise<Response> => { | ||
try { | ||
if (!req.params.id) { | ||
return res | ||
.status(UNPROCESSABLE_ENTITY) | ||
.json(createError(getStatusText(UNPROCESSABLE_ENTITY), 'Param Id undefined!')); | ||
} | ||
const deletedRes: IDeletedMongoose = await this.PhoneTypeService.delete(req.params.id); | ||
if (deletedRes.n !== 1 || deletedRes.ok !== 1) { | ||
return res | ||
.status(UNPROCESSABLE_ENTITY) | ||
.json(createError(getStatusText(UNPROCESSABLE_ENTITY), 'Wrong data request!')); | ||
} | ||
return res.status(OK).json({ message: 'Deleted!', Id: req.params.id }); | ||
} catch (error) { | ||
return res | ||
.status(INTERNAL_SERVER_ERROR) | ||
.json(createError(getStatusText(INTERNAL_SERVER_ERROR), error.message, error)); | ||
} | ||
}; | ||
} | ||
|
||
export default new PhoneTypeController(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,14 @@ | ||
import { Schema, Model, model } from 'mongoose'; | ||
import { INumberModel, IModelClass } from '../interfaces'; | ||
|
||
class NumberModel implements IModelClass<INumberModel> { | ||
public ModelType: Model<INumberModel>; | ||
public ModelSchema: Schema; | ||
public filter: string = '-__v'; // filter auto-import mongoBD __v | ||
constructor() { | ||
this.ModelSchema = new Schema({ | ||
Number: String, | ||
PersonId: { type: Schema.Types.ObjectId, ref: 'Person' }, | ||
Type: { | ||
type: Schema.Types.ObjectId, | ||
ref: 'Phone_type', | ||
}, | ||
}); | ||
this.ModelType = model<INumberModel>('Number', this.ModelSchema); | ||
} | ||
class NumberModel { | ||
public static filter: string = '-__v'; // filter auto-import mongoBD __v | ||
public static readonly ModelSchema: Schema = new Schema({ | ||
Number: String, | ||
_person: { type: Schema.Types.ObjectId, ref: 'Person' }, | ||
_type: { type: Schema.Types.ObjectId, ref: 'Phone_type' }, | ||
}).index({ Number: 'text' }); | ||
public static readonly ModelType: Model<INumberModel> = model<INumberModel>('Number', NumberModel.ModelSchema); | ||
} | ||
|
||
export default NumberModel; |
Oops, something went wrong.