首頁 web前端 js教程 底層設計:輪詢系統 - 邊緣情況

底層設計:輪詢系統 - 邊緣情況

Aug 31, 2024 pm 02:43 PM

Low-Level Design: Polling System - Edge Cases

目錄

案例 1 - 處理更新的版本控制
情況 2 - PollID 作為 UUID 而非主鍵
情況 3 - 選項為空或無效
案例 4 - 重複選項
案例 5 - 問題長度限制
案例 6 - 投票過期

請先參考以下文章:

  1. 底層設計:投票系統:基本

  2. 底層設計:輪詢系統 - 使用 Node.js 和 SQL

邊緣情況處理

案例1

要管理投票問題和選項的更新,同時保留與同一投票 ID 關聯的先前詳細信息,您可以實現版本控制系統。這種方法可讓您追蹤每次民意調查的歷史數據,確保即使在更新後也保留舊的詳細資訊。

第 1 步:資料庫架構更改

  1. 更新投票表

    • 將 current_version_id 欄位新增至 polls 表中以追蹤投票的最新版本。
  2. 建立投票版本表

    • 建立一個新表格來儲存民意調查的歷史版本。

更新的資料庫結構定義

雷雷

第 2 步:API 實施變更

更新輪詢控制器

修改 updatePoll 方法,在建立新版本之前檢查問題是否有變化。

檔案:controllers/pollController.js
雷雷

第 3 步:更新投票路線

確保在 pollRoutes.js 中正確定義路由。

檔案:routes/pollRoutes.js
雷雷

變更摘要

  1. 資料庫:

    • 更新了投票表以包含 current_version_id。
    • 建立了 poll_versions 表來追蹤問題版本。
    • 選項和投票表不變。
  2. API:

    • 建立了一個新的 createPoll 方法來初始化民意調查和版本。
    • 更新了 updatePoll 方法以在建立新版本之前檢查問題變更。
    • 新增了投票和查看投票結果的方法。
  3. 路線:

    • 確保定義所有必要的路由來處理民意調查創建、更新、投票和結果。

案例2

處理 pollId 需要是 UUID(通用唯一識別碼)的場景。

以下是在輪詢系統中為 thepollId 實現 UUID 的步驟,無需提供代碼:

為投票 ID 實施 UUID 的步驟

  1. ** 資料庫架構更新:**

    • 修改 polls、poll_versions、options 和 votes 表,以使用 CHAR(36) 作為 poll_id 而不是整數。
    • 建立一個新的 poll_versions 表來儲存由 UUID 連結的投票問題和選項的歷史版本。
  2. ** UUID 產生:**

    • 決定產生UUID的方法。您可以使用應用程式環境中的庫或內建函數來建立UUID。
  3. ** 建立投票邏輯:**

    • 建立新投票時,產生一個 UUID 並將其用作 poll_id。
    • 將新的投票記錄插入投票表中。
    • 將初始問題插入 poll_versions 表並將其與產生的 UUID 連結。
  4. ** 更新投票邏輯:**

    • 更新投票時:
  5. 檢查問題是否已更改。

    • 如果問題已更改,請在poll_versions 表中建立一個新版本條目來儲存舊問題和選項。
    • 根據需要使用新問題和選項更新投票表。
  6. ** 投票邏輯:**

    • 更新投票機制,確保使用UUID作為poll_id。
  7. 驗證投票請求中提供的 UUID 是否存在於 polls 表中。

  8. ** API 更新:**

    • 修改 API 端點以接受並傳回 poll_id 的 UUID。
    • 確保所有 API 操作(建立、更新、刪除、投票)一致引用 UUID 格式。
  9. ** 檢定:**

    • 徹底測試應用程序,確保在所有場景(建立、更新、投票和檢索投票結果)中正確處理 UUID。
  10. ** 文件:**

    • Update your API documentation to reflect the changes in thepoll_id format and any new behaviors related to versioning and UUID usage.

By following these steps, you can successfully implement UUIDs for pollId in your polling system while ensuring data integrity and historical tracking.


Case 3

Empty or Invalid Options

Validation Approach:

  • API Input Validation: Implement checks in your API endpoints to verify that the options provided in the request body are not empty and meet specific criteria (e.g., no special characters if not allowed).
  • Feedback Mechanism: Provide clear error messages to the user if the options are invalid or empty, guiding them to correct their input.

Case 4

Duplicate Options

Uniqueness Check:

  • Pre-Insert Validation: Before adding options to a poll, check the existing options in the database for duplicates. This can be done by querying the options table using the poll ID and comparing it against the new options.
  • User Feedback: If a duplicate option is detected, return a meaningful error message to inform the user which options are duplicates, allowing them to modify their input accordingly.

