1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-03 23:56:22 -07:00

Add stuff to be able to create a board section from the web interface

This commit is contained in:
Eggbertx 2022-08-07 22:32:37 -07:00
parent 8e3a7c493a
commit dc6cac7213
3 changed files with 44 additions and 20 deletions

View file

@ -9,7 +9,7 @@ import (
"github.com/gochan-org/gochan/pkg/gclog" "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 // 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) // 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 { func (board *Board) UpdateID() error {
@ -134,7 +134,7 @@ func DoesBoardExistByDir(dir string) bool {
return count > 0 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 // 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 // The code should be changed to reflect the new database design
func GetAllBoards() ([]Board, error) { func GetAllBoards() ([]Board, error) {
@ -156,7 +156,7 @@ func GetAllBoards() ([]Board, error) {
return boards, nil 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 // 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 // The code should be changed to reflect the new database design
func GetBoardFromID(boardID int) (Board, error) { func GetBoardFromID(boardID int) (Board, error) {
@ -165,7 +165,7 @@ func GetBoardFromID(boardID int) (Board, error) {
return board, err 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) { func GetBoardFromPostID(postID int) (boardURI string, wasFound bool, err error) {
const query = `SELECT board.uri FROM DBPREFIXboards as board const query = `SELECT board.uri FROM DBPREFIXboards as board
JOIN ( JOIN (
@ -186,7 +186,7 @@ func getBoardIDFromURI(URI string) (id int, err error) {
return id, err 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 { func CreateDefaultBoardIfNoneExist() error {
const sqlStr = `SELECT COUNT(id) FROM DBPREFIXboards` const sqlStr = `SELECT COUNT(id) FROM DBPREFIXboards`
var count int var count int
@ -208,7 +208,7 @@ func CreateDefaultBoardIfNoneExist() error {
return nil 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 // 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 // The code should be changed to reflect the new database design
func CreateBoard(values *Board) error { func CreateBoard(values *Board) error {
@ -238,7 +238,7 @@ func CreateBoard(values *Board) error {
return QueryRowSQL(sqlSELECT, interfaceSlice(values.Dir), interfaceSlice(&values.ID)) 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) { func GetBoardUris() (URIS []string, err error) {
const sql = `SELECT uri FROM DBPREFIXboards` const sql = `SELECT uri FROM DBPREFIXboards`
rows, err := QuerySQL(sql) rows, err := QuerySQL(sql)
@ -256,7 +256,7 @@ func GetBoardUris() (URIS []string, err error) {
return uris, nil return uris, nil
} }
//GetAllSections gets a list of all existing sections // GetAllSections gets a list of all existing sections
func GetAllSections() ([]BoardSection, error) { func GetAllSections() ([]BoardSection, error) {
const sql = `SELECT id, name, abbreviation, position, hidden FROM DBPREFIXsections ORDER BY position ASC, name ASC` const sql = `SELECT id, name, abbreviation, position, hidden FROM DBPREFIXsections ORDER BY position ASC, name ASC`
rows, err := QuerySQL(sql) rows, err := QuerySQL(sql)
@ -293,7 +293,7 @@ func getNextSectionListOrder() (int, error) {
return ID, err 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) { func GetOrCreateDefaultSectionID() (sectionID int, err error) {
const SQL = `SELECT id FROM DBPREFIXsections WHERE name = 'Main'` const SQL = `SELECT id FROM DBPREFIXsections WHERE name = 'Main'`
var ID int var ID int
@ -304,9 +304,9 @@ func GetOrCreateDefaultSectionID() (sectionID int, err error) {
if err != nil { if err != nil {
return 0, err return 0, err
} }
board := BoardSection{Name: "Main", Abbreviation: "Main", Hidden: false, ListOrder: ID} section := BoardSection{Name: "Main", Abbreviation: "Main", Hidden: false, ListOrder: ID}
err = CreateSection(&board) err = CreateSection(&section)
return board.ID, err return section.ID, err
} }
if err != nil { if err != nil {
return 0, err //other error return 0, err //other error
@ -314,7 +314,7 @@ func GetOrCreateDefaultSectionID() (sectionID int, err error) {
return ID, nil 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 { func CreateSection(section *BoardSection) error {
const sqlINSERT = `INSERT INTO DBPREFIXsections (name, abbreviation, hidden, position) VALUES (?,?,?,?)` const sqlINSERT = `INSERT INTO DBPREFIXsections (name, abbreviation, hidden, position) VALUES (?,?,?,?)`
const sqlSELECT = `SELECT id FROM DBPREFIXsections WHERE position = ?` const sqlSELECT = `SELECT id FROM DBPREFIXsections WHERE position = ?`

View file

@ -562,9 +562,32 @@ var actions = []Action{
Permissions: AdminPerms, Permissions: AdminPerms,
JSONoutput: NoJSON, JSONoutput: NoJSON,
Callback: func(writer http.ResponseWriter, request *http.Request, wantsJSON bool) (output interface{}, err error) { Callback: func(writer http.ResponseWriter, request *http.Request, wantsJSON bool) (output interface{}, err error) {
sections, err := gcsql.GetAllSections() if request.PostForm.Get("create_section_btn") != "" {
if err != nil { // user is creating a new board section
return "", err 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 { for i, input := range request.Form {
@ -574,7 +597,7 @@ var actions = []Action{
if err = serverutil.MinifyTemplate(gctemplates.ManageSections, map[string]interface{}{ if err = serverutil.MinifyTemplate(gctemplates.ManageSections, map[string]interface{}{
"webroot": config.GetSystemCriticalConfig().WebRoot, "webroot": config.GetSystemCriticalConfig().WebRoot,
"site_config": config.GetSiteConfig(), "site_config": config.GetSiteConfig(),
"sections": sections, "sections": gcsql.AllSections,
}, pageBuffer, "text/html"); err != nil { }, pageBuffer, "text/html"); err != nil {
return "", err return "", err
} }

View file

@ -1,9 +1,10 @@
<form action="{{.webroot}}manage?action=boardsections" method="POST"> <form action="{{.webroot}}manage?action=boardsections" method="POST">
<h2>New section</h2> <h2>New section</h2>
<table> <table>
<tr><td>Name:</td><td><input type="text" name="newname" id="newname"></td></tr> <tr><td>Name:</td><td><input type="text" name="newname"></td></tr>
<tr><td>Abbreviation:</td><td><input type="text" name="newabbr" id="newabbr"></td></tr> <tr><td>Abbreviation:</td><td><input type="text" name="newabbr"></td></tr>
<tr><td>Hidden:</td><td><input type="checkbox" name="newhidden" id="newhidden"></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> </table>
<input type="submit" name="create_section_btn" value="Create section"> <input type="submit" name="create_section_btn" value="Create section">
</form> </form>