Skip to content

Instantly share code, notes, and snippets.

package root
type User struct {
Id string `json:"id"`
Username string `json:"username"`
Password string `json:"password"`
}
type UserService interface {
CreateUser(u *User) error
@eamonnmcevoy
eamonnmcevoy / user.go
Last active March 14, 2017 08:21
User.go part 1, create user
package root
type User struct {
Id string `json:"id"`
Username string `json:"username"`
Password string `json:"password"`
}
type UserService interface {
CreateUser(u *User) error
package mongo
import (
"gopkg.in/mgo.v2"
)
type Session struct {
session *mgo.Session
}
package mongo
import (
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"go_rest_api/pkg"
)
type UserService struct {
collection *mgo.Collection
package mongo
import (
"go_rest_api/pkg"
"gopkg.in/mgo.v2/bson"
"gopkg.in/mgo.v2"
)
type userModel struct {
Id bson.ObjectId `bson:"_id,omitempty"`
package config
import (
"os"
"fmt"
"encoding/json"
"path/filepath"
"go_rest_api/pkg"
)
package root
type MongoConfig struct {
Ip string `json:"ip"`
DbName string `json:"dbName"`
}
type ServerConfig struct {
Port string `json:"port"`
}
@eamonnmcevoy
eamonnmcevoy / user_service.go
Last active March 14, 2017 09:13
Create(u *root.User)
func(p *UserService) Create(u *root.User) error {
user := newUserModel(u)
return p.collection.Insert(&user)
}
func(p *UserService) GetByUsername(username string) (*root.User,error) {
model := userModel{}
err := p.collection.Find(bson.M{"username": username}).One(&model)
return model.toRootUser(), err
}
@eamonnmcevoy
eamonnmcevoy / mongo_test.go
Last active March 14, 2017 12:31
GetByUsername(username string)
package mongo_test
import (
"log"
"testing"
"go_rest_api/pkg"
"go_rest_api/pkg/mongo"
)
package mongo
import (
"gopkg.in/mgo.v2"
)
type Session struct {
session *mgo.Session
}