add postgres and refactor code

This commit is contained in:
andrusyakka
2023-07-17 13:59:44 +03:00
parent 94d2a1df2b
commit 6a386e38cf
15 changed files with 107 additions and 47 deletions

View File

@@ -2,24 +2,27 @@ package handler
import (
"astra-users/model"
"astra-users/pkg/utils"
"github.com/labstack/echo/v4"
"net/http"
)
func (h *Handler) CreateOrganization(c echo.Context) (err error) {
organization := new(model.Organization)
organization := new(model.OrganizationApi)
if err = c.Bind(organization); err != nil {
return c.NoContent(http.StatusBadRequest)
}
tx := h.DB.Create(organization)
newOrg, err := utils.TypeConverter[model.Organization](organization)
tx := h.DB.Create(&newOrg)
if tx.RowsAffected == 0 {
return c.NoContent(http.StatusBadGateway)
}
return c.JSON(http.StatusCreated, organization)
return c.JSON(http.StatusCreated, newOrg)
}
func (h *Handler) GetOrganizations(c echo.Context) error {

View File

@@ -2,6 +2,7 @@ package handler
import (
"astra-users/model"
"astra-users/pkg/utils"
"errors"
"github.com/labstack/echo/v4"
"net/http"
@@ -37,17 +38,19 @@ func (h *Handler) GetPosition(id int) (*model.Position, error) {
}
func (h *Handler) CreatePosition(c echo.Context) (err error) {
position := new(model.Position)
position := new(model.PositionApi)
if err = c.Bind(position); err != nil {
return c.NoContent(http.StatusBadRequest)
}
tx := h.DB.Create(position)
newPosition, err := utils.TypeConverter[model.Position](position)
tx := h.DB.Create(&newPosition)
if tx.RowsAffected == 0 {
return c.NoContent(http.StatusBadGateway)
}
return c.JSON(http.StatusCreated, position)
return c.JSON(http.StatusCreated, newPosition)
}

View File

@@ -129,7 +129,7 @@ func (h *Handler) DeleteUser(c echo.Context) (err error) {
}
func (h *Handler) CreateUser(c echo.Context) (err error) {
var user = new(model.User)
user := new(model.APIUser)
if err = c.Bind(user); err != nil {
return c.NoContent(http.StatusBadRequest)
@@ -137,13 +137,17 @@ func (h *Handler) CreateUser(c echo.Context) (err error) {
user.Password = fmt.Sprintf("%x", sha256.Sum256([]byte(user.Password)))
tx := h.DB.Create(user)
newUser, err := utils.TypeConverter[model.User](&user)
tx := h.DB.Create(&newUser)
if tx.RowsAffected == 0 {
return c.NoContent(http.StatusBadGateway)
}
user.Password = ""
newUser.Password = ""
return c.JSON(http.StatusCreated, user)
h.DB.Find(&newUser.Position, newUser.PositionID)
return c.JSON(http.StatusCreated, newUser)
}