miniflux/server/ui/controller/category.go

258 lines
6.7 KiB
Go
Raw Normal View History

2017-11-20 06:10:04 +01:00
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package controller
import (
"errors"
"log"
2017-11-20 06:10:04 +01:00
"github.com/miniflux/miniflux2/model"
"github.com/miniflux/miniflux2/server/core"
"github.com/miniflux/miniflux2/server/ui/form"
)
// ShowCategories shows the page with all categories.
2017-11-20 06:10:04 +01:00
func (c *Controller) ShowCategories(ctx *core.Context, request *core.Request, response *core.Response) {
args, err := c.getCommonTemplateArgs(ctx)
if err != nil {
2017-11-22 03:30:16 +01:00
response.HTML().ServerError(err)
2017-11-20 06:10:04 +01:00
return
}
2017-11-22 03:37:08 +01:00
user := ctx.LoggedUser()
2017-11-20 06:10:04 +01:00
categories, err := c.store.GetCategoriesWithFeedCount(user.ID)
if err != nil {
2017-11-22 03:30:16 +01:00
response.HTML().ServerError(err)
2017-11-20 06:10:04 +01:00
return
}
2017-11-22 03:30:16 +01:00
response.HTML().Render("categories", args.Merge(tplParams{
2017-11-20 06:10:04 +01:00
"categories": categories,
"total": len(categories),
"menu": "categories",
}))
}
// ShowCategoryEntries shows all entries for the given category.
2017-11-20 06:10:04 +01:00
func (c *Controller) ShowCategoryEntries(ctx *core.Context, request *core.Request, response *core.Response) {
2017-11-22 03:37:08 +01:00
user := ctx.LoggedUser()
2017-11-22 03:14:45 +01:00
offset := request.QueryIntegerParam("offset", 0)
2017-11-20 06:10:04 +01:00
args, err := c.getCommonTemplateArgs(ctx)
if err != nil {
2017-11-22 03:30:16 +01:00
response.HTML().ServerError(err)
2017-11-20 06:10:04 +01:00
return
}
category, err := c.getCategoryFromURL(ctx, request, response)
if err != nil {
return
}
builder := c.store.GetEntryQueryBuilder(user.ID, user.Timezone)
builder.WithCategoryID(category.ID)
builder.WithOrder(model.DefaultSortingOrder)
builder.WithDirection(model.DefaultSortingDirection)
builder.WithoutStatus(model.EntryStatusRemoved)
2017-11-20 06:10:04 +01:00
builder.WithOffset(offset)
builder.WithLimit(NbItemsPerPage)
entries, err := builder.GetEntries()
if err != nil {
2017-11-22 03:30:16 +01:00
response.HTML().ServerError(err)
2017-11-20 06:10:04 +01:00
return
}
count, err := builder.CountEntries()
if err != nil {
2017-11-22 03:30:16 +01:00
response.HTML().ServerError(err)
2017-11-20 06:10:04 +01:00
return
}
2017-11-22 03:30:16 +01:00
response.HTML().Render("category_entries", args.Merge(tplParams{
2017-11-20 06:10:04 +01:00
"category": category,
"entries": entries,
"total": count,
2017-11-22 03:37:08 +01:00
"pagination": c.getPagination(ctx.Route("categoryEntries", "categoryID", category.ID), count, offset),
2017-11-20 06:10:04 +01:00
"menu": "categories",
}))
}
// CreateCategory shows the form to create a new category.
2017-11-20 06:10:04 +01:00
func (c *Controller) CreateCategory(ctx *core.Context, request *core.Request, response *core.Response) {
args, err := c.getCommonTemplateArgs(ctx)
if err != nil {
2017-11-22 03:30:16 +01:00
response.HTML().ServerError(err)
2017-11-20 06:10:04 +01:00
return
}
2017-11-22 03:30:16 +01:00
response.HTML().Render("create_category", args.Merge(tplParams{
2017-11-20 06:10:04 +01:00
"menu": "categories",
}))
}
// SaveCategory validate and save the new category into the database.
2017-11-20 06:10:04 +01:00
func (c *Controller) SaveCategory(ctx *core.Context, request *core.Request, response *core.Response) {
2017-11-22 03:37:08 +01:00
user := ctx.LoggedUser()
2017-11-20 06:10:04 +01:00
args, err := c.getCommonTemplateArgs(ctx)
if err != nil {
2017-11-22 03:30:16 +01:00
response.HTML().ServerError(err)
2017-11-20 06:10:04 +01:00
return
}
2017-11-22 03:14:45 +01:00
categoryForm := form.NewCategoryForm(request.Request())
2017-11-20 06:10:04 +01:00
if err := categoryForm.Validate(); err != nil {
2017-11-22 03:30:16 +01:00
response.HTML().Render("create_category", args.Merge(tplParams{
2017-11-20 06:10:04 +01:00
"errorMessage": err.Error(),
}))
return
}
duplicateCategory, err := c.store.GetCategoryByTitle(user.ID, categoryForm.Title)
if err != nil {
2017-11-22 03:30:16 +01:00
response.HTML().ServerError(err)
return
}
if duplicateCategory != nil {
2017-11-22 03:30:16 +01:00
response.HTML().Render("create_category", args.Merge(tplParams{
"errorMessage": "This category already exists.",
}))
return
}
2017-11-20 06:10:04 +01:00
category := model.Category{Title: categoryForm.Title, UserID: user.ID}
err = c.store.CreateCategory(&category)
if err != nil {
log.Println(err)
2017-11-22 03:30:16 +01:00
response.HTML().Render("create_category", args.Merge(tplParams{
2017-11-20 06:10:04 +01:00
"errorMessage": "Unable to create this category.",
}))
return
}
2017-11-22 03:37:08 +01:00
response.Redirect(ctx.Route("categories"))
2017-11-20 06:10:04 +01:00
}
// EditCategory shows the form to modify a category.
2017-11-20 06:10:04 +01:00
func (c *Controller) EditCategory(ctx *core.Context, request *core.Request, response *core.Response) {
2017-11-22 03:37:08 +01:00
user := ctx.LoggedUser()
2017-11-20 06:10:04 +01:00
category, err := c.getCategoryFromURL(ctx, request, response)
if err != nil {
log.Println(err)
return
}
args, err := c.getCategoryFormTemplateArgs(ctx, user, category, nil)
if err != nil {
2017-11-22 03:30:16 +01:00
response.HTML().ServerError(err)
2017-11-20 06:10:04 +01:00
return
}
2017-11-22 03:30:16 +01:00
response.HTML().Render("edit_category", args)
2017-11-20 06:10:04 +01:00
}
// UpdateCategory validate and update a category.
2017-11-20 06:10:04 +01:00
func (c *Controller) UpdateCategory(ctx *core.Context, request *core.Request, response *core.Response) {
2017-11-22 03:37:08 +01:00
user := ctx.LoggedUser()
2017-11-20 06:10:04 +01:00
category, err := c.getCategoryFromURL(ctx, request, response)
if err != nil {
log.Println(err)
return
}
2017-11-22 03:14:45 +01:00
categoryForm := form.NewCategoryForm(request.Request())
2017-11-20 06:10:04 +01:00
args, err := c.getCategoryFormTemplateArgs(ctx, user, category, categoryForm)
if err != nil {
2017-11-22 03:30:16 +01:00
response.HTML().ServerError(err)
2017-11-20 06:10:04 +01:00
return
}
if err := categoryForm.Validate(); err != nil {
2017-11-22 03:30:16 +01:00
response.HTML().Render("edit_category", args.Merge(tplParams{
2017-11-20 06:10:04 +01:00
"errorMessage": err.Error(),
}))
return
}
if c.store.AnotherCategoryExists(user.ID, category.ID, categoryForm.Title) {
2017-11-22 03:30:16 +01:00
response.HTML().Render("edit_category", args.Merge(tplParams{
"errorMessage": "This category already exists.",
}))
return
}
2017-11-20 06:10:04 +01:00
err = c.store.UpdateCategory(categoryForm.Merge(category))
if err != nil {
log.Println(err)
2017-11-22 03:30:16 +01:00
response.HTML().Render("edit_category", args.Merge(tplParams{
2017-11-20 06:10:04 +01:00
"errorMessage": "Unable to update this category.",
}))
return
}
2017-11-22 03:37:08 +01:00
response.Redirect(ctx.Route("categories"))
2017-11-20 06:10:04 +01:00
}
// RemoveCategory delete a category from the database.
2017-11-20 06:10:04 +01:00
func (c *Controller) RemoveCategory(ctx *core.Context, request *core.Request, response *core.Response) {
2017-11-22 03:37:08 +01:00
user := ctx.LoggedUser()
2017-11-20 06:10:04 +01:00
category, err := c.getCategoryFromURL(ctx, request, response)
if err != nil {
return
}
if err := c.store.RemoveCategory(user.ID, category.ID); err != nil {
2017-11-22 03:30:16 +01:00
response.HTML().ServerError(err)
2017-11-20 06:10:04 +01:00
return
}
2017-11-22 03:37:08 +01:00
response.Redirect(ctx.Route("categories"))
2017-11-20 06:10:04 +01:00
}
func (c *Controller) getCategoryFromURL(ctx *core.Context, request *core.Request, response *core.Response) (*model.Category, error) {
2017-11-22 03:14:45 +01:00
categoryID, err := request.IntegerParam("categoryID")
2017-11-20 06:10:04 +01:00
if err != nil {
2017-11-22 03:30:16 +01:00
response.HTML().BadRequest(err)
2017-11-20 06:10:04 +01:00
return nil, err
}
2017-11-22 03:37:08 +01:00
user := ctx.LoggedUser()
2017-11-20 06:10:04 +01:00
category, err := c.store.GetCategory(user.ID, categoryID)
if err != nil {
2017-11-22 03:30:16 +01:00
response.HTML().ServerError(err)
2017-11-20 06:10:04 +01:00
return nil, err
}
if category == nil {
2017-11-22 03:30:16 +01:00
response.HTML().NotFound()
2017-11-20 06:10:04 +01:00
return nil, errors.New("Category not found")
}
return category, nil
}
func (c *Controller) getCategoryFormTemplateArgs(ctx *core.Context, user *model.User, category *model.Category, categoryForm *form.CategoryForm) (tplParams, error) {
args, err := c.getCommonTemplateArgs(ctx)
if err != nil {
return nil, err
}
if categoryForm == nil {
args["form"] = form.CategoryForm{
Title: category.Title,
}
} else {
args["form"] = categoryForm
}
args["category"] = category
args["menu"] = "categories"
return args, nil
}