Pracownia.Programowania/models/post.go
2019-02-01 13:06:00 +01:00

64 lines
1.4 KiB
Go

package models
import (
"git.wmi.amu.edu.pl/s439508/Pracownia.Programowania/common"
"gopkg.in/mgo.v2/bson"
)
type Post struct {
Id string `bson:"_id,omitempty"`
Title string `bson:"title"`
ContentHtml string `bson:"content_html"`
ContentMd string `bson:"content_md"`
ContentCom string `bson:"content_com"`
}
//Post
var Posts = new(posts)
type posts struct{}
func (posts) FindOne(id string) (*Post, error) {
var p *Post
return p, common.DB.Posts.Find(bson.M{"_id": id}).One(&p)
}
func (posts) FindAll() ([]*Post, error) {
var ps []*Post
return ps, common.DB.Posts.Find(nil).All(&ps)
}
func (posts) Create(id, title, contentHtml, contentMd, contentCom string) (*Post, error) {
p := &Post{
Id: id,
Title: title,
ContentMd: contentMd,
ContentHtml: contentHtml,
ContentCom: contentCom,
}
if err := common.DB.Posts.Insert(p); err != nil {
return nil, err
}
return p, nil
}
func (posts) Update(id, title, contentHtml, contentMd, contentCom string) error {
if err := common.DB.Posts.UpdateId(id,
bson.M{"$set": bson.M{
"title": title,
"content_html": contentHtml,
"content_md": contentMd,
"content_com": contentCom,
}}); err != nil {
return err
}
return nil
}
func (posts) DeletebyId(id string) error {
if err := common.DB.Posts.RemoveId(id); err != nil {
return err
}
return nil
}