90 lines
2.0 KiB
Go
90 lines
2.0 KiB
Go
package views
|
|
|
|
import (
|
|
"fmt"
|
|
"iris-test/app/lib/auth"
|
|
"iris-test/app/repository"
|
|
|
|
"github.com/kataras/iris/v12"
|
|
)
|
|
|
|
type editUserJSON struct {
|
|
FirstName string `json:"first-name"`
|
|
LastName string `json:"last-name"`
|
|
Email string `json:"email"`
|
|
Password string `json:"password"`
|
|
}
|
|
|
|
func GetUsersPage(ctx iris.Context) {
|
|
userRepository := repository.Dao.UsersRepository
|
|
|
|
pagination := repository.NewPagination()
|
|
ordering := []repository.Ordering{
|
|
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)
|
|
|
|
ctx.ViewData("users", users)
|
|
|
|
if err := ctx.View("pages/users.jet"); 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{
|
|
"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 editUserJSON
|
|
err := ctx.ReadJSON(&postData)
|
|
|
|
if err != nil {
|
|
ctx.StopWithError(iris.StatusBadRequest, err)
|
|
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)
|
|
}
|