diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/go-rest-api-homework.iml b/.idea/go-rest-api-homework.iml new file mode 100644 index 0000000..25ed3f6 --- /dev/null +++ b/.idea/go-rest-api-homework.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..639900d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..5002cd0 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/go.mod b/go.mod index 823e921..8782693 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.1.0 diff --git a/go.sum b/go.sum index b2b392c..823cdbb 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,2 @@ -github.com/go-chi/chi/v5 v5.0.10 h1:rLz5avzKpjqxrYwXNfmjkrYYXOyLJd37pz53UFHC6vk= -github.com/go-chi/chi/v5 v5.0.10/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= +github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= +github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= diff --git a/precode.go b/precode.go index d082d83..7853a89 100644 --- a/precode.go +++ b/precode.go @@ -1,10 +1,11 @@ package main import ( + "bytes" + "encoding/json" "fmt" - "net/http" - "github.com/go-chi/chi/v5" + "net/http" ) // Task ... @@ -40,13 +41,82 @@ 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 createTask(w http.ResponseWriter, r *http.Request) { + var newTask 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(), &newTask); err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + + if newTask.ID == "" || newTask.Description == "" { + http.Error(w, "Не указан ID или Description", http.StatusBadRequest) + return + } + + tasks[newTask.ID] = newTask + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusCreated) +} + +func getTaskByID(w http.ResponseWriter, r *http.Request) { + taskID := chi.URLParam(r, "id") + task, ok := tasks[taskID] + 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 deleteTaskByID(w http.ResponseWriter, r *http.Request) { + taskID := chi.URLParam(r, "id") + _, ok := tasks[taskID] + if !ok { + http.Error(w, "Таск не найден", http.StatusBadRequest) + return + } + + delete(tasks, taskID) + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) +} func main() { r := chi.NewRouter() // здесь регистрируйте ваши обработчики - // ... + r.Get("/tasks", getTasks) + r.Post("/tasks", createTask) + r.Get("/tasks/{id}", getTaskByID) + r.Delete("/tasks/{id}", deleteTaskByID) if err := http.ListenAndServe(":8080", r); err != nil { fmt.Printf("Ошибка при запуске сервера: %s", err.Error())