Skip to content

Instantly share code, notes, and snippets.

@congjf
Created December 24, 2013 03:32
Show Gist options
  • Save congjf/8108451 to your computer and use it in GitHub Desktop.
Save congjf/8108451 to your computer and use it in GitHub Desktop.
Golang Type
Golang Type
// Sections数据模型结构
type Sections struct {
Name string `json:"name" bson:"name"`
Upper string `json:"upper" bson:"upper"`
Master string `json:"master" bson:"master"`
Uppermaster string `json:"uppermaster" bson:"uppermaster"`
OrgID bson.ObjectId `json:"orgid" bson:"orgid"`
}
// Sections新建
func (o *Sections) Create() error {
log.Println("Model: Sections Create")
session, err := getSession()
if err != nil {
log.Println("Sections: " + err.Error())
return err
}
defer session.Close()
collection := session.DB(DATABASE).C("organizations")
// Operation
change := bson.M{"$push": bson.M{"sections": o}}
orgid := o.OrgID
log.Println(change)
err = collection.UpdateId(orgid, change)
if err != nil {
log.Println(err.Error())
} else {
log.Println("Sections - " + o.Name + ": Create Successful!")
}
return err
}
// Sections检索
func (o *Sections) Retrieve(conditions map[string]interface{}) ([]Sections, error) {
log.Println("Model: Sections Retrieve")
session, err := getSession()
if err != nil {
log.Println("Sections: " + err.Error())
return nil, err
}
defer session.Close()
collection := session.DB(DATABASE).C("organizations")
org := Organizations{}
result := []Sections{}
err = collection.Find(conditions).One(&org)
if err != nil {
log.Println(err.Error())
return nil, err
}
result = org.Sections
log.Println(result)
return result, nil
}
// Sections更新
func (o *Sections) Update() error {
log.Println("Model: Sections Update")
session, err := getSession()
if err != nil {
log.Println("Sections: " + err.Error())
return err
}
defer session.Close()
// collection := session.DB(DATABASE).C("sections")
return nil
}
// Sections删除
func (o *Sections) Delete() error {
log.Println("Model: Sections Delete")
session, err := getSession()
if err != nil {
log.Println("Sections: " + err.Error())
return err
}
defer session.Close()
// collection := session.DB(DATABASE).C("sections")
return nil
}
// The method of the struct for implement some interface MUST have not a Pointer Receiver!
package controllers
import (
"net/http"
)
type Base interface {
Index(rw http.ResponseWriter, req *http.Request)
Create(rw http.ResponseWriter, req *http.Request)
New(rw http.ResponseWriter, req *http.Request)
Edit(rw http.ResponseWriter, req *http.Request)
Show(rw http.ResponseWriter, req *http.Request)
Update(rw http.ResponseWriter, req *http.Request)
Destroy(rw http.ResponseWriter, req *http.Request)
}
type Organizations struct {
}
// for GET /Organizations
func (u Organizations) Index(rw http.ResponseWriter, req *http.Request) {
notice := "Organizations#Index:" + req.Method + req.URL.String()
log.Println(notice)
var org model.Organizations
conditions := bson.M{"_id": bson.M{"$ne": ""}}
result, _ := org.Retrieve(conditions)
encoder := json.NewEncoder(rw)
err := encoder.Encode(result)
if err != nil {
log.Println(err.Error())
rw.WriteHeader(http.StatusInternalServerError)
} else {
rw.WriteHeader(http.StatusOK)
}
}
// for POST /Organizations
func (u Organizations) Create(rw http.ResponseWriter, req *http.Request) {
notice := "Organizations#Create:" + req.Method + req.URL.String()
log.Println(notice)
org := model.Organizations{}
decoder := json.NewDecoder(req.Body)
err := decoder.Decode(&org)
if err != nil {
log.Println(err.Error())
rw.WriteHeader(http.StatusNotAcceptable)
}
err = org.Create()
if err != nil {
rw.WriteHeader(http.StatusNotImplemented)
} else {
rw.WriteHeader(http.StatusCreated)
}
}
// for GET /Organizations/new
func (u Organizations) New(rw http.ResponseWriter, req *http.Request) {
notice := "Organizations#New:" + req.Method + req.URL.String()
log.Println(notice)
}
// for GET /Organizations/:id/edit
func (u Organizations) Edit(rw http.ResponseWriter, req *http.Request) {
notice := "Organizations#Edit:" + req.Method + req.URL.String()
log.Println(notice)
}
// for GET /Organizations/:id
func (u Organizations) Show(rw http.ResponseWriter, req *http.Request) {
notice := "Organizations#Show:" + req.Method + req.URL.String()
log.Println(notice)
}
// for PATCH,PUT /Organizations/:id
func (u Organizations) Update(rw http.ResponseWriter, req *http.Request) {
notice := "Organizations#Update:" + req.Method + req.URL.String()
log.Println(notice)
org := model.Organizations{}
decoder := json.NewDecoder(req.Body)
err := decoder.Decode(&org)
if err != nil {
log.Println(err.Error())
rw.WriteHeader(http.StatusNotAcceptable)
}
err = org.Update()
if err != nil {
rw.WriteHeader(http.StatusNotImplemented)
} else {
rw.WriteHeader(http.StatusCreated)
}
}
// for DELETE /Organizations/:id
func (u Organizations) Destroy(rw http.ResponseWriter, req *http.Request) {
notice := "Organizations#Destroy:" + req.Method + req.URL.String()
log.Println(notice)
org := model.Organizations{}
decoder := json.NewDecoder(req.Body)
err := decoder.Decode(&org)
if err != nil {
log.Println(err.Error())
rw.WriteHeader(http.StatusNotAcceptable)
}
err = org.Delete()
if err != nil {
rw.WriteHeader(http.StatusNotImplemented)
} else {
rw.WriteHeader(http.StatusCreated)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment