import mongoose = require ( 'mongoose' ) ; import RefLecture from './model/RefLecture' ; import RefLectrureNotFoundError from './error/RefLectureNotFoundError' ; let refLectureSchema = new mongoose . Schema ( { classification : String , department : String , academic_year : String , course_title : { type : String , required : true } , credit : Number , class_time : String , class_time_json : [ { day : Number , start : Number , len : Number , place : String } ] , class_time_mask : { type : [ Number ] , required : true , default : [ 0 , 0 , 0 , 0 , 0 , 0 , 0 ] } , instructor : String , quota : Number , enrollment : Number , remark : String , category : String , year : { type : Number , required : true } , semester : { type : Number , required : true } , course_number : { type : String , required : true } , lecture_number : { type : String , required : true } , } ) ; refLectureSchema . index ( { year : 1 , semester : 1 } ) ; refLectureSchema . index ( { course_number : 1 , lecture_number : 1 } ) ; let mongooseModel = mongoose . model ( 'Lecture' , refLectureSchema , 'lectures' ) ; export async function queryWithCourseTitle ( query , courseTitle , limit , offset ) { let firstChar = courseTitle . slice ( 0 , 1 ) ; let docs = await mongooseModel . aggregate ( [ { $match : query } , { $addFields : { _firstChar : { $substrCP : [ "$course_title" , 0 , 1 ] } , _lastChar : { $substrCP : [ "$course_title" , { $subtract : [ { $strLenCP : "$course_title" } , 1 ] } , 1 ] } , _courseTitleLengthExceptWhiteSpace : { $reduce : { input : { $split : [ "$course_title" , " " ] } , initialValue : 0 , in : { $add : [ "$$value" , { $strLenCP : "$$this" } ] } } } , } } , { $addFields : { _firstCharMatches : { $cond : { if : { $eq : [ "$_firstChar" , firstChar ] } , then : 0 , else : 1 } } , _endsWithNumber : { $cond : { if : { $and : [ { $gte : [ { $strcasecmp : [ "$_lastChar" , "0" ] } , 0 ] } , { $lte : [ { $strcasecmp : [ "$_lastChar" , "9" ] } , 0 ] } ] } , then : 0 , else : 1 } } } } , { $sort : { _firstCharMatches : 1 , _courseTitleLengthExceptWhiteSpace : 1 , _endsWithNumber : 1 , course_title : 1 } } , { $skip : offset } , { $limit : limit } ] ) . exec ( ) ; return docs . map ( fromMongoose ) ; } export async function query ( query , limit , offset ) { let docs = await mongooseModel . find ( query ) . sort ( 'course_title' ) . skip ( offset ) . limit ( limit ) . exec ( ) ; return docs . map ( fromMongoose ) ; } export async function findByCourseNumber ( year , semester , courseNumber , lectureNumber ) { let doc = await mongooseModel . findOne ( { 'year' : year , 'semester' : semester , 'course_number' : courseNumber , 'lecture_number' : lectureNumber } ) . lean ( ) . exec ( ) ; return fromMongoose ( doc ) ; } export async function findByMongooseId ( id ) { let doc = await mongooseModel . findOne ( { '_id' : id } ) . exec ( ) ; return fromMongoose ( doc ) ; } export async function findBySemester ( year , semester ) { let docs = await mongooseModel . find ( { year : year , semester : semester } ) . lean ( ) . exec ( ) ; return docs . map ( fromMongoose ) ; } export async function deleteBySemester ( year , semester ) { await mongooseModel . remove ( { year : year , semester : semester } ) . exec ( ) ; } export async function deleteByLectureId ( lectureId ) { await mongooseModel . remove ( { _id : lectureId } ) . exec ( ) ; } export async function insertAll ( lectures ) { let docs = await mongooseModel . insertMany ( lectures ) ; return docs . length ; } export async function partialUpdateRefLecture ( lectureId , lecture ) { let newMongooseDocument = await mongooseModel . findOneAndUpdate ( { "_id" : lectureId } , { $set : lecture } , { new : true } ) . exec ( ) ; if ( ! newMongooseDocument ) { throw new RefLectrureNotFoundError ( ) ; } return fromMongoose ( newMongooseDocument ) ; } function fromMongoose ( mongooseDoc ) { if ( mongooseDoc === null ) return null ; return { _id : mongooseDoc . _id , classification : mongooseDoc . classification , department : mongooseDoc . department , academic_year : mongooseDoc . academic_year , course_title : mongooseDoc . course_title , credit : mongooseDoc . credit , class_time : mongooseDoc . class_time , class_time_json : mongooseDoc . class_time_json , class_time_mask : mongooseDoc . class_time_mask , instructor : mongooseDoc . instructor , quota : mongooseDoc . quota , remark : mongooseDoc . remark , category : mongooseDoc . category , year : mongooseDoc . year , semester : mongooseDoc . semester , course_number : mongooseDoc . course_number , lecture_number : mongooseDoc . lecture_number , } ; }