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

     1  package managepost
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/ngocphuongnb/tetua/app/entities"
     7  	e "github.com/ngocphuongnb/tetua/app/entities"
     8  	"github.com/ngocphuongnb/tetua/app/repositories"
     9  	"github.com/ngocphuongnb/tetua/app/server"
    10  	"github.com/ngocphuongnb/tetua/app/utils"
    11  	"github.com/ngocphuongnb/tetua/views"
    12  )
    13  
    14  func Index(c server.Context) error {
    15  	page := c.QueryInt("page", 1)
    16  	search := c.Query("q")
    17  	publish := c.Query("publish", "all")
    18  	approve := c.Query("approve", "all")
    19  	topicID := c.QueryInt("topic", 0)
    20  	userID := c.QueryInt("user", 0)
    21  	topicIDs := []int{}
    22  	userIDs := []int{}
    23  	status := http.StatusOK
    24  	topics, err := repositories.Topic.All(c.Context())
    25  
    26  	if topicID > 0 {
    27  		topicIDs = append(topicIDs, topicID)
    28  	}
    29  	if userID > 0 {
    30  		userIDs = append(userIDs, userID)
    31  	}
    32  
    33  	if err != nil {
    34  		c.WithError("Error getting topics", err)
    35  	}
    36  
    37  	data, err := repositories.Post.Paginate(c.Context(), &e.PostFilter{
    38  		Publish:  publish,
    39  		Approve:  approve,
    40  		TopicIDs: topicIDs,
    41  		UserIDs:  userIDs,
    42  		Filter: &entities.Filter{
    43  			BaseUrl: utils.Url("/manage/posts"),
    44  			Page:    page,
    45  			Search:  search,
    46  		}})
    47  	c.Meta().Title = "Manage posts"
    48  
    49  	if err != nil {
    50  		status = http.StatusBadRequest
    51  		c.WithError("Error getting posts", err)
    52  	}
    53  
    54  	return c.Status(status).Render(views.ManagePostIndex(
    55  		data,
    56  		entities.PrintTopicsTree(topics, []int{}),
    57  		topicIDs,
    58  		search,
    59  		publish,
    60  		approve,
    61  	))
    62  }
    63  
    64  func Approve(c server.Context) error {
    65  	if err := repositories.Post.Approve(c.Context(), c.ParamInt("id")); err != nil {
    66  		c.Logger().Error("Error aprrove post", err)
    67  		return c.Status(http.StatusBadRequest).Json(&entities.Message{
    68  			Type:    "error",
    69  			Message: "Error aprrove post",
    70  		})
    71  	}
    72  
    73  	return c.Status(http.StatusOK).Json(&entities.Message{
    74  		Type:    "success",
    75  		Message: "Post aprroved",
    76  	})
    77  }