浏览代码

ForumHandler decompose

TooManySugar 3 年之前
父节点
当前提交
8f5ab63225
共有 3 个文件被更改,包括 124 次插入68 次删除
  1. 66 1
      cmd/web/connector.go
  2. 57 67
      cmd/web/handlers.go
  3. 1 0
      cmd/web/main.go

+ 66 - 1
cmd/web/connector.go

@@ -7,6 +7,7 @@ import (
 	"html/template"
 	"io/ioutil"
 	"log"
+	"strings"
 
 	_ "github.com/go-sql-driver/mysql"
 )
@@ -358,7 +359,43 @@ func (c connection) GetLastNews(amount int64) (news []NewsItem, count int64, err
 
 }
 
-func (c connection) TakeChildFolders(parentUuid string, amount int64) (folders []Folder, err error){
+func (c connection) TakeFolderByUuid(uuid string) (folder Folder, err error) {
+	db, err := sql.Open("mysql", c.dBOpenStr)
+	if err != nil {
+		return
+	}
+	defer db.Close()
+
+	var act_query string
+
+	act_query = fmt.Sprintf("SELECT * FROM `%s`.`folders` as `folders`", c.connProperties.Db)
+	act_query += fmt.Sprintf(" WHERE `folders`.`uuid` = '%s'", uuid)
+
+	fmt.Println(act_query)
+	rows, err := db.Query(act_query)
+	if err != nil {
+		return
+	}
+	defer rows.Close()
+
+	for rows.Next() {
+		var id interface{}
+		if err := rows.Scan(
+			&id,
+			&folder.Uuid,
+			&folder.ParentUuid,
+			&folder.Type,
+			&folder.Title,
+			&folder.Snake,
+		); err != nil {
+			log.Fatal(err)
+		}
+	}
+
+	return
+}
+
+func (c connection) TakeChildFolders(parentUuid string, amount int64) (folders []Folder, err error) {
 	db, err := sql.Open("mysql", c.dBOpenStr)
 	if err != nil {
 		return
@@ -472,6 +509,34 @@ func (c connection) TakeTopicMessages(parentUuid string, amount int64) (messages
 	return
 }
 
+func (c connection)TakeForumFolderByPath(path string) (folder Folder, err error) {
+	parentUuid := RootUuid
+	folder, err = c.TakeFolderByUuid(parentUuid)
+	if err != nil {
+		return folder, fmt.Errorf("Not found root of forum")
+	}
+
+	compltedPath := "/forum"
+
+	if len(path) == 0 {
+		return
+	}
+
+	dirs := strings.Split(path,"/")
+	for _, dir :=range dirs {
+		folder, err = c.TakeChildFolderBySnake(parentUuid, dir)
+		if err != nil {
+			return folder, fmt.Errorf("ERROR: %v", err)
+		}
+		if folder == (Folder{}) {
+			return folder, fmt.Errorf("Not found %v at %s", dir, compltedPath)
+		}
+		parentUuid = folder.Uuid
+		compltedPath += fmt.Sprintf("/%v", dir)
+	}
+	return
+}
+
 /*
 func main() {
 

+ 57 - 67
cmd/web/handlers.go

@@ -12,7 +12,7 @@ import (
 
 )
 
-// Struct for torrent entry in 'New torrents' list 
+// Struct for torrent entry in 'New torrents' list
 type Entry struct {
 	Num int
 	Link string
@@ -278,81 +278,71 @@ func DownloadTorrentHandler(w http.ResponseWriter, r *http.Request) {
 
 
 func ForumHandler(w http.ResponseWriter, r *http.Request) {
-	forumpath := mux.Vars(r)["rest"]
+	forumPath := mux.Vars(r)["rest"]
+
 	w.Write([]byte("now at forum\n"))
-	fmt.Println(len(forumpath))
-	if len(forumpath) == 0 {
-		// Serve forum root
-		w.Write([]byte("forum root\n"))
-		rootUuid := "7e92ebbd-750f-4a34-ad62-bc6a4f47cbfa"
-		folders, err := dBConnector.TakeChildFolders(rootUuid, 10)
-		if err != nil {
-			w.Write([]byte("error getting folders from server"))
-			fmt.Println("ERROR:", err)
-		}
-		for _, folder := range folders {
-			w.Write([]byte(fmt.Sprintf("<a href=%s>%s</a>\n", (r.URL.String()+"/"+folder.Snake), folder.Title)))
-		}
+
+	folder, err := dBConnector.TakeForumFolderByPath(forumPath)
+	if err != nil {
+		// forumErrorTemplate
+		if strings.Contains(err.Error(), "Not found") {
+			w.Write([]byte(err.Error()))
+			return
+		} 
+		w.Write([]byte("Internal server error"))
+		fmt.Println(err)
 		return
 	}
-	if len(forumpath) > 0 {
-		// Serve subfolders
-		dirs := strings.Split(forumpath,"/")
-
-		//var currentUuid string
-		var parentUuid string
-		parentUuid = "7e92ebbd-750f-4a34-ad62-bc6a4f47cbfa"
-
-		var folder Folder
-		for _, dir :=range dirs {
-			var err error
-			folder, err = dBConnector.TakeChildFolderBySnake(parentUuid, dir)
-			if err != nil {
-				fmt.Println("ERROR:", err)
-				return
-			}
-			if folder == (Folder{}) {
-				// Child not found
-				w.Write([]byte(fmt.Sprintf("\nNot found %v", dir)))
-				return
-			}
-			parentUuid = folder.Uuid
 
-			w.Write([]byte(fmt.Sprintf("/%v", dir)))
-		}
+	w.Write([]byte(fmt.Sprintf("\nType of %v is: %d\n", folder.Title, folder.Type)))
 
-		w.Write([]byte(fmt.Sprintf("\nType of %v is: %d\n", folder.Title, folder.Type)))
+	// tempIndex := strings.LastIndex(r.URL.String(), fmt.Sprintf("/%s",folder.Snake))
+	// if tempIndex != -1 {
+	// 	//Non root path
+	// 	backUrl := r.URL.String()[:tempIndex]
+	// 	_ = backUrl
+	// }
 
-		if folder.Type == 1 {
-			// Load subfolders
-			folders, err := dBConnector.TakeChildFolders(folder.Uuid, 10)
-			if err != nil {
-				w.Write([]byte("error getting folders from server"))
-				fmt.Println("ERROR:", err)
-			}
-			for _, folder := range folders {
-				w.Write([]byte(fmt.Sprintf("<a href=%s>%s type:%d</a>\n", (r.URL.String()+"/"+folder.Snake), folder.Title, folder.Type)))
-			}
-		}
+	if folder.Type == 1 {
+		HandleForumFolder(folder, w, r)
+		return
+	}
+	
+	if folder.Type == 2 {
+		HandleForumTopic(folder, w, r)
+		return
+	}
 
-		if folder.Type == 2 {
-			// Load messages
-			messages, err := dBConnector.TakeTopicMessages(folder.Uuid, 0)
-			if err != nil {
-				w.Write([]byte("error getting messages from server"))
-				fmt.Println("ERROR:", err)
-			}
+	w.Write([]byte("Internal server error"))
+	fmt.Println("ERROR: Uncrecognized folder type -", folder.Type)
+}
 
-			if len(messages) == 0 {
-				// Should be imposible
-				w.Write([]byte("No messages in this topic"))
-				return
-			}
+func HandleForumFolder(folder Folder, w http.ResponseWriter, r *http.Request) {
+	folders, err := dBConnector.TakeChildFolders(folder.Uuid, 10)
+	if err != nil {
+		w.Write([]byte("error getting folders from server"))
+		fmt.Println("ERROR:", err)
+	}
+	for _, folder := range folders {
+		w.Write([]byte(fmt.Sprintf("<a href=%s>%s type:%d</a>\n", (r.URL.String()+"/"+folder.Snake), folder.Title, folder.Type)))
+	}
+	return
+}
 
-			for _, message := range messages {
-				w.Write([]byte(fmt.Sprintf("Author:\n%s\n Time:\n %s\n Text:\n %s\n\n", message.Author, message.Time, message.Text)))
-			}
-		}
+func HandleForumTopic(topic Folder, w http.ResponseWriter, r *http.Request) {
+	messages, err := dBConnector.TakeTopicMessages(topic.Uuid, 0)
+	if err != nil {
+		w.Write([]byte("error getting messages from server"))
+		fmt.Println("ERROR:", err)
+	}
+
+	if len(messages) == 0 {
+		// Should be imposible
+		w.Write([]byte("No messages in this topic"))
 		return
 	}
+
+	for _, message := range messages {
+		w.Write([]byte(fmt.Sprintf("Author:\n%s\n Time:\n %s\n Text:\n %s\n\n", message.Author, message.Time, message.Text)))
+	}
 }

+ 1 - 0
cmd/web/main.go

@@ -14,6 +14,7 @@ import (
 
 const (
 	NOMYSQL        = false
+	RootUuid = "7e92ebbd-750f-4a34-ad62-bc6a4f47cbfa"
 
 	//TODO в var структуру с парсом из файла JSON
 	NewIndexTemplate = "ui\\templates\\newindex.html"