コード例 #1
0
ファイル: myfeeds.go プロジェクト: vincentcr/myfeeds
func parseFeedRequest(r *http.Request, feed *services.Feed) error {
	var feedReq FeedRequest
	if err := parseAndValidate(r, &feedReq); err != nil {
		return err
	}
	feed.ID = services.RecordID(feedReq.ID)
	feed.Title = feedReq.Title
	feed.Description = feedReq.Description
	if feedReq.Items != nil {
		feed.Items = make([]services.FeedItem, 0, len(feedReq.Items))
		for _, itemReq := range feedReq.Items {
			var item services.FeedItem
			copyFeedItemFromRequest(itemReq, &item)
			feed.Items = append(feed.Items, item)
		}
	}
	return nil
}
コード例 #2
0
ファイル: myfeeds.go プロジェクト: vincentcr/myfeeds
func routeFeeds(m *Mux) {
	m.Get("/api/v1/feeds", mustAuthenticate(services.AccessRead, func(c *MyFeedsContext, w http.ResponseWriter, r *http.Request) {
		feeds, err := c.Services.Feeds.GetAllJson(c.MustGetUser())
		if err != nil {
			panic(err)
		}
		writeCacheable(r, w, "application/json", feeds)
	}))

	m.Get("/api/v1/feeds/:feedID", mustAuthenticate(services.AccessRead, func(c *MyFeedsContext, w http.ResponseWriter, r *http.Request) {
		feedID := services.RecordID(c.URLParams["feedID"])
		feed, err := c.Services.Feeds.GetJson(c.MustGetUser(), feedID)
		if err != nil {
			panic(err)
		}
		writeCacheable(r, w, "application/json", feed)
	}))

	m.Get("/api/v1/feeds/:feedID/rss", mustAuthenticate(services.AccessRead, func(c *MyFeedsContext, w http.ResponseWriter, r *http.Request) {
		feedID := services.RecordID(c.URLParams["feedID"])
		rss, err := c.Services.Feeds.GetRss(c.MustGetUser(), feedID)
		if err != nil {
			panic(err)
		}
		writeCacheable(r, w, "text/xml", rss)
	}))

	m.Post("/api/v1/feeds", mustAuthenticateRW(func(c *MyFeedsContext, w http.ResponseWriter, r *http.Request) {
		var feed services.Feed
		if err := parseFeedRequest(r, &feed); err != nil {
			panic(err)
		}

		user := c.MustGetUser()

		token, err := c.Services.Users.CreateToken(user, services.AccessRead)
		if err != nil {
			panic(err)
		}

		err = c.Services.Feeds.Create(user, token, &feed)
		if err != nil {
			panic(err)
		}
		jsonify(feed, w)
	}))

	m.Put("/api/v1/feeds/:feedID", mustAuthenticateRW(func(c *MyFeedsContext, w http.ResponseWriter, r *http.Request) {
		feedID := services.RecordID(c.URLParams["feedID"])
		feed := services.Feed{}
		if err := parseFeedRequest(r, &feed); err != nil {
			panic(err)
		}
		feed.ID = feedID
		err := c.Services.Feeds.Update(c.MustGetUser(), &feed)
		if err != nil {
			panic(err)
		}
		w.WriteHeader(http.StatusNoContent)
	}))

	m.Delete("/api/v1/feeds/:feedID", mustAuthenticateRW(func(c *MyFeedsContext, w http.ResponseWriter, r *http.Request) {
		feedID := services.RecordID(c.URLParams["feedID"])
		err := c.Services.Feeds.Delete(c.MustGetUser(), feedID)
		if err != nil {
			panic(err)
		}
		w.WriteHeader(http.StatusNoContent)
	}))

	m.Post("/api/v1/feeds/:feedID/items", mustAuthenticateRW(func(c *MyFeedsContext, w http.ResponseWriter, r *http.Request) {
		feedID := services.RecordID(c.URLParams["feedID"])
		item := services.FeedItem{FeedID: feedID}
		if err := parseFeedItemRequest(r, &item); err != nil {
			panic(err)
		}
		err := c.Services.Feeds.AddItem(c.MustGetUser(), &item)
		if err != nil {
			panic(err)
		}
		jsonify(item, w)
	}))

	m.Put("/api/v1/feeds/:feedID/items/:itemID", mustAuthenticateRW(func(c *MyFeedsContext, w http.ResponseWriter, r *http.Request) {
		itemID := services.RecordID(c.URLParams["itemID"])
		item := services.FeedItem{ID: itemID}
		if err := parseFeedItemRequest(r, &item); err != nil {
			panic(err)
		}
		err := c.Services.Feeds.UpdateItem(c.MustGetUser(), item)
		if err != nil {
			panic(err)
		}
		jsonify(item, w)
	}))

	m.Delete("/api/v1/feeds/:feedID/items/:itemID", mustAuthenticateRW(func(c *MyFeedsContext, w http.ResponseWriter, r *http.Request) {
		feedID := services.RecordID(c.URLParams["feedID"])
		itemID := services.RecordID(c.URLParams["itemID"])
		err := c.Services.Feeds.DeleteItem(c.MustGetUser(), feedID, itemID)
		if err != nil {
			panic(err)
		}
		w.WriteHeader(http.StatusNoContent)
	}))
}