Skip to content

Instantly share code, notes, and snippets.

View Solution's full-sized avatar
🎯
Focusing

Patrik Tomášik Solution

🎯
Focusing
View GitHub Profile
func main() {
// init the repository based on gorm instance
repository := NewRepository(database)
// create qo from repository, you can do it again and again
qo := repository.NewQueryObject().
FilterByUsername("myUsername"). // compose your q.
FilterByActive()
// and than fetch it
type Repository struct {
database *gorm.DB
}
func NewRepository(database *gorm.DB) Repository {
return Repository{database: database}
}
func (r Repository) NewQueryObject() *QueryObject {
return NewQueryObject(r.database.Session(&gorm.Session{NewDB: true}))
func main() {
// always use new session
qo := NewQueryObject(database.Session(&gorm.Session{NewDB: true})).
FilterByUsername("myUsername").
FilterByActive()
// and now retrieve the model
var user model.User
qo.GetQuery().First(&user)
type QueryObject struct {
query *gorm.DB
}
func NewQueryObject(database *gorm.DB) *QueryObject {
return &QueryObject{query: database.Model(&User{})}
}
func (qo *QueryObject) FilterByUsername(username string) *QueryObject {
qo.query = qo.query.Where("username = ?", username)
type User struct {
gorm.Model
Firstname string
Surname string
Email string
Phone string
Password string
Role string
Active bool
type QueryObject interface {
GetQuery() *gorm.DB
}
type User struct {
gorm.Model
Username string
Firstname string
Surname string
Email string
Phone string
Password string
Role string
package transportServiceClient
import (
"net/http"
"strconv"
"fmt"
)
type Client struct {
*client.Client
package client
import (
"encoding/json"
"fmt"
"io/ioutil"
"github.com/Solution/go-httpwares"
"github.com/Solution/go-httpwares/metrics"
"github.com/Solution/go-httpwares/tags"
@Solution
Solution / requestFactory.go
Created December 13, 2018 21:32
Simple request factory
package client
import (
"net/http"
"strings"
"github.com/pkg/errors"
)
type RequestFactory struct {
schema Schema