>> |
№168165
Пример пока ещё сырого кода:
package api
import ( "os" "time" "strconv" "strings" "net/http" "text/template" "encoding/json" "path/filepath" "unicode/utf8" "3ch/backend/database" "3ch/backend/models" "3ch/backend/structs" "3ch/backend/utils" )
func SearchPosts(w http.ResponseWriter, r http.Request) { w.Header().Set("Content-Type", "text/html") var error structs.Error var searchResults structs.SearchResults var board models.Board var boardId string var text string err := r.ParseMultipartForm(32 << 20) if(err != nil) { error.Code = 100 error.Message = "Ошибка обработки запроса!" } else { boardId = r.FormValue("board") text = r.FormValue("text") } if(error.Message == "" && boardId == "") { error.Code = -1 error.Message = "Укажите борду!" } if(error.Message == "") { err := board.GetById(boardId) if(err != nil) { error.Code = -2 error.Message = "Борда не существует!" } } if(error.Message == "" && text == "") { error.Code = -3 error.Message = "Введите поисковый запрос!" } if(error.Message == "" && utf8.RuneCountInString(text) < 3) { error.Code = -4 error.Message = "Слишком короткий запрос!" } searchResults.Error = error searchResults.BoardName = board.Name searchResults.Posts.Search(boardId, text) ex, err := os.Executable() exPath := filepath.Dir(ex) tpl := template.Must(template.ParseFiles(exPath + "/templates/search.html")) tpl.Execute(w, searchResults) }
func CreatePost(w http.ResponseWriter, r http.Request) { w.Header().Set("Content-Type", "application/json") var error structs.Error var response structs.Response var postItem models.Post var parentItem models.Post var board models.Board var boardId string var num int var parent int var name string var subject string var comment string err := r.ParseMultipartForm(32 << 20) if(err != nil) { error.Code = 100 error.Message = "Ошибка обработки запроса." } else { boardId = r.FormValue("board") parent, _ = strconv.Atoi(r.FormValue("thread")) name = strings.TrimSpace(r.FormValue("name")) subject = strings.TrimSpace(r.FormValue("subject")) comment = strings.TrimSpace(r.FormValue("comment")) } if(error.Message == "" && boardId == "") { error.Code = -1 error.Message = "Укажите борду." } if(error.Message == "") { err := board.GetById(boardId) if(err != nil) { error.Code = -2 error.Message = "Борда не существует." } } if(error.Message == "" && parent > 0) { err := parentItem.GetByBoardNum(board.ID, parent) if(err != nil) { error.Code = -3 error.Message = "Тред не существует." } else if(parentItem.Parent > 0 || parentItem.Deleted != 0 || parentItem.Archived != 0) { error.Code = -4 error.Message = "Тред не существует." } else if(parentItem.Closed != 0) { error.Code = -4 error.Message = "Тред закрыт." } } if(error.Message == "" && parent == 0 && board.EnableSubject == 1 && subject == "") { error.Code = -5 error.Message = "Заполните поле \"Тема\"." } if(error.Message == "" && comment == "") { error.Code = -6 error.Message = "Вы ничего не ввели в сообщении." } if(error.Message == "") { num = utils.GetNewNum(board.ID) if(num == 0) { error.Code = -7 error.Message = "Не удалось отправить сообщение. Повторите попытку." } } if(error.Message == "") { timestamp := time.Now().Unix() ip := r.Header.Get("X-Forwarded-For") postItem.Num = num postItem.Parent = parentItem.Num postItem.Board = board.ID if(board.EnableNames == 1 && name != "") { postItem.Name = name } else { postItem.Name = board.DefaultName } postItem.Subject = subject postItem.Comment = comment postItem.Files = "[]" postItem.Timestamp = timestamp postItem.Lasthit = timestamp postItem.Ip = ip postItem.ProcessBBCode() err := postItem.Create() if(err != nil) { error.Code = -8 error.Message = "Не удалось отправить сообщение. Повторите попытку." } } if(error.Message == "") { response.Result = 1 response.Num = num database.MySQL.Exec("UPDATE boards SET last_num = ? WHERE id = ?", num, board.ID) if(parentItem.ID > 0) { database.MySQL.Exec("UPDATE posts SET lasthit = ? WHERE id = ?", postItem.Timestamp, parentItem.ID) } if(postItem.Parent > 0) { models.RenderThreadInFile(board.ID, postItem.Parent) } else { models.RenderThreadInFile(board.ID, postItem.Num) } } else { response.Result = 0 response.Error = error } jsonResponse, _ := json.MarshalIndent(response, "", " ") w.Write(jsonResponse) }
|