Add status
This commit is contained in:
parent
9425061964
commit
db62fa1bf7
12 changed files with 130 additions and 92 deletions
|
|
@ -37,7 +37,7 @@ func (s *Storage) populateStatus(rows *sql.Rows) (model.Status, error) {
|
|||
return status, nil
|
||||
}
|
||||
|
||||
func (s *Storage) CreatePost(status model.Status) (int64, error) {
|
||||
func (s *Storage) CreateStatus(status model.Status) (int64, error) {
|
||||
var lid int64
|
||||
err := s.db.QueryRow(`INSERT INTO statuses (author, content) VALUES ($1, $2) RETURNING id`,
|
||||
status.User, status.Content).Scan(&lid)
|
||||
|
|
|
|||
|
|
@ -1,34 +0,0 @@
|
|||
package form
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type EditorForm struct {
|
||||
Name string
|
||||
Content string
|
||||
}
|
||||
|
||||
//func (f *HomepageForm) Validate() error {
|
||||
// if f.Password != f.Confirm {
|
||||
// return errors.New("password doesn't match confirmation")
|
||||
// }
|
||||
// if len(f.Username) < 3 {
|
||||
// return errors.New("username needs to be at least 3 characters")
|
||||
// }
|
||||
// match, _ := regexp.MatchString("^[a-z0-9-_]+$", f.Username)
|
||||
// if !match {
|
||||
// return errors.New("only lowercase letters and digits are accepted for username")
|
||||
// }
|
||||
// if len(f.Password) < 6 {
|
||||
// return errors.New("password needs to be at least 6 characters")
|
||||
// }
|
||||
// return nil
|
||||
//}
|
||||
|
||||
func NewEditorForm(r *http.Request) *EditorForm {
|
||||
return &EditorForm{
|
||||
Name: r.FormValue("name"),
|
||||
Content: r.FormValue("content"),
|
||||
}
|
||||
}
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
package form
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type FolderForm struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
func NewFolderForm(r *http.Request) *FolderForm {
|
||||
return &FolderForm{
|
||||
Name: r.FormValue("name"),
|
||||
}
|
||||
}
|
||||
|
|
@ -6,7 +6,7 @@ import (
|
|||
"regexp"
|
||||
)
|
||||
|
||||
type HomepageForm struct {
|
||||
type RegisterForm struct {
|
||||
Username string
|
||||
Password string
|
||||
Confirm string
|
||||
|
|
@ -14,7 +14,7 @@ type HomepageForm struct {
|
|||
Error string
|
||||
}
|
||||
|
||||
func (f *HomepageForm) Validate() error {
|
||||
func (f *RegisterForm) Validate() error {
|
||||
if f.Password != f.Confirm {
|
||||
return errors.New("password doesn't match confirmation")
|
||||
}
|
||||
|
|
@ -31,8 +31,8 @@ func (f *HomepageForm) Validate() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func NewHomepageForm(r *http.Request) *HomepageForm {
|
||||
return &HomepageForm{
|
||||
func NewRegisterForm(r *http.Request) *RegisterForm {
|
||||
return &RegisterForm{
|
||||
Username: r.FormValue("name"),
|
||||
Password: r.FormValue("password"),
|
||||
Confirm: r.FormValue("password-confirm"),
|
||||
16
web/handler/form/status.go
Normal file
16
web/handler/form/status.go
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
package form
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type StatusForm struct {
|
||||
Content string
|
||||
Error string
|
||||
}
|
||||
|
||||
func NewStatusForm(r *http.Request) *StatusForm {
|
||||
return &StatusForm{
|
||||
Content: r.FormValue("content"),
|
||||
}
|
||||
}
|
||||
|
|
@ -1,26 +0,0 @@
|
|||
package form
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type UploadForm struct {
|
||||
Filename string
|
||||
File io.Reader
|
||||
}
|
||||
|
||||
func NewUploadForm(r *http.Request) (*UploadForm, error) {
|
||||
if err := r.ParseMultipartForm(2 << 20); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
file, handler, err := r.FormFile("file")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
return &UploadForm{
|
||||
Filename: handler.Filename,
|
||||
File: file,
|
||||
}, err
|
||||
}
|
||||
|
|
@ -46,6 +46,14 @@ type Handler struct {
|
|||
// return user, err
|
||||
//}
|
||||
|
||||
func (h *Handler) getUser(r *http.Request) (string, error) {
|
||||
user, err := h.sess.Get(r)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func New(cfg *config.Config, sess *session.Session, data *storage.Storage) (http.Handler, error) {
|
||||
router := mux.NewRouter()
|
||||
h := &Handler{
|
||||
|
|
@ -60,19 +68,11 @@ func New(cfg *config.Config, sess *session.Session, data *storage.Storage) (http
|
|||
router.HandleFunc("/", h.showIndexView).Methods(http.MethodGet)
|
||||
router.HandleFunc("/login", h.showLoginView).Methods(http.MethodGet)
|
||||
router.HandleFunc("/check-login", h.checkLogin).Methods(http.MethodPost)
|
||||
//router.HandleFunc("/help", h.showHelpView).Methods(http.MethodGet)
|
||||
//router.HandleFunc("/profile/{username}", h.showProfileView).Methods(http.MethodGet)
|
||||
router.HandleFunc("/register", h.handleRegister)
|
||||
//router.HandleFunc("/editor", h.handleEditor)
|
||||
//router.HandleFunc("/upload", h.upload)
|
||||
//router.HandleFunc("/files", h.handleFiles)
|
||||
//router.HandleFunc("/new-folder", h.handleNewFolder)
|
||||
//router.HandleFunc("/new-file", h.handleNewFile)
|
||||
//router.HandleFunc("/homepages", h.showHomepagesView).Methods(http.MethodGet)
|
||||
//router.HandleFunc("/rename", h.handleRename)
|
||||
//router.HandleFunc("/delete", h.handleDelete).Methods(http.MethodPost)
|
||||
router.HandleFunc("/logout", h.logout).Methods(http.MethodGet)
|
||||
//router.PathPrefix("/").Handler(http.FileServer(http.Dir(cfg.AssetsDir)))
|
||||
|
||||
router.HandleFunc("/statuses/new", h.showNewStatusView).Methods(http.MethodGet)
|
||||
router.HandleFunc("/statuses/save", h.saveStatus).Methods(http.MethodPost)
|
||||
|
||||
return router, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,21 @@
|
|||
package handler
|
||||
|
||||
var TplMap = map[string]string{
|
||||
"create_status": `{{ define "content" }}
|
||||
<h1>New status</h1>
|
||||
{{ if .form.Error }}
|
||||
<p>{{ .form.Error }}</p>
|
||||
{{ end }}
|
||||
{{ if .flash }}
|
||||
<p>{{ .flash }}</p>
|
||||
{{ end }}
|
||||
<form action="/statuses/save" method="post">
|
||||
<div class="field">
|
||||
<input type="text" name="content" placeholder="What's new?" required autofocus />
|
||||
</div>
|
||||
<input type="submit" value="Submit">
|
||||
</form>
|
||||
{{ end }}`,
|
||||
"index": `{{ define "content" }}
|
||||
<p>This is the index</p>
|
||||
{{ end }}`,
|
||||
|
|
|
|||
15
web/handler/html/create_status.html
Normal file
15
web/handler/html/create_status.html
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
{{ define "content" }}
|
||||
<h1>New status</h1>
|
||||
{{ if .form.Error }}
|
||||
<p>{{ .form.Error }}</p>
|
||||
{{ end }}
|
||||
{{ if .flash }}
|
||||
<p>{{ .flash }}</p>
|
||||
{{ end }}
|
||||
<form action="/statuses/save" method="post">
|
||||
<div class="field">
|
||||
<input type="text" name="content" placeholder="What's new?" required autofocus />
|
||||
</div>
|
||||
<input type="submit" value="Submit">
|
||||
</form>
|
||||
{{ end }}
|
||||
|
|
@ -82,7 +82,7 @@ func buildIndex(t *template.Template, name string) []byte {
|
|||
func (h *Handler) register(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case "POST":
|
||||
f := form.NewHomepageForm(r)
|
||||
f := form.NewRegisterForm(r)
|
||||
showError := func(err error) {
|
||||
f.Error = err.Error()
|
||||
h.renderLayout(w, "register", map[string]interface{}{"form": *f}, "")
|
||||
|
|
|
|||
24
web/handler/status_create.go
Normal file
24
web/handler/status_create.go
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
package handler
|
||||
|
||||
import "net/http"
|
||||
|
||||
func (h *Handler) showNewStatusView(w http.ResponseWriter, r *http.Request) {
|
||||
user, err := h.getUser(r)
|
||||
if err != nil {
|
||||
unauthorized(w)
|
||||
return
|
||||
}
|
||||
session, err := h.sess.Store.Get(r, "ichi")
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
flash := ""
|
||||
if flashes := session.Flashes(); len(flashes) > 0 {
|
||||
flash = flashes[0].(string)
|
||||
}
|
||||
session.Save(r, w)
|
||||
h.renderLayout(w, "create_status", map[string]interface{}{
|
||||
"flash": flash,
|
||||
}, user)
|
||||
}
|
||||
43
web/handler/status_save.go
Normal file
43
web/handler/status_save.go
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"status/model"
|
||||
"status/web/handler/form"
|
||||
)
|
||||
|
||||
func (h *Handler) saveStatus(w http.ResponseWriter, r *http.Request) {
|
||||
user, err := h.getUser(r)
|
||||
if err != nil {
|
||||
unauthorized(w)
|
||||
return
|
||||
}
|
||||
f := form.NewStatusForm(r)
|
||||
status := model.Status{
|
||||
User: user,
|
||||
Content: f.Content,
|
||||
}
|
||||
if err := status.Validate(); err != nil {
|
||||
f.Error = err.Error()
|
||||
h.renderLayout(w, "create_status", map[string]interface{}{
|
||||
"form": f,
|
||||
}, "")
|
||||
return
|
||||
}
|
||||
if _, err := h.storage.CreateStatus(status); err != nil {
|
||||
serverError(w, err)
|
||||
return
|
||||
}
|
||||
session, err := h.sess.Store.Get(r, "ichi")
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
session.AddFlash("Status updated!")
|
||||
err = session.Save(r, w)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "/statuses/new", http.StatusFound)
|
||||
}
|
||||
Loading…
Reference in a new issue