Menu
CoddyTech

סיכום – מודלים של REST API

חלק מהיחידה תכנות מונחה עצמים במסלול ה-GO של Coddy. שיעור 78 מתוך 107.

challenge icon

אתגר

קל

בוא נבנה מערכת מודלים של REST API לפלטפורמת בלוגים! תיצור טיפוסים מובנים היטב שמסודרים ל-JSON ומספקים ייצוגים קריאים כמחרוזות — בדיוק מה שצריך כשיוצרים תגובות API אמיתיות.

תארגן את הקוד שלך בשני קבצים:

  • models.go: הגדר את מודלי הנתונים של ה-API עם תגיות JSON מתאימות וייצוגים כמחרוזות.

    צור מבנה Author עם שדות ID (int), Name (string) ו-Email (string). מפה אותם למפתחות JSON "id", "name" ו-"email". ממש את String() כך שיחזיר את התבנית [Name] <[Email]>.

    צור מבנה Post עם ID (int), Title (string), Content (string), Author (מבנה Author מקונן) ו-Published (bool). מפה אותם למפתחות "id", "title", "content", "author" ו-"published". השדה Content צריך להשתמש ב-omitempty כדי שתוכן ריק לא יופיע ב-JSON. ממש את String() כך שיחזיר "[Title]" by [Author's String representation].

    צור מבנה APIResponse עם Success (bool), Message (string) ו-Data (Post). מפה אותם למפתחות "success", "message" ו-"data". השדה Message צריך להשתמש ב-omitempty.

  • main.go: בנה תגובות API והדגם גם פלט JSON וגם ייצוגים כמחרוזות.

    קרא סוג פעולה (json או display), ואז קרא את פרטי הפוסט: מזהה הפוסט, הכותרת, התוכן, מזהה המחבר, שם המחבר, כתובת האימייל של המחבר וסטטוס הפרסום (true או false).

    עבור json: צור APIResponse עם Success: true ועם ה-Post בתור הנתונים (השאר את Message ריק). המר ל-JSON והדפס אותו.

    עבור display: צור את ה-Post והדפס אותו באמצעות מתודת String שלו, ואז הדפס את ה-Author בנפרד בשורה חדשה.

הקלטים הבאים יסופקו:

  • שורה 1: סוג הפעולה (json או display)
  • שורה 2: מזהה הפוסט
  • שורה 3: הכותרת
  • שורה 4: התוכן (עשוי להיות ריק)
  • שורה 5: מזהה המחבר
  • שורה 6: שם המחבר
  • שורה 7: כתובת האימייל של המחבר
  • שורה 8: סטטוס הפרסום (true או false)

לדוגמה, בהינתן:

json
1
Getting Started with Go
Learn the basics of Go programming
101
Jane Smith
jane@example.com
true

הפלט שלך צריך להיות:

{"success":true,"data":{"id":1,"title":"Getting Started with Go","content":"Learn the basics of Go programming","author":{"id":101,"name":"Jane Smith","email":"jane@example.com"},"published":true}}

ובהינתן:

json
2
Draft Post

102
John Doe
john@example.com
false

הפלט שלך צריך להיות:

{"success":true,"data":{"id":2,"title":"Draft Post","author":{"id":102,"name":"John Doe","email":"john@example.com"},"published":false}}

שים לב שהתוכן הריק מושמט מפלט ה-JSON.

ובהינתן:

display
3
Advanced Interfaces
Deep dive into Go interfaces
103
Alice Chen
alice@example.com
true

הפלט שלך צריך להיות:

"Advanced Interfaces" by Alice Chen <alice@example.com>
Alice Chen <alice@example.com>

נסו בעצמכם

package main

import (
	"bufio"
	"encoding/json"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	reader := bufio.NewReader(os.Stdin)

	// קרא את סוג הפעולה
	operation, _ := reader.ReadString('\n')
	operation = strings.TrimSpace(operation)

	// קרא את מזהה הפוסט
	postIDStr, _ := reader.ReadString('\n')
	postID, _ := strconv.Atoi(strings.TrimSpace(postIDStr))

	// קרא את הכותרת
	title, _ := reader.ReadString('\n')
	title = strings.TrimSpace(title)

	// קרא את התוכן
	content, _ := reader.ReadString('\n')
	content = strings.TrimSpace(content)

	// קרא את מזהה המחבר
	authorIDStr, _ := reader.ReadString('\n')
	authorID, _ := strconv.Atoi(strings.TrimSpace(authorIDStr))

	// קרא את שם המחבר
	authorName, _ := reader.ReadString('\n')
	authorName = strings.TrimSpace(authorName)

	// קרא את כתובת האימייל של המחבר
	authorEmail, _ := reader.ReadString('\n')
	authorEmail = strings.TrimSpace(authorEmail)

	// קרא את סטטוס הפרסום
	publishedStr, _ := reader.ReadString('\n')
	published := strings.TrimSpace(publishedStr) == "true"

	// TODO: צור struct מסוג Author עם הערכים שנקראו

	// TODO: צור struct מסוג Post עם הערכים שנקראו ועם ה-Author

	// TODO: טפל בסוג הפעולה
	// אם operation הוא "json":
	//   - צרו APIResponse עם Success: true ועם ה-Post בתור Data
	//   - המירו ל-JSON באמצעות json.Marshal
	//   - הדפיסו את מחרוזת ה-JSON
	// אם operation הוא "display":
	//   - הדפיסו את ה-Post באמצעות המתודה String שלו
	//   - הדפיסו את ה-Author בשורה חדשה

	// קוד זמני לשימוש במשתנים (יש להסיר בעת המימוש)
	_ = postID
	_ = title
	_ = content
	_ = authorID
	_ = authorName
	_ = authorEmail
	_ = published
	_ = operation
	_ = json.Marshal
	fmt.Println("TODO: Implement the solution")
}

כל השיעורים ביחידה תכנות מונחה עצמים

תרגלו בעצמכם: קומפיילר Go אונליין