From 3dd20da7235bd4cb14b79591b4315f4dbcbaaa53 Mon Sep 17 00:00:00 2001 From: andrusyakka Date: Wed, 5 Jul 2023 18:03:24 +0300 Subject: [PATCH] add org structure --- config/config.go | 2 +- handler/auth.go | 2 +- handler/organization.go | 31 +++++++++++++++++++++++ handler/position.go | 53 ++++++++++++++++++++++++++++++++++++++++ handler/users.go | 2 +- main.go | 8 +++++- model/organization.go | 10 ++++++++ model/position.go | 11 +++++++++ model/user.go | 17 +++++++------ test.db | Bin 24576 -> 69632 bytes 10 files changed, 125 insertions(+), 11 deletions(-) create mode 100644 handler/organization.go create mode 100644 handler/position.go create mode 100644 model/organization.go create mode 100644 model/position.go diff --git a/config/config.go b/config/config.go index 47c530d..c5a07be 100644 --- a/config/config.go +++ b/config/config.go @@ -16,7 +16,7 @@ type Config struct { func New() *Config { return &Config{ Server: ServerConfig{ - Port: getEnv("PORT", ""), + Port: getEnv("PORT", "8080"), ConnectionString: getEnv("CONNECTION_STRING", "test.db"), }, } diff --git a/handler/auth.go b/handler/auth.go index 92434dc..f16cd83 100644 --- a/handler/auth.go +++ b/handler/auth.go @@ -17,7 +17,7 @@ func (h *Handler) Login(c echo.Context) (err error) { u.Password = fmt.Sprintf("%x", sha256.Sum256([]byte(u.Password))) - var t = h.DB.Where("user_name = ? AND password = ?", u.UserName, u.Password).First(&u) + var t = h.DB.Model(&model.User{}).Preload("Position").Where("user_name = ? AND password = ?", u.UserName, u.Password).First(&u) if t.RowsAffected == 0 { return c.NoContent(http.StatusNotFound) diff --git a/handler/organization.go b/handler/organization.go new file mode 100644 index 0000000..b4b639e --- /dev/null +++ b/handler/organization.go @@ -0,0 +1,31 @@ +package handler + +import ( + "astra-users/model" + "github.com/labstack/echo/v4" + "net/http" +) + +func (h *Handler) CreateOrganization(c echo.Context) (err error) { + organization := new(model.Organization) + + if err = c.Bind(organization); err != nil { + return c.NoContent(http.StatusBadRequest) + } + + tx := h.DB.Create(organization) + + if tx.RowsAffected == 0 { + return c.NoContent(http.StatusBadGateway) + } + + return c.JSON(http.StatusCreated, organization) +} + +func (h *Handler) GetOrganizations(c echo.Context) error { + var organizations []model.Organization + + h.DB.Find(&organizations) + + return c.JSON(http.StatusCreated, organizations) +} diff --git a/handler/position.go b/handler/position.go new file mode 100644 index 0000000..bf2be54 --- /dev/null +++ b/handler/position.go @@ -0,0 +1,53 @@ +package handler + +import ( + "astra-users/model" + "errors" + "github.com/labstack/echo/v4" + "net/http" + "strconv" +) + +func (h *Handler) GetPositionHandler(c echo.Context) error { + id, err := strconv.Atoi(c.Param("id")) + + if err != nil { + return c.NoContent(http.StatusBadRequest) + } + + position, err := h.GetPosition(id) + + if err != nil { + return err + } + + return c.JSON(http.StatusOK, position) +} + +func (h *Handler) GetPosition(id int) (*model.Position, error) { + var position model.Position + + r := h.DB.Find(&position, id) + + if r.RowsAffected == 0 { + return nil, errors.New("position not found") + } + + return &position, nil +} + +func (h *Handler) CreatePosition(c echo.Context) (err error) { + position := new(model.Position) + + if err = c.Bind(position); err != nil { + return c.NoContent(http.StatusBadRequest) + } + + tx := h.DB.Create(position) + + if tx.RowsAffected == 0 { + return c.NoContent(http.StatusBadGateway) + } + + return c.JSON(http.StatusCreated, position) +} diff --git a/handler/users.go b/handler/users.go index c1f02be..6fa0557 100644 --- a/handler/users.go +++ b/handler/users.go @@ -102,7 +102,7 @@ func (h *Handler) CreateUser(c echo.Context) (err error) { user.Password = fmt.Sprintf("%x", sha256.Sum256([]byte(user.Password))) - tx := h.DB.Omit("ID", "CreatedAt", "UpdatedAt", "DeletedAt").Create(user) + tx := h.DB.Create(user) if tx.RowsAffected == 0 { return c.NoContent(http.StatusBadGateway) diff --git a/main.go b/main.go index 832fa7b..0c778cd 100644 --- a/main.go +++ b/main.go @@ -34,7 +34,7 @@ func main() { } e := echo.New() - db.AutoMigrate(&model.User{}) + db.AutoMigrate(&model.User{}, &model.Organization{}, &model.Position{}) e.Use(middleware.Logger()) @@ -49,5 +49,11 @@ func main() { e.DELETE("/:id", h.DeleteUser) e.GET("/health", h.Health) + e.GET("/positions/:id", h.GetPositionHandler) + e.POST("/positions", h.CreatePosition) + + e.GET("/organizations", h.GetOrganizations) + e.POST("/organizations", h.CreateOrganization) + e.Logger.Fatal(e.Start(":" + config.Env.Server.Port)) } diff --git a/model/organization.go b/model/organization.go new file mode 100644 index 0000000..74e9fdf --- /dev/null +++ b/model/organization.go @@ -0,0 +1,10 @@ +package model + +import "gorm.io/gorm" + +type Organization struct { + gorm.Model + Name string `gorm:"unique;index"` + ParentID uint `json:"parent_id"` + Positions []Position `json:"positions,omitempty"` +} diff --git a/model/position.go b/model/position.go new file mode 100644 index 0000000..368e746 --- /dev/null +++ b/model/position.go @@ -0,0 +1,11 @@ +package model + +import "gorm.io/gorm" + +type Position struct { + gorm.Model + Name string `gorm:"unique;index"` + RightMask int `json:"right_mask"` + OrganizationID uint `json:"organization_id" gorm:"index;not null"` + Users []User `json:"users,omitempty"` +} diff --git a/model/user.go b/model/user.go index 9484519..de60721 100644 --- a/model/user.go +++ b/model/user.go @@ -5,16 +5,19 @@ import "gorm.io/gorm" type ( User struct { gorm.Model - UserName string `json:"userName" gorm:"unique"` - Password string `json:"password,omitempty"` - FirstName string `json:"firstName"` - LastName string `json:"lastName"` + UserName string `json:"user_name" gorm:"unique;index"` + Password string `json:"password,omitempty"` + FirstName string `json:"first_name"` + LastName string `json:"last_name"` + Role string `json:"role" gorm:"default:employee"` + PositionID uint `json:"-"` + Position Position `gorm:"-:migration"` } ) type APIUser struct { - UserName string `json:"userName" gorm:"unique"` + UserName string `json:"user_name" gorm:"unique;index"` Password string `json:"password,omitempty"` - FirstName string `json:"firstName"` - LastName string `json:"lastName"` + FirstName string `json:"first_name"` + LastName string `json:"last_name"` } diff --git a/test.db b/test.db index 882d457fa36ad79e18fd740f203e0b8655048155..5ba84c9fc1fdb4817d420294b0f28da87a32071a 100644 GIT binary patch literal 69632 zcmeI*-EP}d00(e?v`x}A^`;4ogsNJdU_|XYe%rZ1`48l4_nrP4{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