package public import ( "database/sql" "dorisbot/models" "errors" "time" ) // InstagramModel - type InstagramModel struct { ID string `db:"id"` LastPost string `db:"lastpost"` Ctime time.Time `db:"ctime"` Mtime time.Time `db:"mtime"` } // NewInstagram - func NewInstagram(id string) (ig *InstagramModel, err error) { if len(id) == 0 { return nil, errors.New("id is empty") } ig = &InstagramModel{ID: id} return ig, nil } // Write - func (p *InstagramModel) Write() (err error) { db := models.GetConn() query := ` insert into "public"."instagram" ("id", "lastpost") values (:id, :lastpost) returning * ` row, err := db.NamedQuery(query, p) if err != nil { return err } defer row.Close() if row.Next() { err = row.StructScan(p) if err != nil { return err } } return } // UpdatePost - func (p *InstagramModel) UpdatePost(postid string) (err error) { db := models.GetConn() query := `update "public"."instagram" set "lastpost" = $1, "mtime" = now() where "id" = $2` _, err = db.Exec(query, postid, p.ID) if err != nil { return err } p.LastPost = postid return } // GetInstagramByID - func GetInstagramByID(id string) (ig *InstagramModel, err error) { if len(id) == 0 { return nil, errors.New("id is empty") } ig = &InstagramModel{} db := models.GetConn() query := ` select "id", "lastpost", "ctime", "mtime" from "public"."instagram" where "id" = $1 ` err = db.Get(ig, query, id) if err != nil { if err == sql.ErrNoRows { return nil, nil } return nil, err } return } // GetAllInstagram - func GetAllInstagram() (list []InstagramModel, err error) { db := models.GetConn() query := ` select id, lastpost, ctime, mtime from "public"."instagram" order by ctime ` err = db.Select(&list, query) if err != nil { return nil, err } return list, nil } // GetInstagramByChannelAndID - func GetInstagramByChannelAndID(id, channel string) (ig *InstagramModel, err error) { if len(channel) == 0 || len(id) == 0 { return nil, errors.New("channel or ig is empty") } ig = &InstagramModel{} db := models.GetConn() query := ` select ig.id, ig.lastpost, ig.ctime, ig.mtime from "public"."instagram" ig left join "discord"."channel_instagram_rt" rt on rt.instagram = ig.id left join "discord"."channel" ch on ch.id = rt.channel where ch.id = $1 and ig.id = $2 ` err = db.Get(ig, query, channel, ig) if err != nil { if err == sql.ErrNoRows { return nil, nil } return nil, err } return }