Skip to content

Commit

Permalink
feat: add api for wip/commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hunjixin committed Dec 12, 2023
1 parent 279edf9 commit 8f81d68
Show file tree
Hide file tree
Showing 21 changed files with 2,750 additions and 127 deletions.
2 changes: 2 additions & 0 deletions api/api_impl/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ type APIController struct {
controller.VersionController
controller.ObjectController
controller.UserController
controller.WipController
controller.CommitController
}
36 changes: 27 additions & 9 deletions api/custom_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,53 @@ type JiaozifsResponse struct {
http.ResponseWriter
}

func (response *JiaozifsResponse) JSON(v interface{}) {
// JSON convert object to json format and write to response,
// if not specific code, default code is 200. given code will
// overwrite default code, if more than one code, the first one will be used.
func (response *JiaozifsResponse) JSON(v any, code ...int) {
if len(code) == 0 {
response.WriteHeader(http.StatusOK)
} else {
response.WriteHeader(code[0])
}
response.Header().Set("Content-Type", "application/json")
response.WriteHeader(http.StatusOK)
err := json.NewEncoder(response).Encode(v)
err := json.NewEncoder(response.ResponseWriter).Encode(v)
if err != nil {
response.Error(err)
return
}
}

// OK response with 200
func (response *JiaozifsResponse) OK() {
response.WriteHeader(http.StatusOK)
}

// NotFound response with 404
func (response *JiaozifsResponse) NotFound() {
response.WriteHeader(http.StatusNotFound)
}

// Error response with 500 and error message
func (response *JiaozifsResponse) Error(err error) {
response.WriteHeader(http.StatusInternalServerError)
_, _ = response.Write([]byte(err.Error()))
}

func (response *JiaozifsResponse) String(msg string) {
// String response and string
// if not specific code, default code is 200. given code will
// overwrite default code, if more than one code, the first one will be used.
func (response *JiaozifsResponse) String(msg string, code ...int) {
if len(code) == 0 {
response.WriteHeader(http.StatusOK)
} else {
response.WriteHeader(code[0])
}
response.Header().Set("Content-Type", "text/plain;charset=UTF-8")
response.WriteHeader(http.StatusOK)
_, _ = response.Write([]byte(msg))
}

func (response *JiaozifsResponse) CodeMsg(code int, msg string) {
response.WriteHeader(code)
_, _ = response.Write([]byte(msg))
}
// Code response with uncommon code
func (response *JiaozifsResponse) Code(code int) {
response.WriteHeader(code)
}
104 changes: 104 additions & 0 deletions api/custom_response_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package api

import (
"fmt"
"net/http"
"testing"

"go.uber.org/mock/gomock"
)

func TestJiaozifsResponse(t *testing.T) {
t.Run("not found", func(t *testing.T) {
ctrl := gomock.NewController(t)
resp := NewMockResponseWriter(ctrl)
jzResp := JiaozifsResponse{resp}

resp.EXPECT().WriteHeader(http.StatusNotFound)
jzResp.NotFound()
})

t.Run("ok", func(t *testing.T) {
ctrl := gomock.NewController(t)
resp := NewMockResponseWriter(ctrl)
jzResp := JiaozifsResponse{resp}

resp.EXPECT().WriteHeader(http.StatusOK)
jzResp.OK()
})
t.Run("code", func(t *testing.T) {
ctrl := gomock.NewController(t)
resp := NewMockResponseWriter(ctrl)
jzResp := JiaozifsResponse{resp}

resp.EXPECT().WriteHeader(http.StatusCreated)
jzResp.Code(http.StatusCreated)
})
t.Run("error", func(t *testing.T) {
ctrl := gomock.NewController(t)
resp := NewMockResponseWriter(ctrl)
jzResp := JiaozifsResponse{resp}

resp.EXPECT().WriteHeader(http.StatusInternalServerError)
resp.EXPECT().Write([]byte("mock"))
jzResp.Error(fmt.Errorf("mock"))
})

t.Run("string", func(t *testing.T) {
ctrl := gomock.NewController(t)
resp := NewMockResponseWriter(ctrl)
jzResp := JiaozifsResponse{resp}

resp.EXPECT().WriteHeader(http.StatusOK)
resp.EXPECT().Header().DoAndReturn(func() http.Header {
return make(http.Header)
})
resp.EXPECT().Write([]byte("test"))
jzResp.String("test")
})

t.Run("string with code", func(t *testing.T) {
ctrl := gomock.NewController(t)
resp := NewMockResponseWriter(ctrl)
jzResp := JiaozifsResponse{resp}

resp.EXPECT().WriteHeader(http.StatusCreated)
resp.EXPECT().Header().DoAndReturn(func() http.Header {
return make(http.Header)
})
resp.EXPECT().Write([]byte("test"))
jzResp.String("test", http.StatusCreated)
})

t.Run("json", func(t *testing.T) {
ctrl := gomock.NewController(t)
resp := NewMockResponseWriter(ctrl)
jzResp := JiaozifsResponse{resp}

resp.EXPECT().WriteHeader(http.StatusOK)
resp.EXPECT().Header().DoAndReturn(func() http.Header {
return make(http.Header)
})

resp.EXPECT().Write([]byte("{\"Name\":\"aa\"}\n"))
jzResp.JSON(struct {
Name string
}{Name: "aa"})
})
t.Run("json with code", func(t *testing.T) {
ctrl := gomock.NewController(t)
resp := NewMockResponseWriter(ctrl)
jzResp := JiaozifsResponse{resp}

resp.EXPECT().WriteHeader(http.StatusCreated)
resp.EXPECT().Header().DoAndReturn(func() http.Header {
return make(http.Header)
})

resp.EXPECT().Write([]byte("{\"Name\":\"aa\"}\n"))
jzResp.JSON(struct {
Name string
}{Name: "aa"}, http.StatusCreated)
})

}
1 change: 1 addition & 0 deletions api/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
package api

//go:generate go run github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen -package api -templates ./tmpls -generate "types,client,chi-server,spec" -o jiaozifs.gen.go ./swagger.yml
//go:generate go run go.uber.org/mock/mockgen@latest --package=api --destination=resp.gen.go net/http ResponseWriter
Loading

0 comments on commit 8f81d68

Please sign in to comment.