add org structure
This commit is contained in:
@@ -16,7 +16,7 @@ type Config struct {
|
|||||||
func New() *Config {
|
func New() *Config {
|
||||||
return &Config{
|
return &Config{
|
||||||
Server: ServerConfig{
|
Server: ServerConfig{
|
||||||
Port: getEnv("PORT", ""),
|
Port: getEnv("PORT", "8080"),
|
||||||
ConnectionString: getEnv("CONNECTION_STRING", "test.db"),
|
ConnectionString: getEnv("CONNECTION_STRING", "test.db"),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ func (h *Handler) Login(c echo.Context) (err error) {
|
|||||||
|
|
||||||
u.Password = fmt.Sprintf("%x", sha256.Sum256([]byte(u.Password)))
|
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 {
|
if t.RowsAffected == 0 {
|
||||||
return c.NoContent(http.StatusNotFound)
|
return c.NoContent(http.StatusNotFound)
|
||||||
|
|||||||
31
handler/organization.go
Normal file
31
handler/organization.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
53
handler/position.go
Normal file
53
handler/position.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
@@ -102,7 +102,7 @@ func (h *Handler) CreateUser(c echo.Context) (err error) {
|
|||||||
|
|
||||||
user.Password = fmt.Sprintf("%x", sha256.Sum256([]byte(user.Password)))
|
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 {
|
if tx.RowsAffected == 0 {
|
||||||
return c.NoContent(http.StatusBadGateway)
|
return c.NoContent(http.StatusBadGateway)
|
||||||
|
|||||||
8
main.go
8
main.go
@@ -34,7 +34,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
e := echo.New()
|
e := echo.New()
|
||||||
|
|
||||||
db.AutoMigrate(&model.User{})
|
db.AutoMigrate(&model.User{}, &model.Organization{}, &model.Position{})
|
||||||
|
|
||||||
e.Use(middleware.Logger())
|
e.Use(middleware.Logger())
|
||||||
|
|
||||||
@@ -49,5 +49,11 @@ func main() {
|
|||||||
e.DELETE("/:id", h.DeleteUser)
|
e.DELETE("/:id", h.DeleteUser)
|
||||||
e.GET("/health", h.Health)
|
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))
|
e.Logger.Fatal(e.Start(":" + config.Env.Server.Port))
|
||||||
}
|
}
|
||||||
|
|||||||
10
model/organization.go
Normal file
10
model/organization.go
Normal file
@@ -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"`
|
||||||
|
}
|
||||||
11
model/position.go
Normal file
11
model/position.go
Normal file
@@ -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"`
|
||||||
|
}
|
||||||
@@ -5,16 +5,19 @@ import "gorm.io/gorm"
|
|||||||
type (
|
type (
|
||||||
User struct {
|
User struct {
|
||||||
gorm.Model
|
gorm.Model
|
||||||
UserName string `json:"userName" gorm:"unique"`
|
UserName string `json:"user_name" gorm:"unique;index"`
|
||||||
Password string `json:"password,omitempty"`
|
Password string `json:"password,omitempty"`
|
||||||
FirstName string `json:"firstName"`
|
FirstName string `json:"first_name"`
|
||||||
LastName string `json:"lastName"`
|
LastName string `json:"last_name"`
|
||||||
|
Role string `json:"role" gorm:"default:employee"`
|
||||||
|
PositionID uint `json:"-"`
|
||||||
|
Position Position `gorm:"-:migration"`
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
type APIUser struct {
|
type APIUser struct {
|
||||||
UserName string `json:"userName" gorm:"unique"`
|
UserName string `json:"user_name" gorm:"unique;index"`
|
||||||
Password string `json:"password,omitempty"`
|
Password string `json:"password,omitempty"`
|
||||||
FirstName string `json:"firstName"`
|
FirstName string `json:"first_name"`
|
||||||
LastName string `json:"lastName"`
|
LastName string `json:"last_name"`
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user