Day24-介接 API(二)Google Calendar(II)Events——Read、Update、Delete

大家好~
昨天成功在日历上新增 Event 了,
今天来对日历的 Event 做其他操作吧~

Read

Read 的部分我们今天会使用两种方式:

  • Events: list
    • 取得多笔 Events
  • Events: get
    • 取得单笔 Event

以下开始我们的实作~

Events: list


$optParams 的部分我选用了以下四个:

  • orderBy
    • Events 的排序依据。
  • singleEvents
    • 预设为 false,若要用 startTime 排序,记得要设成 true 喔~
  • timeMin
    • Events 开始时间的下限,这边我选择使用当天日期。
  • timeMax
    • Events 开始时间的上限,这边我选择为一周後的日期。
public function list()
{
    $response = $this->googleCalendar->events->listEvents(
        $this->calendarId,
        [
            'orderBy' => 'startTime',
            'singleEvents' => true,
            'timeMin' => Carbon::today('Asia/Taipei')->toRfc3339String(),
            'timeMax' => Carbon::today('Asia/Taipei')->addWeek()->toRfc3339String()
        ]
    );

    $collection = collect($response->getItems());

    return $collection->map(function ($item) {
        return [
            'id' => $item->id,
            'summary' => $item->summary,
            'start' => $item->getStart()->getDateTime(),
            'end' => $item->getEnd()->getDateTime(),
        ];
    });
}
成果预览:

测试成功~

Events: get

public function get($eventId)
{
    $eventData = $this->googleCalendar->events->get($this->calendarId, $eventId);

    return [
        'id' => $eventData->getId(),
        'link' => $eventData->getHtmlLink(),
        'summary' => $eventData->getSummary(),
        'description' => $eventData->getDescription(),
        'start' => $eventData->getStart()->getDateTime(),
        'end' => $eventData->getEnd()->getDateTime(),
    ];
}
成果预览:

拿刚刚使用的 Events:list 里的其中一笔 id,
做单笔 Event 的查询,
接下来的范例也会以这笔10月9号的 Event 做示范喔~

Update

Events: patch

Event 只会更新在该次请求中有包含的字段,
其他未在该次请求中之字段则保持不变。

public function patch(Request $request, $eventId)
{
    $calendarEvent = new Event();

    if (isset($request->summary)) {
        $calendarEvent->setSummary($request->summary);
    }

    if (isset($request->description)) {
        $calendarEvent->setDescription($request->description);
    }

    if (isset($request->startTime)) {
        $startTime = new EventDateTime();
        $startTime->setDateTime($request->startTime);

        $calendarEvent->setStart($startTime);
    }


    if (isset($request->endTime)) {
        $endTime = new EventDateTime();
        $endTime->setDateTime($request->endTime);

        $calendarEvent->setEnd($endTime);
    }


    $eventPatch = $this->googleCalendar->events->patch(
        $this->calendarId,
        $eventId,
        $calendarEvent
    );

    return [
        'id' => $eventPatch->getId(),
        'link' => $eventPatch->getHtmlLink(),
        'summary' => $eventPatch->getSummary(),
        'description' => $eventPatch->getDescription(),
        'start' => $eventPatch->getStart()->getDateTime(),
        'end' => $eventPatch->getEnd()->getDateTime(),
    ];
}
成果预览:

这边更新的 Event 和刚刚 Events: get 范例用的 Event 是同一个喔,
从上图可发现 Event 的 summary 更新成功~

Delete

Events: delete

public function delete($eventId)
{
    $this->googleCalendar->events->delete($this->calendarId, $eventId);

    return [
        'message' => 'event has been deleted'
    ];
}
成果预览:

删除成功~
然後再用 Events:list 确认一下。

可以看到10月9号的 Event 已经不见了。

那麽 Events 的 CRUD 都成功啦~
大家明天见!
若文章有任何问题,
还请大家不吝赐教!


<<:  [DAY24] Azure Machine Learning SDK 的 ScriptRunConfig

>>:  【从零开始的Swift开发心路历程-Day27】如何在App中播放影片!

Day-17 Pytorch 的 Linear Regression

在前面我们学习过了 Pytorch 的基础用法,今天我们来正式依照 Pytorch Model C...

D10. 学习基础C、C++语言

D10: 简单的练习UVA(11805) #include <stdio.h> #inc...

Dungeon Mizarka 012

今天一样是要利用Editor Script进行少部份量化的工作,已经了解了怎麽样用Editor Sc...

AWS Lambda 搭配 Amazon API Gateway

AWS Lambda 搭配 Amazon API Gateway AWS Lambda 是一种无服务...

Day 09: 【番外篇】关於写 Code 这件事 (待改进中... )

「42 年里,我什麽都经历过。我被开除过,也被表扬过。我当过小组长、主管、也当过普通员工,甚至当过...