-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
這次也抓到測試碼的錯誤:go 似乎是依字母順序來執行測試的,所以新增資料的 測試碼會造成資料污染。這裡先把正常 add 的資料獨立出來.確認 add 的程式正 常之後,再另外處理資料獨立的問題
- Loading branch information
Showing
2 changed files
with
52 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
|
||
"git.ronmi.tw/ronmi/sdm" | ||
"github.com/Patrolavia/jsonapi" | ||
) | ||
|
||
type add struct { | ||
M *sdm.Manager | ||
} | ||
|
||
func (h *add) Handle(enc *json.Encoder, dec *json.Decoder, httpData *jsonapi.HTTP) { | ||
var param Order | ||
if err := dec.Decode(¶m); err != nil { | ||
httpData.WriteHeader(http.StatusBadRequest) | ||
enc.Encode("Parameter is not Order object") | ||
return | ||
} | ||
|
||
// validating data | ||
param.Code = strings.ToUpper(strings.TrimSpace(param.Code)) | ||
if len(param.Code) != 3 || param.Local == 0 || param.Foreign == 0 || param.Time <= 0 { | ||
httpData.WriteHeader(http.StatusBadRequest) | ||
enc.Encode("Parameter is not Order object") | ||
return | ||
} | ||
|
||
if _, err := h.M.Insert("orders", param); err != nil { | ||
httpData.WriteHeader(http.StatusInternalServerError) | ||
enc.Encode(fmt.Sprintf("Error saving order: %s", err)) | ||
return | ||
} | ||
|
||
enc.Encode(nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters