2019-02-15 07:25:55 +08:00
|
|
|
import {NextFunction, Request, Response} from 'express';
|
|
|
|
import {ErrorCodes, ErrorDTO} from '../../common/entities/Error';
|
2019-02-16 00:47:09 +08:00
|
|
|
import {ObjectManagers} from '../model/ObjectManagers';
|
2019-03-03 17:30:12 +08:00
|
|
|
import {PersonDTO} from '../../common/entities/PersonDTO';
|
|
|
|
import {PhotoDTO} from '../../common/entities/PhotoDTO';
|
2019-02-15 07:25:55 +08:00
|
|
|
|
|
|
|
|
|
|
|
const LOG_TAG = '[PersonMWs]';
|
|
|
|
|
|
|
|
export class PersonMWs {
|
|
|
|
|
|
|
|
|
2019-03-04 04:17:42 +08:00
|
|
|
public static async updatePerson(req: Request, res: Response, next: NextFunction) {
|
|
|
|
if (!req.params.name) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
req.resultPipe = await ObjectManagers.getInstance()
|
|
|
|
.PersonManager.updatePerson(req.params.name as string,
|
|
|
|
req.body as PersonDTO);
|
|
|
|
return next();
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
return next(new ErrorDTO(ErrorCodes.PERSON_ERROR, 'Error during updating a person', err));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-15 07:25:55 +08:00
|
|
|
public static async listPersons(req: Request, res: Response, next: NextFunction) {
|
|
|
|
try {
|
2019-02-16 00:47:09 +08:00
|
|
|
req.resultPipe = await ObjectManagers.getInstance()
|
2019-02-15 07:25:55 +08:00
|
|
|
.PersonManager.getAll();
|
|
|
|
|
|
|
|
return next();
|
|
|
|
|
|
|
|
} catch (err) {
|
2019-03-04 04:17:42 +08:00
|
|
|
return next(new ErrorDTO(ErrorCodes.PERSON_ERROR, 'Error during listing persons', err));
|
2019-02-15 07:25:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-03 17:30:12 +08:00
|
|
|
public static async addSamplePhotoForAll(req: Request, res: Response, next: NextFunction) {
|
|
|
|
if (!req.resultPipe) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const persons = (req.resultPipe as PersonWithPhoto[]);
|
|
|
|
for (let i = 0; i < persons.length; i++) {
|
|
|
|
persons[i].samplePhoto = await ObjectManagers.getInstance()
|
|
|
|
.PersonManager.getSamplePhoto(persons[i].name);
|
|
|
|
}
|
|
|
|
req.resultPipe = persons;
|
|
|
|
return next();
|
|
|
|
|
|
|
|
} catch (err) {
|
2019-03-04 04:17:42 +08:00
|
|
|
return next(new ErrorDTO(ErrorCodes.PERSON_ERROR, 'Error during adding sample photo for all persons', err));
|
2019-03-03 17:30:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static async removeSamplePhotoForAll(req: Request, res: Response, next: NextFunction) {
|
|
|
|
if (!req.resultPipe) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const persons = (req.resultPipe as PersonWithPhoto[]);
|
|
|
|
for (let i = 0; i < persons.length; i++) {
|
|
|
|
delete persons[i].samplePhoto;
|
|
|
|
}
|
|
|
|
req.resultPipe = persons;
|
|
|
|
return next();
|
|
|
|
|
|
|
|
} catch (err) {
|
2019-03-04 04:17:42 +08:00
|
|
|
return next(new ErrorDTO(ErrorCodes.PERSON_ERROR, 'Error during removing sample photo from all persons', err));
|
2019-03-03 17:30:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-15 07:25:55 +08:00
|
|
|
public static async getSamplePhoto(req: Request, res: Response, next: NextFunction) {
|
|
|
|
if (!req.params.name) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
const name = req.params.name;
|
|
|
|
try {
|
2019-02-16 00:47:09 +08:00
|
|
|
const photo = await ObjectManagers.getInstance()
|
2019-02-15 07:25:55 +08:00
|
|
|
.PersonManager.getSamplePhoto(name);
|
|
|
|
|
|
|
|
if (photo === null) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
req.resultPipe = photo;
|
|
|
|
return next();
|
|
|
|
|
|
|
|
} catch (err) {
|
2019-03-04 04:17:42 +08:00
|
|
|
return next(new ErrorDTO(ErrorCodes.PERSON_ERROR, 'Error during getting sample photo for a person', err));
|
2019-02-15 07:25:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-03-03 17:30:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
export interface PersonWithPhoto extends PersonDTO {
|
|
|
|
samplePhoto: PhotoDTO;
|
|
|
|
}
|