113 lines
2.9 KiB
Go
113 lines
2.9 KiB
Go
package views
|
|
|
|
import (
|
|
"fmt"
|
|
"iris-test/app/lib/auth"
|
|
"iris-test/app/lib/helpers"
|
|
"iris-test/app/repository"
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
"github.com/kataras/iris/v12"
|
|
)
|
|
|
|
type editUserRequestDto struct {
|
|
FirstName string `json:"firstName" validate:"required"`
|
|
LastName string `json:"lastName" validate:"required"`
|
|
Email string `json:"email" validate:"required,email"`
|
|
Password string `json:"password"`
|
|
}
|
|
|
|
func GetUsersPage(ctx iris.Context) {
|
|
userRepository := repository.Dao.UsersRepository
|
|
|
|
pagination := repository.NewPagination()
|
|
ordering := []repository.Ordering{
|
|
repository.NewOrdering("updated_at", repository.ORDERING_DESC),
|
|
repository.NewOrdering("first_name", repository.ORDERING_ASC),
|
|
repository.NewOrdering("last_name", repository.ORDERING_ASC),
|
|
}
|
|
|
|
isActive := true
|
|
users := userRepository.List(&repository.UserFilter{IsActive: &isActive}, &pagination, &ordering)
|
|
|
|
vars := iris.Map{
|
|
"activePage": "users",
|
|
"users": users,
|
|
}
|
|
|
|
if err := ctx.View("pages/users.jet", vars); err != nil {
|
|
showError(ctx, err)
|
|
return
|
|
}
|
|
}
|
|
|
|
func EditUserPage(ctx iris.Context) {
|
|
userId := ctx.Params().Get("userId")
|
|
userRepository := repository.Dao.UsersRepository
|
|
|
|
filter := repository.UserFilter{Id: &userId}
|
|
user := userRepository.Get(&filter)
|
|
|
|
vars := iris.Map{
|
|
"activePage": "users",
|
|
"user": user,
|
|
"currentPath": ctx.Path(),
|
|
"backlink": "/users",
|
|
}
|
|
|
|
if err := ctx.View("pages/user-edit.jet", vars); err != nil {
|
|
showError(ctx, err)
|
|
return
|
|
}
|
|
}
|
|
|
|
func SaveUser(ctx iris.Context) {
|
|
var postData editUserRequestDto
|
|
err := ctx.ReadJSON(&postData)
|
|
|
|
if err != nil {
|
|
// Handle the error, below you will find the right way to do that...
|
|
if errs, ok := err.(validator.ValidationErrors); ok {
|
|
// Wrap the errors with JSON format, the underline library returns the errors as interface.
|
|
validationErrors := helpers.WrapValidationErrors(errs)
|
|
|
|
// Fire an application/json+problem response and stop the handlers chain.
|
|
ctx.StopWithProblem(
|
|
iris.StatusBadRequest, iris.NewProblem().
|
|
Title("Validation error").
|
|
Detail("One or more fields failed to be validated").
|
|
Key("errors", validationErrors).
|
|
Key("success", false),
|
|
)
|
|
return
|
|
}
|
|
// It's probably an internal JSON error, let's dont give more info here.
|
|
ctx.StopWithStatus(iris.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
userId := ctx.Params().Get("userId")
|
|
userRepository := repository.Dao.UsersRepository
|
|
|
|
filter := repository.UserFilter{Id: &userId}
|
|
user := userRepository.Get(&filter)
|
|
|
|
user.FirstName = postData.FirstName
|
|
user.LastName = postData.LastName
|
|
user.Email = postData.Email
|
|
|
|
if len(postData.Password) > 0 {
|
|
user.SetPassword(postData.Password)
|
|
fmt.Printf("Set password: %s\n", user.Password)
|
|
fmt.Printf("IsPasswordGoodEnough: %v\n", auth.IsPasswordGoodEnough(postData.Password))
|
|
}
|
|
|
|
userRepository.Save(user)
|
|
|
|
response := iris.Map{
|
|
"success": true,
|
|
}
|
|
|
|
ctx.JSON(response)
|
|
}
|