Beispiel #1
0
func (s *service) createFeed(request *restful.Request, response *restful.Response) {
	feed := types.Feed{}
	err := request.ReadEntity(&feed)
	if err != nil {
		response.WriteEntity(Status{"fail", "Could not parse Feed: " + err.Error()})
		return
	}
	err = feed.ValidateNew()
	if err != nil {
		response.WriteEntity(Status{"fail", "Could not validate Feed: " + err.Error()})
		return
	}
	_, err = s.store.Get(feed.Id)
	if err == nil {
		response.WriteEntity(Status{"fail", "Feed already exists"})
		return
	}
	err = s.store.Put(&feed)
	if err != nil {
		response.WriteEntity(Status{"fail", "Could not store Feed: " + err.Error()})
		return
	}
	response.WriteEntity(Status{"ok", "Feed created"})
}
Beispiel #2
0
func (s *store) putFeed(feed types.Feed) error {
	items := feed.Items
	feed.Items = []types.Item{}
	info_bytes, err := json.Marshal(feed)
	if err != nil {
		return err
	}
	item_bytes, err := json.Marshal(items)
	if err != nil {
		return err
	}
	stmt, err := s.conn.Prepare("insert or replace into feed(id, type, json) values (?, ?, ?)")
	if err != nil {
		return err
	}
	s.conn.Begin()
	err = stmt.Exec(feed.Id, feed.Type, info_bytes)
	stmt.Finalize()
	if err != nil {
		s.conn.Rollback()
		return err
	}
	stmt, err = s.conn.Prepare("insert or replace into item(id, json) values (?, ?)")
	if err != nil {
		s.conn.Rollback()
		return err
	}
	err = stmt.Exec(feed.Id, item_bytes)
	stmt.Finalize()
	if err != nil {
		s.conn.Rollback()
		return err
	}
	s.conn.Commit()
	return nil
}