Case 5

Question Length Limit

Character Limitation:

  • API Validation: Set a maximum character limit for poll questions and options within your API. This can be done by checking the length of the question and each option during the creation and update processes.
  • User Interface Feedback: Implement client-side validation to provide instant feedback to users when they exceed the character limit while typing, enhancing the user experience.

Case 6

Poll Expiration

Expiration Mechanism:

  • Timestamp Management: Add a timestamp field to the polls table to record when each poll is created and optionally another field for the expiration date.
  • Scheduled Checks: Implement a background job or cron task that periodically checks for expired polls and marks them as inactive in the database. This can also include preventing votes on expired polls.
  • User Notifications: Optionally, notify poll creators and participants of impending expiration dates, allowing them to engage with the poll before it becomes inactive.

Please refer to the following articles first:

  1. Low-Level Design: Polling System: Basic

  2. Low-Level Design: Polling System - Using Node.js & SQL

More Details:

Get all articles related to system design
Hastag: SystemDesignWithZeeshanAli

systemdesignwithzeeshanali

Git: https://github.com/ZeeshanAli-0704/SystemDesignWithZeeshanAli

以上是底層設計:輪詢系統 - 邊緣情況的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
4 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

在JavaScript中替換字符串字符 在JavaScript中替換字符串字符 Mar 11, 2025 am 12:07 AM

JavaScript字符串替換方法詳解及常見問題解答 本文將探討兩種在JavaScript中替換字符串字符的方法:在JavaScript代碼內部替換和在網頁HTML內部替換。 在JavaScript代碼內部替換字符串 最直接的方法是使用replace()方法: str = str.replace("find","replace"); 該方法僅替換第一個匹配項。要替換所有匹配項,需使用正則表達式並添加全局標誌g: str = str.replace(/fi

自定義Google搜索API設置教程 自定義Google搜索API設置教程 Mar 04, 2025 am 01:06 AM

本教程向您展示瞭如何將自定義的Google搜索API集成到您的博客或網站中,提供了比標準WordPress主題搜索功能更精緻的搜索體驗。 令人驚訝的是簡單!您將能夠將搜索限制為Y

構建您自己的Ajax Web應用程序 構建您自己的Ajax Web應用程序 Mar 09, 2025 am 12:11 AM

因此,在這裡,您準備好了解所有稱為Ajax的東西。但是,到底是什麼? AJAX一詞是指用於創建動態,交互式Web內容的一系列寬鬆的技術。 Ajax一詞,最初由Jesse J創造

示例顏色json文件 示例顏色json文件 Mar 03, 2025 am 12:35 AM

本文系列在2017年中期進行了最新信息和新示例。 在此JSON示例中,我們將研究如何使用JSON格式將簡單值存儲在文件中。 使用鍵值對符號,我們可以存儲任何類型的

8令人驚嘆的jQuery頁面佈局插件 8令人驚嘆的jQuery頁面佈局插件 Mar 06, 2025 am 12:48 AM

利用輕鬆的網頁佈局:8 ESTISSEL插件jQuery大大簡化了網頁佈局。 本文重點介紹了簡化該過程的八個功能強大的JQuery插件,對於手動網站創建特別有用

什麼是這個'在JavaScript? 什麼是這個'在JavaScript? Mar 04, 2025 am 01:15 AM

核心要點 JavaScript 中的 this 通常指代“擁有”該方法的對象,但具體取決於函數的調用方式。 沒有當前對象時,this 指代全局對象。在 Web 瀏覽器中,它由 window 表示。 調用函數時,this 保持全局對象;但調用對象構造函數或其任何方法時,this 指代對象的實例。 可以使用 call()、apply() 和 bind() 等方法更改 this 的上下文。這些方法使用給定的 this 值和參數調用函數。 JavaScript 是一門優秀的編程語言。幾年前,這句話可

通過來源查看器提高您的jQuery知識 通過來源查看器提高您的jQuery知識 Mar 05, 2025 am 12:54 AM

jQuery是一個很棒的JavaScript框架。但是,與任何圖書館一樣,有時有必要在引擎蓋下發現發生了什麼。也許是因為您正在追踪一個錯誤,或者只是對jQuery如何實現特定UI感到好奇

10張移動秘籍用於移動開發 10張移動秘籍用於移動開發 Mar 05, 2025 am 12:43 AM

該帖子編寫了有用的作弊表,參考指南,快速食譜以及用於Android,BlackBerry和iPhone應用程序開發的代碼片段。 沒有開發人員應該沒有他們! 觸摸手勢參考指南(PDF)是Desig的寶貴資源

See all articles