User list

This commit is contained in:
Eden Kirin
2023-10-25 19:54:21 +02:00
parent 34b2f55cfc
commit b143983d52
10 changed files with 160 additions and 34 deletions

View File

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"iris-test/app/repository"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -48,5 +49,7 @@ func InitDB() *gorm.DB {
db, _ := DBConn.DB() db, _ := DBConn.DB()
db.SetConnMaxIdleTime(CONNECTION_MAX_IDLE_TIME) db.SetConnMaxIdleTime(CONNECTION_MAX_IDLE_TIME)
repository.Dao = repository.CreateDAO(DBConn)
return DBConn return DBConn
} }

View File

@ -3,9 +3,11 @@ package main
import ( import (
"fmt" "fmt"
"iris-test/app/views" "iris-test/app/views"
"os"
"time" "time"
"github.com/kataras/iris/v12" "github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/middleware/accesslog"
"github.com/kataras/iris/v12/sessions" "github.com/kataras/iris/v12/sessions"
"github.com/kataras/iris/v12/sessions/sessiondb/redis" "github.com/kataras/iris/v12/sessions/sessiondb/redis"
) )
@ -41,10 +43,49 @@ func createSessionEngine() *sessions.Sessions {
return sessions_engine return sessions_engine
} }
func createAccessLog() *accesslog.AccessLog {
// Initialize a new access log middleware.
ac := accesslog.New(os.Stdout)
// Remove this line to disable logging to console:
// ac.AddOutput(os.Stdout)
// The default configuration:
ac.Delim = '|'
ac.TimeFormat = "2006-01-02 15:04:05"
ac.Async = false
ac.IP = true
ac.BytesReceivedBody = true
ac.BytesSentBody = true
ac.BytesReceived = false
ac.BytesSent = false
ac.BodyMinify = true
ac.RequestBody = true
ac.ResponseBody = false
ac.KeepMultiLineError = true
ac.PanicLog = accesslog.LogHandler
// Default line format if formatter is missing:
// Time|Latency|Code|Method|Path|IP|Path Params Query Fields|Bytes Received|Bytes Sent|Request|Response|
//
// Set Custom Formatter:
ac.SetFormatter(&accesslog.JSON{
Indent: " ",
HumanTime: true,
})
// ac.SetFormatter(&accesslog.CSV{})
// ac.SetFormatter(&accesslog.Template{Text: "{{.Code}}"})
return ac
}
func createApp() *iris.Application { func createApp() *iris.Application {
sessions_engine := createSessionEngine() sessionsEngine := createSessionEngine()
accessLog := createAccessLog()
app := iris.New() app := iris.New()
app.Use(sessions_engine.Handler()) app.Logger().SetLevel(Config.Application.LogLevel)
app.Use(sessionsEngine.Handler())
app.UseRouter(accessLog.Handler)
app.RegisterView(iris.Jet("./app/templates", ".jet").Reload(true)) app.RegisterView(iris.Jet("./app/templates", ".jet").Reload(true))
views.CreateRouter(app) views.CreateRouter(app)
return app return app

18
app/models/user.go Normal file
View File

@ -0,0 +1,18 @@
package models
import "time"
type User struct {
Id string `gorm:"type(uuid);unique"`
Email string `gorm:"unique"`
FirstName string
LastName string
Password string
IsActive bool
CreatedAt time.Time
UpdatedAt time.Time
}
func (u *User) TableName() string {
return "users"
}

17
app/repository/dao.go Normal file
View File

@ -0,0 +1,17 @@
package repository
import "gorm.io/gorm"
type DAO struct {
db *gorm.DB
UsersRepository *UsersRepository
}
var Dao DAO
func CreateDAO(db *gorm.DB) DAO {
return DAO{
db: db,
UsersRepository: CreateUsersRepository(db),
}
}

View File

@ -0,0 +1,12 @@
package repository
type Pagination struct {
PageSize int
Page int
}
func (p *Pagination) New() *Pagination {
p.PageSize = 50
p.Page = 1
return p
}

45
app/repository/users.go Normal file
View File

@ -0,0 +1,45 @@
package repository
import (
"iris-test/app/models"
"gorm.io/gorm"
)
type UsersRepository struct {
db *gorm.DB
}
type UserFilter struct {
IsActive *bool
}
func CreateUsersRepository(db *gorm.DB) *UsersRepository {
return &UsersRepository{db}
}
func applyFilter(db *gorm.DB, filter *UserFilter) *gorm.DB {
query := db
// if filter.State != "" {
// query = query.Where("state = ?", filter.State)
// }
// if filter.SendAt_lt != nil {
// query = query.Where("send_at < ?", filter.SendAt_lt)
// }
// if filter.SendAt_notNull != nil {
// query = query.Not(map[string]interface{}{"send_at": nil})
// }
return query
}
func (repository *UsersRepository) List(filter *UserFilter) *[]models.User {
var users []models.User
query := repository.db.Model(&models.User{})
query = applyFilter(query, filter)
query.Find(&users)
return &users
}

View File

@ -5,9 +5,9 @@
<tbody> <tbody>
{{ range users }} {{ range users }}
<tr> <tr>
<td>{{ .firstName }}</td> <td>{{ .FirstName }}</td>
<td>{{ .lastName }}</td> <td>{{ .LastName }}</td>
<td>{{ .email }}</td> <td>{{ .Email }}</td>
</tr> </tr>
{{ end }} {{ end }}
</tbody> </tbody>

View File

@ -1,39 +1,19 @@
package views package views
import "github.com/kataras/iris/v12" import (
"iris-test/app/repository"
type User struct { "github.com/kataras/iris/v12"
firstName string )
lastName string
email string
}
var users = []User{
{
firstName: "Pero",
lastName: "Perić",
email: "pero@gmail.com",
},
{
firstName: "Mirko",
lastName: "Mirković",
email: "mirko@gmail.com",
},
{
firstName: "Ivo",
lastName: "Ivić",
email: "ivo@gmail.com",
},
{
firstName: "Slavko",
lastName: "Slavković",
email: "slavko@gmail.com",
},
}
func GetUsersPage(ctx iris.Context) { func GetUsersPage(ctx iris.Context) {
params1 := []string{"param 1", "param 2", "param 3"} params1 := []string{"param 1", "param 2", "param 3"}
ctx.ViewData("params1", params1) ctx.ViewData("params1", params1)
userRepository := repository.Dao.UsersRepository
users := userRepository.List(&repository.UserFilter{})
ctx.ViewData("users", users) ctx.ViewData("users", users)
if err := ctx.View("pages/users.jet"); err != nil { if err := ctx.View("pages/users.jet"); err != nil {

3
go.mod
View File

@ -34,6 +34,7 @@ require (
github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect github.com/jinzhu/now v1.1.5 // indirect
github.com/josharian/intern v1.0.0 // indirect github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kataras/blocks v0.0.8 // indirect github.com/kataras/blocks v0.0.8 // indirect
github.com/kataras/golog v0.1.9 // indirect github.com/kataras/golog v0.1.9 // indirect
github.com/kataras/pio v0.0.12 // indirect github.com/kataras/pio v0.0.12 // indirect
@ -43,6 +44,8 @@ require (
github.com/mailgun/raymond/v2 v2.0.48 // indirect github.com/mailgun/raymond/v2 v2.0.48 // indirect
github.com/mailru/easyjson v0.7.7 // indirect github.com/mailru/easyjson v0.7.7 // indirect
github.com/microcosm-cc/bluemonday v1.0.26 // indirect github.com/microcosm-cc/bluemonday v1.0.26 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/redis/go-redis/v9 v9.2.0 // indirect github.com/redis/go-redis/v9 v9.2.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect

7
go.sum
View File

@ -42,6 +42,7 @@ github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4= github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY= github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
@ -66,6 +67,8 @@ github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/kataras/blocks v0.0.8 h1:MrpVhoFTCR2v1iOOfGng5VJSILKeZZI+7NGfxEh3SUM= github.com/kataras/blocks v0.0.8 h1:MrpVhoFTCR2v1iOOfGng5VJSILKeZZI+7NGfxEh3SUM=
github.com/kataras/blocks v0.0.8/go.mod h1:9Jm5zx6BB+06NwA+OhTbHW1xkMOYxahnqTN5DveZ2Yg= github.com/kataras/blocks v0.0.8/go.mod h1:9Jm5zx6BB+06NwA+OhTbHW1xkMOYxahnqTN5DveZ2Yg=
github.com/kataras/golog v0.1.9 h1:vLvSDpP7kihFGKFAvBSofYo7qZNULYSHOH2D7rPTKJk= github.com/kataras/golog v0.1.9 h1:vLvSDpP7kihFGKFAvBSofYo7qZNULYSHOH2D7rPTKJk=
@ -100,6 +103,10 @@ github.com/microcosm-cc/bluemonday v1.0.26 h1:xbqSvqzQMeEHCqMi64VAs4d8uy6Mequs3r
github.com/microcosm-cc/bluemonday v1.0.26/go.mod h1:JyzOCs9gkyQyjs+6h10UEVSe02CGwkhd72Xdqh78TWs= github.com/microcosm-cc/bluemonday v1.0.26/go.mod h1:JyzOCs9gkyQyjs+6h10UEVSe02CGwkhd72Xdqh78TWs=
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0= github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0= github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=