github.com/ngocphuongnb/tetua@v0.0.7-alpha/app/web/feed.go (about)

     1  package web
     2  
     3  import (
     4  	"net/http"
     5  	"strconv"
     6  
     7  	"github.com/gorilla/feeds"
     8  	"github.com/ngocphuongnb/tetua/app/config"
     9  	"github.com/ngocphuongnb/tetua/app/entities"
    10  	"github.com/ngocphuongnb/tetua/app/repositories"
    11  	"github.com/ngocphuongnb/tetua/app/server"
    12  	"github.com/ngocphuongnb/tetua/app/utils"
    13  )
    14  
    15  func Feed(c server.Context) error {
    16  	posts, err := repositories.Post.Find(c.Context(), &entities.PostFilter{Filter: &entities.Filter{
    17  		Limit: 50,
    18  	}})
    19  
    20  	if err != nil {
    21  		c.Logger().Error(err)
    22  		return c.Status(http.StatusInternalServerError).SendString("Error")
    23  	}
    24  
    25  	feed := &feeds.Feed{
    26  		Title:       config.Setting("app_name"),
    27  		Link:        &feeds.Link{Href: utils.Url("")},
    28  		Description: config.Setting("app_desc"),
    29  		Author:      &feeds.Author{Name: config.Setting("contact_name"), Email: config.Setting("contact_email")},
    30  		// Created:     time.Now(),
    31  	}
    32  
    33  	feed.Items = utils.SliceMap(posts, func(post *entities.Post) *feeds.Item {
    34  		return &feeds.Item{
    35  			Id:          strconv.Itoa(post.ID),
    36  			Title:       post.Name,
    37  			Description: post.Description,
    38  			Content:     post.ContentHTML,
    39  			Author:      &feeds.Author{Name: post.User.Name(), Email: post.User.Email},
    40  			Link:        &feeds.Link{Href: post.Url()},
    41  			Created:     *post.CreatedAt,
    42  			Updated:     *post.UpdatedAt,
    43  		}
    44  	})
    45  
    46  	rss, err := feed.ToRss()
    47  	if err != nil {
    48  		c.Logger().Error(err)
    49  		return c.Status(http.StatusInternalServerError).SendString("Error")
    50  	}
    51  
    52  	c.Response().Header("content-type", "application/xml; charset=utf-8")
    53  	return c.SendString(rss)
    54  }