initial commit
This commit is contained in:
29
handler/auth.go
Normal file
29
handler/auth.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"astra-users/model"
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"github.com/labstack/echo/v4"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (h *Handler) Login(c echo.Context) (err error) {
|
||||
// Bind
|
||||
u := new(model.User)
|
||||
if err = c.Bind(u); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
if t.RowsAffected == 0 {
|
||||
return c.NoContent(http.StatusNotFound)
|
||||
}
|
||||
|
||||
u.Password = ""
|
||||
|
||||
return c.JSON(http.StatusOK, u)
|
||||
}
|
||||
7
handler/handler.go
Normal file
7
handler/handler.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package handler
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type Handler struct {
|
||||
DB *gorm.DB
|
||||
}
|
||||
14
handler/health.go
Normal file
14
handler/health.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"github.com/labstack/echo/v4"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (h *Handler) Health(c echo.Context) error {
|
||||
type Resp struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, Resp{Status: "OK"})
|
||||
}
|
||||
114
handler/users.go
Normal file
114
handler/users.go
Normal file
@@ -0,0 +1,114 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"astra-users/model"
|
||||
"astra-users/pkg/utils"
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"github.com/labstack/echo/v4"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (h *Handler) GetCurrentUser(c echo.Context) error {
|
||||
userId := c.Request().Header.Get("UserId")
|
||||
|
||||
if userId == "" {
|
||||
return c.NoContent(http.StatusBadRequest)
|
||||
}
|
||||
|
||||
u := new(model.User)
|
||||
|
||||
tx := h.DB.First(&u, userId)
|
||||
|
||||
if tx.RowsAffected == 0 {
|
||||
return c.NoContent(http.StatusNotFound)
|
||||
}
|
||||
|
||||
u.Password = ""
|
||||
|
||||
return c.JSON(http.StatusOK, &u)
|
||||
}
|
||||
|
||||
func (h *Handler) GetUsers(c echo.Context) error {
|
||||
var users []model.User
|
||||
|
||||
query := c.Request().URL.Query()
|
||||
limit, offset := utils.GetPaginationParams(query)
|
||||
sortCol, isDesc, filterCol, filterVal := utils.GetFilterParams(query)
|
||||
|
||||
tx := h.DB.Limit(limit).Offset(offset)
|
||||
|
||||
utils.ApplyIdFilter(c, tx)
|
||||
utils.ApplySort(tx, sortCol, isDesc)
|
||||
utils.ApplyFilters(tx, filterCol, filterVal)
|
||||
|
||||
tx = tx.Find(&users)
|
||||
|
||||
if tx.RowsAffected == 1 {
|
||||
return c.JSON(http.StatusOK, users[0])
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, users)
|
||||
}
|
||||
|
||||
func (h *Handler) UpdateUser(c echo.Context) (err error) {
|
||||
var user = new(model.User)
|
||||
var apiUser = new(model.APIUser)
|
||||
tx := h.DB
|
||||
|
||||
id := c.Param("id")
|
||||
tx = tx.Find(&user, id)
|
||||
|
||||
if tx.RowsAffected == 0 {
|
||||
return c.NoContent(http.StatusNotFound)
|
||||
}
|
||||
|
||||
if err = c.Bind(apiUser); err != nil {
|
||||
return c.NoContent(http.StatusBadRequest)
|
||||
}
|
||||
|
||||
user.FirstName = apiUser.FirstName
|
||||
user.LastName = apiUser.LastName
|
||||
|
||||
tx = h.DB.Save(&user)
|
||||
|
||||
if tx.RowsAffected == 0 {
|
||||
return c.NoContent(http.StatusBadGateway)
|
||||
}
|
||||
|
||||
user.Password = ""
|
||||
|
||||
return c.JSON(http.StatusOK, &user)
|
||||
}
|
||||
|
||||
func (h *Handler) DeleteUser(c echo.Context) (err error) {
|
||||
id := c.Param("id")
|
||||
|
||||
tx := h.DB.Delete(&model.User{}, id)
|
||||
|
||||
if tx.RowsAffected == 0 {
|
||||
return c.NoContent(http.StatusBadGateway)
|
||||
}
|
||||
|
||||
return c.NoContent(http.StatusOK)
|
||||
}
|
||||
|
||||
func (h *Handler) CreateUser(c echo.Context) (err error) {
|
||||
var user = new(model.User)
|
||||
|
||||
if err = c.Bind(user); err != nil {
|
||||
return c.NoContent(http.StatusBadRequest)
|
||||
}
|
||||
|
||||
user.Password = fmt.Sprintf("%x", sha256.Sum256([]byte(user.Password)))
|
||||
|
||||
tx := h.DB.Omit("ID", "CreatedAt", "UpdatedAt", "DeletedAt").Create(user)
|
||||
|
||||
if tx.RowsAffected == 0 {
|
||||
return c.NoContent(http.StatusBadGateway)
|
||||
}
|
||||
|
||||
user.Password = ""
|
||||
|
||||
return c.JSON(http.StatusCreated, user)
|
||||
}
|
||||
Reference in New Issue
Block a user