From 62c90340eec738caa100674e93c74f9eea42ee5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=B0=D0=B2=D0=B5=D0=BB=20=D0=A1=D0=B0=D1=88=D0=BA?= =?UTF-8?q?=D0=BE=D0=B2?= Date: Fri, 18 Oct 2024 18:07:26 +0300 Subject: [PATCH 1/3] =?UTF-8?q?=D0=97=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5?= =?UTF-8?q?=20=D0=B2=D1=8B=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5=D0=BD=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 2 +- precode.go | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 823e921..0e92922 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module github.com/Yandex-Practicum/go-rest-api-homework go 1.20 -require github.com/go-chi/chi/v5 v5.0.10 // indirect +require github.com/go-chi/chi/v5 v5.0.10 diff --git a/precode.go b/precode.go index d082d83..2eb44c0 100644 --- a/precode.go +++ b/precode.go @@ -1,13 +1,15 @@ package main import ( + "bytes" + "encoding/json" "fmt" "net/http" "github.com/go-chi/chi/v5" ) -// Task ... +// Task структура для определения задач type Task struct { ID string `json:"id"` Description string `json:"description"` @@ -40,13 +42,79 @@ var tasks = map[string]Task{ } // Ниже напишите обработчики для каждого эндпоинта -// ... +func getTasks(w http.ResponseWriter, r *http.Request) { + resp, err := json.Marshal(tasks) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + w.Write(resp) +} + +func postTask(w http.ResponseWriter, r *http.Request) { + var task Task + var buf bytes.Buffer + + _, err := buf.ReadFrom(r.Body) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + + if err = json.Unmarshal(buf.Bytes(), &task); err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + tasks[task.ID] = task + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusCreated) +} + +func getTask(w http.ResponseWriter, r *http.Request) { + id := chi.URLParam(r, "id") + task, ok := tasks[id] + if !ok { + http.Error(w, "", http.StatusBadRequest) + return + } + + resp, err := json.Marshal(task) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + w.Write(resp) +} + +func delTask(w http.ResponseWriter, r *http.Request) { + id := chi.URLParam(r, "id") + + task, ok := tasks[id] + if !ok { + http.Error(w, "", http.StatusBadRequest) + return + } + + delete(tasks, task.ID) + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) +} func main() { r := chi.NewRouter() // здесь регистрируйте ваши обработчики - // ... + r.Get("/tasks/{id}", getTask) + r.Get("/tasks", getTasks) + r.Post("/tasks", postTask) + r.Delete("/tasks/{id}", delTask) if err := http.ListenAndServe(":8080", r); err != nil { fmt.Printf("Ошибка при запуске сервера: %s", err.Error()) From 38cae43bf4bb7831eb7a420b27f1cd1e5de1fb19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=B0=D0=B2=D0=B5=D0=BB=20=D0=A1=D0=B0=D1=88=D0=BA?= =?UTF-8?q?=D0=BE=D0=B2?= Date: Fri, 18 Oct 2024 19:16:51 +0300 Subject: [PATCH 2/3] =?UTF-8?q?=D0=9E=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=D0=BB=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- precode.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/precode.go b/precode.go index 2eb44c0..81ad998 100644 --- a/precode.go +++ b/precode.go @@ -50,7 +50,14 @@ func getTasks(w http.ResponseWriter, r *http.Request) { } w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) - w.Write(resp) + + _, err = w.Write(resp) + if err != nil { + fmt.Println("Ошибка при записи ответа:", err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + } func postTask(w http.ResponseWriter, r *http.Request) { @@ -67,6 +74,11 @@ func postTask(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusBadRequest) return } + _, ok := tasks[task.ID] + if ok { + http.Error(w, "Задача с таким ID уже cуществует", http.StatusBadRequest) + return + } tasks[task.ID] = task w.Header().Set("Content-Type", "application/json") @@ -83,13 +95,18 @@ func getTask(w http.ResponseWriter, r *http.Request) { resp, err := json.Marshal(task) if err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) + http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) - w.Write(resp) + _, err = w.Write(resp) + if err != nil { + fmt.Println("Ошибка при записи ответа:", err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } } func delTask(w http.ResponseWriter, r *http.Request) { @@ -97,7 +114,7 @@ func delTask(w http.ResponseWriter, r *http.Request) { task, ok := tasks[id] if !ok { - http.Error(w, "", http.StatusBadRequest) + http.Error(w, "Задачи с таким ID не существует", http.StatusBadRequest) return } From a47f40c33478eeda59c1c8aeb27d56bb28f60442 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=B0=D0=B2=D0=B5=D0=BB=20=D0=A1=D0=B0=D1=88=D0=BA?= =?UTF-8?q?=D0=BE=D0=B2?= Date: Fri, 18 Oct 2024 19:22:18 +0300 Subject: [PATCH 3/3] =?UTF-8?q?=D0=A3=D0=BB=D1=83=D1=87=D1=88=D0=B8=D0=BB?= =?UTF-8?q?=20=D0=BE=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=BA=D1=83=20?= =?UTF-8?q?=D0=BE=D1=88=D0=B8=D0=B1=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- precode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/precode.go b/precode.go index 81ad998..2c2c4ae 100644 --- a/precode.go +++ b/precode.go @@ -89,7 +89,7 @@ func getTask(w http.ResponseWriter, r *http.Request) { id := chi.URLParam(r, "id") task, ok := tasks[id] if !ok { - http.Error(w, "", http.StatusBadRequest) + http.Error(w, "Задачи с таким ID не существует", http.StatusBadRequest) return }