This repository was archived by the owner on Feb 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathhandler_dept.go
More file actions
127 lines (100 loc) · 4.76 KB
/
Copy pathhandler_dept.go
File metadata and controls
127 lines (100 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package httpservice
import (
"context"
"fmt"
"net/http"
"strconv"
"github.com/gorilla/mux"
myctx "github.com/romapres2010/httpserver/ctx"
myerror "github.com/romapres2010/httpserver/error"
mylog "github.com/romapres2010/httpserver/log"
)
// GetDeptHandler handle JSON for GetDept
func (s *Service) GetDeptHandler(w http.ResponseWriter, r *http.Request) {
mylog.PrintfDebugMsg("START ==================================================================================")
// Запускаем типовой process, возврат ошибки игнорируем
_ = s.process("GET", w, r, func(ctx context.Context, requestBuf []byte, buf []byte) ([]byte, Header, int, error) {
reqID := myctx.FromContextRequestID(ctx) // RequestID передается через context
mylog.PrintfDebugMsg("START: reqID", reqID)
// Считаем PK из URL запроса и проверим на число
vars := mux.Vars(r)
idStr := vars["id"]
id, err := strconv.Atoi(idStr)
if err != nil {
return nil, nil, http.StatusBadRequest, myerror.WithCause("8001", "Failed to process parameter 'id' invalid number: reqID, id", err, reqID, idStr).PrintfInfo()
}
// вызываем JSON сервис, передаем ему буфер для копирования
responseBuf, err := s.jsonService.GetDept(ctx, id, buf)
if err != nil {
return nil, nil, http.StatusInternalServerError, err
}
// Если данные не найдены
if responseBuf == nil {
return nil, nil, http.StatusNotFound, nil
}
// формируем ответ
header := Header{}
header["Content-Type"] = "application/json; charset=utf-8"
header["Errcode"] = "0"
header["RequestID"] = fmt.Sprintf("%v", reqID)
mylog.PrintfDebugMsg("SUCCESS: reqID", reqID)
return responseBuf, header, http.StatusOK, nil
})
mylog.PrintfDebugMsg("SUCCESS ==================================================================================")
}
// CreateDeptHandler handle JSON for CreateDept
func (s *Service) CreateDeptHandler(w http.ResponseWriter, r *http.Request) {
mylog.PrintfDebugMsg("START ==================================================================================")
// Запускаем типовой process, возврат ошибки игнорируем
_ = s.process("POST", w, r, func(ctx context.Context, requestBuf []byte, buf []byte) ([]byte, Header, int, error) {
reqID := myctx.FromContextRequestID(ctx) // RequestID передается через context
mylog.PrintfDebugMsg("START: reqID", reqID)
// вызываем JSON сервис
id, responseBuf, err := s.jsonService.CreateDept(ctx, requestBuf, buf)
if err != nil {
return nil, nil, http.StatusInternalServerError, err
}
// формируем ответ
header := Header{}
header["Content-Type"] = "application/json; charset=utf-8"
header["Errcode"] = "0"
header["Id"] = fmt.Sprintf("%v", id)
header["RequestID"] = fmt.Sprintf("%v", reqID)
mylog.PrintfDebugMsg("SUCCESS: reqID", reqID)
return responseBuf, header, http.StatusOK, nil
})
mylog.PrintfDebugMsg("SUCCESS ==================================================================================")
}
// UpdateDeptHandler handle JSON for UpdateDept
func (s *Service) UpdateDeptHandler(w http.ResponseWriter, r *http.Request) {
mylog.PrintfDebugMsg("START ==================================================================================")
// Запускаем типовой process, возврат ошибки игнорируем
_ = s.process("PUT", w, r, func(ctx context.Context, requestBuf []byte, buf []byte) ([]byte, Header, int, error) {
reqID := myctx.FromContextRequestID(ctx) // RequestID передается через context
mylog.PrintfDebugMsg("START: reqID", reqID)
// Считаем параметры и проверим на число
vars := mux.Vars(r)
idStr := vars["id"]
id, err := strconv.Atoi(idStr)
if err != nil {
return nil, nil, http.StatusBadRequest, myerror.WithCause("8001", "Failed to process parameter 'id' invalid number: reqID, id", err, reqID, idStr).PrintfInfo()
}
// вызываем JSON сервис
responseBuf, err := s.jsonService.UpdateDept(ctx, id, requestBuf, buf)
if err != nil {
return nil, nil, http.StatusInternalServerError, err
}
// Если данные не найдены
if responseBuf == nil {
return nil, nil, http.StatusNotFound, nil
}
// формируем ответ
header := Header{}
header["Content-Type"] = "application/json; charset=utf-8"
header["Errcode"] = "0"
header["RequestID"] = fmt.Sprintf("%v", reqID)
mylog.PrintfDebugMsg("SUCCESS: reqID", reqID)
return responseBuf, header, http.StatusOK, nil
})
mylog.PrintfDebugMsg("SUCCESS ==================================================================================")
}