Check for category uniqueness before saving

This commit is contained in:
Frédéric Guillot 2017-11-21 14:57:27 -08:00
parent 5983db1a77
commit 238b9e4c85
10 changed files with 47 additions and 12 deletions

View file

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT.
// 2017-11-21 14:38:46.654499991 -0800 PST m=+0.006321563
// 2017-11-21 14:55:14.456403496 -0800 PST m=+0.037949400
package locale
@ -137,12 +137,14 @@ var Translations = map[string]string{
"no": "non",
"Are you sure?": "Êtes-vous sûr ?",
"Work in progress...": "Travail en cours...",
"This user already exists.": "Cet utilisateur existe déjà."
"This user already exists.": "Cet utilisateur existe déjà.",
"This category already exists.": "Cette catégorie existe déjà.",
"Unable to update this category.": "Impossible de mettre à jour cette catégorie."
}
`,
}
var TranslationsChecksums = map[string]string{
"en_US": "6fe95384260941e8a5a3c695a655a932e0a8a6a572c1e45cb2b1ae8baa01b897",
"fr_FR": "bfa4a8bb15bd6016da8b9b5f851121a4ae278e2a195cd2edc4737ff075d28f02",
"fr_FR": "5c8c2c5e35a17a7dd3c30596b73342f70950a3bbce00786d43ccba01b96ea672",
}

View file

@ -121,5 +121,7 @@
"no": "non",
"Are you sure?": "Êtes-vous sûr ?",
"Work in progress...": "Travail en cours...",
"This user already exists.": "Cet utilisateur existe déjà."
"This user already exists.": "Cet utilisateur existe déjà.",
"This category already exists.": "Cette catégorie existe déjà.",
"Unable to update this category.": "Impossible de mettre à jour cette catégorie."
}

View file

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT.
// 2017-11-21 14:38:46.651098874 -0800 PST m=+0.002920446
// 2017-11-21 14:55:14.42928305 -0800 PST m=+0.010828954
package static

View file

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT.
// 2017-11-21 14:38:46.651532115 -0800 PST m=+0.003353687
// 2017-11-21 14:55:14.43289693 -0800 PST m=+0.014442834
package static

View file

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT.
// 2017-11-21 14:38:46.652422004 -0800 PST m=+0.004243576
// 2017-11-21 14:55:14.43700259 -0800 PST m=+0.018548494
package static

View file

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT.
// 2017-11-21 14:38:46.654164863 -0800 PST m=+0.005986435
// 2017-11-21 14:55:14.455330256 -0800 PST m=+0.036876160
package template

View file

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT.
// 2017-11-21 14:38:46.652925155 -0800 PST m=+0.004746727
// 2017-11-21 14:55:14.438565193 -0800 PST m=+0.020111097
package template

View file

@ -6,10 +6,11 @@ package controller
import (
"errors"
"log"
"github.com/miniflux/miniflux2/model"
"github.com/miniflux/miniflux2/server/core"
"github.com/miniflux/miniflux2/server/ui/form"
"log"
)
func (c *Controller) ShowCategories(ctx *core.Context, request *core.Request, response *core.Response) {
@ -104,6 +105,19 @@ func (c *Controller) SaveCategory(ctx *core.Context, request *core.Request, resp
return
}
duplicateCategory, err := c.store.GetCategoryByTitle(user.ID, categoryForm.Title)
if err != nil {
response.Html().ServerError(err)
return
}
if duplicateCategory != nil {
response.Html().Render("create_category", args.Merge(tplParams{
"errorMessage": "This category already exists.",
}))
return
}
category := model.Category{Title: categoryForm.Title, UserID: user.ID}
err = c.store.CreateCategory(&category)
if err != nil {
@ -158,6 +172,13 @@ func (c *Controller) UpdateCategory(ctx *core.Context, request *core.Request, re
return
}
if c.store.AnotherCategoryExists(user.ID, category.ID, categoryForm.Title) {
response.Html().Render("edit_category", args.Merge(tplParams{
"errorMessage": "This category already exists.",
}))
return
}
err = c.store.UpdateCategory(categoryForm.Merge(category))
if err != nil {
log.Println(err)

View file

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT.
// 2017-11-21 14:38:46.650534269 -0800 PST m=+0.002355841
// 2017-11-21 14:55:14.420877594 -0800 PST m=+0.002423498
package sql

View file

@ -8,11 +8,21 @@ import (
"database/sql"
"errors"
"fmt"
"time"
"github.com/miniflux/miniflux2/helper"
"github.com/miniflux/miniflux2/model"
"time"
)
func (s *Storage) AnotherCategoryExists(userID, categoryID int64, title string) bool {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:AnotherCategoryExists] userID=%d, categoryID=%d, title=%s", userID, categoryID, title))
var result int
query := `SELECT count(*) as c FROM categories WHERE user_id=$1 AND id != $2 AND title=$3`
s.db.QueryRow(query, userID, categoryID, title).Scan(&result)
return result >= 1
}
func (s *Storage) CategoryExists(userID, categoryID int64) bool {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:CategoryExists] userID=%d, categoryID=%d", userID, categoryID))