简体   繁体   English

如何检查两个时间戳之间的范围不与其他两个时间戳重叠

[英]How to check the range between two timestamps is not overlaping two other timestamps

I'm using laravel with mongodb and I've got an overlapping issue when storing new data in database.我将laravelmongodb一起使用,在数据库中存储新数据时遇到了重叠问题。 I want to set this feature for user to store a new post in the database and also set a timer so the post get published at the requested timestamp and also be removed in a specific timestamp in the future.我想为用户设置此功能以将新帖子存储在数据库中并设置一个计时器,以便帖子在请求的时间戳发布,并在将来在特定时间戳中删除。 Everything is managed and tested but the only issue is overlapping.一切都经过管理和测试,但唯一的问题是重叠。

The post data looks like this:帖子数据如下所示:

    "title" : "Some title",
    "body"  : [ dummy body data ... ],
    "tags"  : [ some tags here ...],
    "publish_at"   : "1595740000", //timestamp
    "unpublish_at" : "1595741111"  //timestamp

And I'm storing the data in database:我将数据存储在数据库中:

 /**
 * Store a newly created resource in storage.
 *
 * @param StoreRequest $request
 * @return \Illuminate\Http\JsonResponse
 */
public function store(StoreRequest $request)
{
    $data = $request->validated();

    #Here I want to do the validation of timestamps
    #Using Post::all() I can access all of the posts in the database

    $count = Post::where('name', $data['name'])->count();
    if ($count !== 0) {
        // just a sample validation
        return 'name_already_exists';
    } else {
        Post::Create([
            "title" => $data['title'],
            "body" => $data['body'],
            "tags" => $data['tags'],
            "is_active" => true,
            "publish_at" => Carbon::parse($data['publish_at'])->timestamp,
            "unpublish_at" => Carbon::parse($data['unpublish_at'])->timestamp,
        ]);
        return "resource created";
    }

}

Everything looks good, However, In case the user is adding different posts for being published and unpublished in the future.一切看起来都不错,但是,以防用户添加不同的帖子以供将来发布和未发布。 I need to check the new post timestamps not to overlap any other posts timestamps from database.我需要检查新的帖子时间戳,以免与数据库中的任何其他帖子时间戳重叠。

So far, I think I have to check 3 conditions here,到目前为止,我想我必须在这里检查 3 个条件,

  1. the range between new post timestamps doesn't overlap any of others, The major problem新发布时间戳之间的范围不与其他任何重叠,主要问题
  2. the new post publish_at timestamp be bigger than now(), which I know how to do it新帖子 publish_at 时间戳比 now() 大,我知道该怎么做
  3. the new post unpublish_at timestamp be bigger than publish_at timestamp, which also I know how to do it新帖子 unpublish_at 时间戳大于 publish_at 时间戳,我也知道该怎么做

The Laravel version is 6.18, the Mongodb ORM package is Jenssegers/Mongodb which is pretty similar to eloquent ORM and the Carbon package is also installed. The Laravel version is 6.18, the Mongodb ORM package is Jenssegers/Mongodb which is pretty similar to eloquent ORM and the Carbon package is also installed.

Thank you so much.太感谢了。

the check is straight forward:检查很简单:

if($a->publish_at > $b->unbublish_at || $a->unpublish_at < $b->publish_at)
    return true;

this will give indication that a specific post $a is not overlapping with specific post $b.这将表明特定帖子 $a 与特定帖子 $b 不重叠。 notice that i'm not checking the second and third condition that you mentioned, since you considering them done before.请注意,我没有检查您提到的第二个和第三个条件,因为您之前考虑过它们。 All you need to do is just iterate all over the yet unpublished post and check each one of them for no collision situation with a new post.您需要做的只是遍历尚未发布的帖子并检查每个帖子是否与新帖子发生冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM