User list
This commit is contained in:
@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"iris-test/app/repository"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@ -48,5 +49,7 @@ func InitDB() *gorm.DB {
|
||||
db, _ := DBConn.DB()
|
||||
db.SetConnMaxIdleTime(CONNECTION_MAX_IDLE_TIME)
|
||||
|
||||
repository.Dao = repository.CreateDAO(DBConn)
|
||||
|
||||
return DBConn
|
||||
}
|
||||
|
||||
45
app/main.go
45
app/main.go
@ -3,9 +3,11 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"iris-test/app/views"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/kataras/iris/v12/middleware/accesslog"
|
||||
"github.com/kataras/iris/v12/sessions"
|
||||
"github.com/kataras/iris/v12/sessions/sessiondb/redis"
|
||||
)
|
||||
@ -41,10 +43,49 @@ func createSessionEngine() *sessions.Sessions {
|
||||
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 {
|
||||
sessions_engine := createSessionEngine()
|
||||
sessionsEngine := createSessionEngine()
|
||||
accessLog := createAccessLog()
|
||||
|
||||
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))
|
||||
views.CreateRouter(app)
|
||||
return app
|
||||
|
||||
18
app/models/user.go
Normal file
18
app/models/user.go
Normal 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
17
app/repository/dao.go
Normal 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),
|
||||
}
|
||||
}
|
||||
12
app/repository/pagination.go
Normal file
12
app/repository/pagination.go
Normal 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
45
app/repository/users.go
Normal 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
|
||||
}
|
||||
@ -5,9 +5,9 @@
|
||||
<tbody>
|
||||
{{ range users }}
|
||||
<tr>
|
||||
<td>{{ .firstName }}</td>
|
||||
<td>{{ .lastName }}</td>
|
||||
<td>{{ .email }}</td>
|
||||
<td>{{ .FirstName }}</td>
|
||||
<td>{{ .LastName }}</td>
|
||||
<td>{{ .Email }}</td>
|
||||
</tr>
|
||||
{{ end }}
|
||||
</tbody>
|
||||
|
||||
@ -1,39 +1,19 @@
|
||||
package views
|
||||
|
||||
import "github.com/kataras/iris/v12"
|
||||
import (
|
||||
"iris-test/app/repository"
|
||||
|
||||
type User struct {
|
||||
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",
|
||||
},
|
||||
}
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
func GetUsersPage(ctx iris.Context) {
|
||||
params1 := []string{"param 1", "param 2", "param 3"}
|
||||
ctx.ViewData("params1", params1)
|
||||
|
||||
userRepository := repository.Dao.UsersRepository
|
||||
|
||||
users := userRepository.List(&repository.UserFilter{})
|
||||
|
||||
ctx.ViewData("users", users)
|
||||
|
||||
if err := ctx.View("pages/users.jet"); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user