Source: repo/subject.js

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const mongoose = require("mongoose");
const subject_1 = require("./mongoose/model/subject");
const factory = require("../factory");
/**
 * 科目レポジトリー
 */
class MongoRepository {
    constructor(connection) {
        this.subjectModel = connection.model(subject_1.default.modelName);
    }
    static CREATE_MONGO_CONDITIONS(params) {
        // MongoDB検索条件
        const andConditions = [];
        if (params.detailCd !== undefined) {
            andConditions.push({ detailCd: new RegExp(params.detailCd, 'i') });
        }
        return andConditions;
    }
    /**
     * 科目を保管する
     */
    save(params) {
        return __awaiter(this, void 0, void 0, function* () {
            let subject;
            if (params.id === undefined) {
                const doc = yield this.subjectModel.create(Object.assign({}, params.attributes));
                subject = doc.toObject();
            }
            else {
                const doc = yield this.subjectModel.findOneAndUpdate({
                    _id: params.id
                }, params.attributes, { upsert: false, new: true })
                    .exec();
                if (doc === null) {
                    throw new factory.errors.NotFound('Subject');
                }
                subject = doc.toObject();
            }
            return subject;
        });
    }
    countSubject(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const conditions = MongoRepository.CREATE_MONGO_CONDITIONS(params);
            return this.subjectModel.countDocuments({ $and: conditions })
                .setOptions({ maxTimeMS: 10000 })
                .exec();
        });
    }
    findSubjectById(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const doc = yield this.subjectModel.findOne({
                _id: mongoose.Types.ObjectId(params.id)
            }, {
                __v: 0,
                createdAt: 0,
                updatedAt: 0
            })
                .exec();
            if (doc === null) {
                throw new factory.errors.NotFound('Subject');
            }
            return doc.toObject();
        });
    }
    /**
     * 科目を検索する
     */
    searchSubject(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const conditions = MongoRepository.CREATE_MONGO_CONDITIONS(params);
            const query = this.subjectModel.find({ $and: conditions }, {
                __v: 0,
                createdAt: 0,
                updatedAt: 0
            });
            // tslint:disable-next-line:no-single-line-block-comment
            /* istanbul ignore else */
            if (params.limit !== undefined && params.page !== undefined) {
                query.limit(params.limit)
                    .skip(params.limit * (params.page - 1));
            }
            // tslint:disable-next-line:no-single-line-block-comment
            /* istanbul ignore else */
            if (params.sort !== undefined) {
                query.sort(params.sort);
            }
            return query.setOptions({ maxTimeMS: 10000 })
                .exec()
                .then((docs) => docs.map((doc) => doc.toObject()));
        });
    }
    getSubject() {
        return __awaiter(this, void 0, void 0, function* () {
            const query = this.subjectModel.find({});
            return query.setOptions({ maxTimeMS: 10000 })
                .exec()
                .then((docs) => docs.map((doc) => doc.toObject()));
        });
    }
}
exports.MongoRepository = MongoRepository;