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

     1  package websitemap
     2  
     3  import (
     4  	"encoding/xml"
     5  	"net/http"
     6  	"time"
     7  
     8  	"github.com/ngocphuongnb/tetua/app/entities"
     9  	"github.com/ngocphuongnb/tetua/app/repositories"
    10  	"github.com/ngocphuongnb/tetua/app/server"
    11  )
    12  
    13  func User(c server.Context) error {
    14  	sitemapUser := SitemapUrlSets{
    15  		Xmlns:               "http://www.sitemaps.org/schemas/sitemap/0.9",
    16  		XmlnsXsi:            "http://www.w3.org/2001/XMLSchema-instance",
    17  		XmlnsImage:          "http://www.google.com/schemas/sitemap-image/1.1",
    18  		XmlnsSchemaLocation: "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd http://www.google.com/schemas/sitemap-image/1.1 http://www.google.com/schemas/sitemap-image/1.1/sitemap-image.xsd",
    19  	}
    20  
    21  	users, err := repositories.User.Find(c.Context(), &entities.UserFilter{Filter: &entities.Filter{Limit: 10000}})
    22  
    23  	if err != nil {
    24  		c.Logger().Error(err)
    25  		return c.Status(http.StatusInternalServerError).SendString("Error")
    26  	}
    27  
    28  	for _, user := range users {
    29  		avatar := user.Avatar()
    30  		sitemapUrl := &SitemapUrl{
    31  			Loc: &SitemapLoc{
    32  				Value: user.Url(),
    33  			},
    34  			LastMod: &SitemapLastMod{
    35  				Value: user.UpdatedAt.Format(time.RFC3339),
    36  			},
    37  		}
    38  
    39  		if avatar != "" {
    40  			sitemapUrl.Image = &SitemapImage{
    41  				Loc: &SitemapImageLoc{
    42  					Value: avatar,
    43  				},
    44  			}
    45  		}
    46  		sitemapUser.Urls = append(sitemapUser.Urls, sitemapUrl)
    47  	}
    48  
    49  	sitemapBytes, err := xml.Marshal(sitemapUser)
    50  
    51  	if err != nil {
    52  		c.Logger().Error(err)
    53  		return c.Status(http.StatusInternalServerError).SendString("Error")
    54  	}
    55  
    56  	c.Response().Header("content-type", "text/xml; charset=UTF-8")
    57  	return c.SendString(`<?xml version="1.0" encoding="UTF-8"?>` + string(sitemapBytes))
    58  }