mirror of
https://github.com/Eggbertx/gochan.git
synced 2025-08-03 19:56:22 -07:00
Add stuff to be able to create a board section from the web interface
This commit is contained in:
parent
8e3a7c493a
commit
dc6cac7213
3 changed files with 44 additions and 20 deletions
|
@ -9,7 +9,7 @@ import (
|
|||
"github.com/gochan-org/gochan/pkg/gclog"
|
||||
)
|
||||
|
||||
//UpdateID takes a board struct and sets the database id according to the dir that is already set
|
||||
// UpdateID takes a board struct and sets the database id according to the dir that is already set
|
||||
// Deprecated: This method was created to support old functionality during the database refactor of april 2020
|
||||
// The code should be changed to reflect the new database design. (Just bad design in general, try to avoid directly mutating state like this)
|
||||
func (board *Board) UpdateID() error {
|
||||
|
@ -134,7 +134,7 @@ func DoesBoardExistByDir(dir string) bool {
|
|||
return count > 0
|
||||
}
|
||||
|
||||
//GetAllBoards gets a list of all existing boards
|
||||
// GetAllBoards gets a list of all existing boards
|
||||
// Deprecated: This method was created to support old functionality during the database refactor of april 2020
|
||||
// The code should be changed to reflect the new database design
|
||||
func GetAllBoards() ([]Board, error) {
|
||||
|
@ -156,7 +156,7 @@ func GetAllBoards() ([]Board, error) {
|
|||
return boards, nil
|
||||
}
|
||||
|
||||
//GetBoardFromID returns the board corresponding to a given id
|
||||
// GetBoardFromID returns the board corresponding to a given id
|
||||
// Deprecated: This method was created to support old functionality during the database refactor of april 2020
|
||||
// The code should be changed to reflect the new database design
|
||||
func GetBoardFromID(boardID int) (Board, error) {
|
||||
|
@ -165,7 +165,7 @@ func GetBoardFromID(boardID int) (Board, error) {
|
|||
return board, err
|
||||
}
|
||||
|
||||
//GetBoardFromPostID gets the boardURI that a given postid exists on
|
||||
// GetBoardFromPostID gets the boardURI that a given postid exists on
|
||||
func GetBoardFromPostID(postID int) (boardURI string, wasFound bool, err error) {
|
||||
const query = `SELECT board.uri FROM DBPREFIXboards as board
|
||||
JOIN (
|
||||
|
@ -186,7 +186,7 @@ func getBoardIDFromURI(URI string) (id int, err error) {
|
|||
return id, err
|
||||
}
|
||||
|
||||
//CreateDefaultBoardIfNoneExist creates a default board if no boards exist yet
|
||||
// CreateDefaultBoardIfNoneExist creates a default board if no boards exist yet
|
||||
func CreateDefaultBoardIfNoneExist() error {
|
||||
const sqlStr = `SELECT COUNT(id) FROM DBPREFIXboards`
|
||||
var count int
|
||||
|
@ -208,7 +208,7 @@ func CreateDefaultBoardIfNoneExist() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
//CreateBoard creates this board in the database if it doesnt exist already, also sets ID to correct value
|
||||
// CreateBoard creates this board in the database if it doesnt exist already, also sets ID to correct value
|
||||
// Deprecated: This method was created to support old functionality during the database refactor of april 2020
|
||||
// The code should be changed to reflect the new database design
|
||||
func CreateBoard(values *Board) error {
|
||||
|
@ -238,7 +238,7 @@ func CreateBoard(values *Board) error {
|
|||
return QueryRowSQL(sqlSELECT, interfaceSlice(values.Dir), interfaceSlice(&values.ID))
|
||||
}
|
||||
|
||||
//GetBoardUris gets a list of all existing board URIs
|
||||
// GetBoardUris gets a list of all existing board URIs
|
||||
func GetBoardUris() (URIS []string, err error) {
|
||||
const sql = `SELECT uri FROM DBPREFIXboards`
|
||||
rows, err := QuerySQL(sql)
|
||||
|
@ -256,7 +256,7 @@ func GetBoardUris() (URIS []string, err error) {
|
|||
return uris, nil
|
||||
}
|
||||
|
||||
//GetAllSections gets a list of all existing sections
|
||||
// GetAllSections gets a list of all existing sections
|
||||
func GetAllSections() ([]BoardSection, error) {
|
||||
const sql = `SELECT id, name, abbreviation, position, hidden FROM DBPREFIXsections ORDER BY position ASC, name ASC`
|
||||
rows, err := QuerySQL(sql)
|
||||
|
@ -293,7 +293,7 @@ func getNextSectionListOrder() (int, error) {
|
|||
return ID, err
|
||||
}
|
||||
|
||||
//GetOrCreateDefaultSectionID creates the default section if it does not exist yet, returns default section ID if it exists
|
||||
// GetOrCreateDefaultSectionID creates the default section if it does not exist yet, returns default section ID if it exists
|
||||
func GetOrCreateDefaultSectionID() (sectionID int, err error) {
|
||||
const SQL = `SELECT id FROM DBPREFIXsections WHERE name = 'Main'`
|
||||
var ID int
|
||||
|
@ -304,9 +304,9 @@ func GetOrCreateDefaultSectionID() (sectionID int, err error) {
|
|||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
board := BoardSection{Name: "Main", Abbreviation: "Main", Hidden: false, ListOrder: ID}
|
||||
err = CreateSection(&board)
|
||||
return board.ID, err
|
||||
section := BoardSection{Name: "Main", Abbreviation: "Main", Hidden: false, ListOrder: ID}
|
||||
err = CreateSection(§ion)
|
||||
return section.ID, err
|
||||
}
|
||||
if err != nil {
|
||||
return 0, err //other error
|
||||
|
@ -314,7 +314,7 @@ func GetOrCreateDefaultSectionID() (sectionID int, err error) {
|
|||
return ID, nil
|
||||
}
|
||||
|
||||
//CreateSection creates a section, setting the newly created id in the given struct
|
||||
// CreateSection creates a section, setting the newly created id in the given struct
|
||||
func CreateSection(section *BoardSection) error {
|
||||
const sqlINSERT = `INSERT INTO DBPREFIXsections (name, abbreviation, hidden, position) VALUES (?,?,?,?)`
|
||||
const sqlSELECT = `SELECT id FROM DBPREFIXsections WHERE position = ?`
|
||||
|
|
|
@ -562,9 +562,32 @@ var actions = []Action{
|
|||
Permissions: AdminPerms,
|
||||
JSONoutput: NoJSON,
|
||||
Callback: func(writer http.ResponseWriter, request *http.Request, wantsJSON bool) (output interface{}, err error) {
|
||||
sections, err := gcsql.GetAllSections()
|
||||
if err != nil {
|
||||
return "", err
|
||||
if request.PostForm.Get("create_section_btn") != "" {
|
||||
// user is creating a new board section
|
||||
sName := request.PostForm.Get("newname")
|
||||
sAbbr := request.PostForm.Get("newabbr")
|
||||
sHidden := request.PostForm.Get("newhidden")
|
||||
sPosition, err := strconv.Atoi(request.PostForm.Get("newposition"))
|
||||
if sName == "" || sAbbr == "" || sHidden == "" || err != nil {
|
||||
return "", &ErrStaffAction{
|
||||
ErrorField: "formerror",
|
||||
Action: "boardsections",
|
||||
Message: "Missing section title, abbreviation, or hidden status data, or invalid position",
|
||||
}
|
||||
}
|
||||
if err = gcsql.CreateSection(&gcsql.BoardSection{
|
||||
Name: sName,
|
||||
Abbreviation: sAbbr,
|
||||
Hidden: sHidden == "on",
|
||||
ListOrder: sPosition,
|
||||
}); err != nil {
|
||||
return "", &ErrStaffAction{
|
||||
ErrorField: "db",
|
||||
Action: "boardsections",
|
||||
Message: err.Error(),
|
||||
}
|
||||
}
|
||||
gcsql.ResetBoardSectionArrays()
|
||||
}
|
||||
|
||||
for i, input := range request.Form {
|
||||
|
@ -574,7 +597,7 @@ var actions = []Action{
|
|||
if err = serverutil.MinifyTemplate(gctemplates.ManageSections, map[string]interface{}{
|
||||
"webroot": config.GetSystemCriticalConfig().WebRoot,
|
||||
"site_config": config.GetSiteConfig(),
|
||||
"sections": sections,
|
||||
"sections": gcsql.AllSections,
|
||||
}, pageBuffer, "text/html"); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
<form action="{{.webroot}}manage?action=boardsections" method="POST">
|
||||
<h2>New section</h2>
|
||||
<table>
|
||||
<tr><td>Name:</td><td><input type="text" name="newname" id="newname"></td></tr>
|
||||
<tr><td>Abbreviation:</td><td><input type="text" name="newabbr" id="newabbr"></td></tr>
|
||||
<tr><td>Hidden:</td><td><input type="checkbox" name="newhidden" id="newhidden"></td></tr>
|
||||
<tr><td>Name:</td><td><input type="text" name="newname"></td></tr>
|
||||
<tr><td>Abbreviation:</td><td><input type="text" name="newabbr"></td></tr>
|
||||
<tr><td>List order</td><td><input type="number" name="newposition" value="0"/></td></tr>
|
||||
<tr><td>Hidden:</td><td><input type="checkbox" name="newhidden"></td></tr>
|
||||
</table>
|
||||
<input type="submit" name="create_section_btn" value="Create section">
|
||||
</form>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue