From 90a0f562ea2e1e685f71ecb37a81ed68e46cae0d Mon Sep 17 00:00:00 2001 From: Piotr Biernat Date: Sun, 19 Jun 2022 14:45:38 +0200 Subject: [PATCH] [release] release v1 --- cmd/server/main.go | 4 +- internal/app/definition/article.go | 16 -------- internal/app/definition/category.go | 27 -------------- internal/app/entity/article.go | 15 -------- internal/app/entity/category.go | 18 --------- internal/app/handler/article.go | 34 ----------------- internal/app/handler/category.go | 57 ----------------------------- internal/app/router.go | 9 ----- 8 files changed, 3 insertions(+), 177 deletions(-) delete mode 100644 internal/app/definition/article.go delete mode 100644 internal/app/definition/category.go delete mode 100644 internal/app/entity/article.go delete mode 100644 internal/app/entity/category.go delete mode 100644 internal/app/handler/article.go delete mode 100644 internal/app/handler/category.go diff --git a/cmd/server/main.go b/cmd/server/main.go index c7deac3..d3b0886 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -2,7 +2,6 @@ package main import ( "context" - "git.pbiernat.dev/golang/rest-api-prototype/internal/app/database" "net" "os" "os/signal" @@ -10,6 +9,7 @@ import ( "git.pbiernat.dev/golang/rest-api-prototype/internal/app" "git.pbiernat.dev/golang/rest-api-prototype/internal/app/config" + "git.pbiernat.dev/golang/rest-api-prototype/internal/app/database" "git.pbiernat.dev/golang/rest-api-prototype/internal/app/handler" ) @@ -26,6 +26,8 @@ func main() { httpAddr := net.JoinHostPort(config.GetEnv("SERVER_IP", defHttpIp), config.GetEnv("SERVER_PORT", defHttpPort)) dbConnStr := config.GetEnv("DATABASE_URL", defDbUrl) + //fmt.Println(dbConnStr) + //os.Exit(1) dbc, err := database.Connect(dbConnStr) if err != nil { diff --git a/internal/app/definition/article.go b/internal/app/definition/article.go deleted file mode 100644 index a6b2800..0000000 --- a/internal/app/definition/article.go +++ /dev/null @@ -1,16 +0,0 @@ -package definition - -import "git.pbiernat.dev/golang/rest-api-prototype/internal/app/entity" - -type CreateArticleRequest struct { - CategoryID int `json:"category_id"` - Title string `json:"title"` - Intro string `json:"intro"` - Text string `json:"text"` -} - -type CreateArticleResponse struct { - Status string `json:"status"` - Data *entity.Article `json:"data"` - Err string `json:"err,omitempty"` -} diff --git a/internal/app/definition/category.go b/internal/app/definition/category.go deleted file mode 100644 index 09804ca..0000000 --- a/internal/app/definition/category.go +++ /dev/null @@ -1,27 +0,0 @@ -package definition - -import ( - "git.pbiernat.dev/golang/rest-api-prototype/internal/app/entity" - validation "github.com/go-ozzo/ozzo-validation" -) - -type CreateCategoryRequest struct { - Name string `json:"name"` -} - -func (c CreateCategoryRequest) Validate() error { - return validation.ValidateStruct(&c, - validation.Field(&c.Name, validation.Required, validation.Length(3, 255)), - ) -} - -type CreateCategoryResponse struct { - Data *entity.Category `json:"data"` - Err string `json:"err,omitempty"` // FIXME: omitempty on/off? -} - -type DeleteCategoryRequest struct { -} - -type DeleteCategoryResponse struct { -} diff --git a/internal/app/entity/article.go b/internal/app/entity/article.go deleted file mode 100644 index dbc3089..0000000 --- a/internal/app/entity/article.go +++ /dev/null @@ -1,15 +0,0 @@ -package entity - -import ( - "time" -) - -type Article struct { - ID int `json:"id"` - CategoryID int `json:"category_id"` - Title string `json:"title"` - Intro string `json:"intro"` - Text string `json:"text"` - CreateDate time.Time `json:"create_date"` - ModifyDate time.Time `json:"modify_date"` -} diff --git a/internal/app/entity/category.go b/internal/app/entity/category.go deleted file mode 100644 index a91ff4c..0000000 --- a/internal/app/entity/category.go +++ /dev/null @@ -1,18 +0,0 @@ -package entity - -import ( - "time" -) - -type Category struct { - ID int `json:"id"` - Name string `json:"name"` - CreateDate time.Time `json:"create_date"` - ModifyDate time.Time `json:"modify_date"` // FIXME: zero-value issue -} - -// func (c Category) Validate() error { -// return validation.ValidateStruct(&c, -// validation.Field(&c.Name, validation.Required, validation.Length(3, 255)), -// ) -// } diff --git a/internal/app/handler/article.go b/internal/app/handler/article.go deleted file mode 100644 index fc2500d..0000000 --- a/internal/app/handler/article.go +++ /dev/null @@ -1,34 +0,0 @@ -package handler - -import ( - "log" - "net/http" - "time" - - def "git.pbiernat.dev/golang/rest-api-prototype/internal/app/definition" - "git.pbiernat.dev/golang/rest-api-prototype/internal/app/entity" -) - -var CreateArticleHandler *Handler - -func init() { - CreateArticleHandler = &Handler{ - Handle: CreateArticleHandlerFunc, - Request: &def.CreateArticleRequest{}, - Response: &def.CreateArticleResponse{}, - } -} - -func CreateArticleHandlerFunc(h *Handler, w http.ResponseWriter) (interface{}, int, error) { - var art = h.Request.(*def.CreateArticleRequest) - log.Println(art) - - return &entity.Article{ - ID: 1, - CategoryID: 1, - Title: "Dummy article", - Intro: "Intro", - Text: "Text", - CreateDate: time.Now(), - }, http.StatusCreated, nil -} diff --git a/internal/app/handler/category.go b/internal/app/handler/category.go deleted file mode 100644 index b4e5723..0000000 --- a/internal/app/handler/category.go +++ /dev/null @@ -1,57 +0,0 @@ -package handler - -import ( - "log" - "net/http" - "strconv" - "time" - - def "git.pbiernat.dev/golang/rest-api-prototype/internal/app/definition" - "git.pbiernat.dev/golang/rest-api-prototype/internal/app/entity" -) - -var CreateCategoryHandler *Handler -var DeleteCategoryHandler *Handler - -func init() { - CreateCategoryHandler = &Handler{ - Handle: CreateCategoryHandlerFunc, - Request: &def.CreateCategoryRequest{}, - Response: &def.CreateCategoryResponse{}, - } - - DeleteCategoryHandler = &Handler{ - Handle: DeleteCategoryHandlerFunc, - Request: &def.DeleteCategoryRequest{}, - Response: &def.DeleteCategoryResponse{}, - } -} - -func CreateCategoryHandlerFunc(h *Handler, w http.ResponseWriter) (interface{}, int, error) { - var cat = h.Request.(*def.CreateCategoryRequest) - log.Println("Cat input:", cat) - - if err := cat.Validate(); err != nil { - log.Println("Create category validation errors:", err) - return nil, http.StatusUnprocessableEntity, err - } - - return &entity.Category{ - Name: cat.Name, - CreateDate: time.Now(), - }, http.StatusCreated, nil -} - -func DeleteCategoryHandlerFunc(h *Handler, w http.ResponseWriter) (interface{}, int, error) { - var cat = h.Request.(*def.DeleteCategoryRequest) - log.Println(cat) - - id, _ := strconv.Atoi(h.Params["id"]) - log.Println(h.Params) - - if id != 1 { - return nil, http.StatusNotFound, nil - } - - return nil, http.StatusNoContent, nil -} diff --git a/internal/app/router.go b/internal/app/router.go index 59852c7..8545265 100644 --- a/internal/app/router.go +++ b/internal/app/router.go @@ -4,7 +4,6 @@ import ( "net/http" "git.pbiernat.dev/golang/rest-api-prototype/internal/app/handler" - "git.pbiernat.dev/golang/rest-api-prototype/internal/app/service" "github.com/gorilla/mux" ) @@ -23,13 +22,5 @@ func SetupRouter(env *handler.Env) *mux.Router { auth := r.PathPrefix("/auth").Subrouter() auth.Handle("/login", handler.Init(env, handler.AuthLoginHandler)).Methods(http.MethodPost) - api := r.PathPrefix("/api").Subrouter() - api.Use(service.AuthService.ValidateUserTokenMiddleware) // only /api/** endpoints use this middleware - - api.Handle("/article", handler.Init(env, handler.CreateArticleHandler)).Methods(http.MethodPost) - - api.Handle("/category", handler.Init(env, handler.CreateCategoryHandler)).Methods(http.MethodPost) - api.Handle("/category/{id:[0-9]+}", handler.Init(env, handler.DeleteCategoryHandler)).Methods(http.MethodDelete) - return r }