【问题标题】:How to validate time slot conflicts in my sessions?如何验证我的会话中的时隙冲突?
【发布时间】:2021-07-22 06:05:33
【问题描述】:

我有一个Session模型,它有日期、开始时间和结束时间,如下图 // 假设 Session 模型在同一个文件中定义 const mongoose = require('mongoose')

const SessionSchema = new mongoose.Schema({

    date: {
        type: Date,
        required: [true, 'Date is required for session']
    },

    start: {
        type: Date,
        required: [true, 'Please specify end time for session']
    },

    end: {
      type: Date,
      required: [true, 'Please specify end time for session']
    },

    owner: { type: Schema.Types.ObjectId, ref: 'User' },

    participants: {
      type: [Schema.Types.ObjectId],
      ref: 'User'
    },
 })

module.exports = mongoose.model('Session', SessionSchema)

在我的“/session-create”路由中,我想在创建会话之前确保所有者在会话时间间隔中没有另一个冲突部分。这意味着新会话的时间间隔应该在所有者的任何其他现有会话的时间间隔之外。

我该怎么做?我对如何确保在他们的时间间隔内没有冲突的会话感到困惑。

这是我的会话创建路线:

post("/create-session", (req, res) => {
     const { date, start, end } = req.body
     
     // Getting the start datetime
     startHours = start.slice(0, 2)
     startMinutes = start.slice(3)
     startDateTime = new Date(date).setHours(startHours, startMinutes)

     // Getting the end datetime
     endHours = end.slice(0, 2)
     endMinutes = end.slice(3)
     endDateTime = new Date(date).setHours(endHours, endMinutes)
     
     // Session owner cannot have 2 time-conflicting sessions
     // Session start time should not in be in the 
         // time interval of already existing sessions, i.e,

     // Session end time should not be within the time interval of already 
         // existing sessions
}

如何确保会话所有者在会话创建期间不能有 2 个时间冲突的会话?换句话说,我如何在创建时验证会话开始时间不在 已经存在会话的时间间隔并且会话结束时间不在已经存在会话的时间间隔内?

【问题讨论】:

    标签: node.js express validation mongoose


    【解决方案1】:

    可以查询开始日期小于body.date和结束日期大于body.date的会话是否存在返回错误

    post("/create-session", (req, res) => {
      const { date, start, end } = req.body
    
      // Getting the start datetime
      startHours = start.slice(0, 2)
      startMinutes = start.slice(3)
      startDateTime = new Date(date).setHours(startHours, startMinutes)
    
      // Getting the end datetime
      endHours = end.slice(0, 2)
      endMinutes = end.slice(3)
      endDateTime = new Date(date).setHours(endHours, endMinutes)
      
      const existingSession = await findOne({ $and: [{ startDate: { $lt: startDateTime } }, { endDate: { $gt: startDateTime } }] })
      if (existingSession) return res.status(400).send({err: "not allowed"})
    
      // Session owner cannot have 2 time-conflicting sessions
      // Session start time should not in be in the 
          // time interval of already existing sessions, i.e,
    
      // Session end time should not be within the time interval of already 
          // existing sessions
    })
    

    【讨论】:

    • 很好,但您的回答只考虑了 1 个案例。如果现有会话晚于当前会话开始但在当前会话时间间隔内结束,该怎么办。
    • 另外会话必须由同一用户拥有
    • 您可以使用 $or 为结束时间日期添加相同的验证,并且您应该可以使用 req.user 检查所有者
    猜你喜欢
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-11
    • 1970-01-01
    相关资源
    最近更新 更多