diff --git a/cmd/data/markdown.go b/cmd/control/markdown.go similarity index 97% rename from cmd/data/markdown.go rename to cmd/control/markdown.go index 40d432b..861a265 100644 --- a/cmd/data/markdown.go +++ b/cmd/control/markdown.go @@ -1,4 +1,4 @@ -package data +package control import ( "bytes" diff --git a/cmd/data/rss.go b/cmd/control/rss.go similarity index 89% rename from cmd/data/rss.go rename to cmd/control/rss.go index f786b2b..a143a19 100644 --- a/cmd/data/rss.go +++ b/cmd/control/rss.go @@ -1,13 +1,14 @@ -package data +package control import ( "fmt" "time" "git.streifling.com/jason/rss" + "streifling.com/jason/cpolis/cmd/data" ) -func GetChannel(db *DB, title, link, desc string) (*rss.Channel, error) { +func GetChannel(db *data.DB, title, link, desc string) (*rss.Channel, error) { channel := &rss.Channel{ Title: title, Link: link, diff --git a/cmd/data/sessions.go b/cmd/control/sessions.go similarity index 98% rename from cmd/data/sessions.go rename to cmd/control/sessions.go index 81bd283..ae5ec16 100644 --- a/cmd/data/sessions.go +++ b/cmd/control/sessions.go @@ -1,4 +1,4 @@ -package data +package control import ( "crypto/rand" diff --git a/cmd/data/articles.go b/cmd/data/articles.go deleted file mode 100644 index 0b0eb0d..0000000 --- a/cmd/data/articles.go +++ /dev/null @@ -1,15 +0,0 @@ -package data - -import ( - "time" -) - -type Article struct { - Title string - Created time.Time - Desc string - Content string - Published bool - ID int64 - AuthorID int64 -} diff --git a/cmd/data/tags.go b/cmd/data/tags.go deleted file mode 100644 index 71361eb..0000000 --- a/cmd/data/tags.go +++ /dev/null @@ -1,6 +0,0 @@ -package data - -type Tag struct { - Name string - ID int64 -} diff --git a/cmd/data/user.go b/cmd/data/user.go deleted file mode 100644 index 6e88e42..0000000 --- a/cmd/data/user.go +++ /dev/null @@ -1,16 +0,0 @@ -package data - -const ( - Admin = iota - Editor - Writer -) - -type User struct { - UserName string - FirstName string - LastName string - RejectedArticles []*Article - ID int64 - Role int -} diff --git a/cmd/data/db.go b/cmd/model/db.go similarity index 99% rename from cmd/data/db.go rename to cmd/model/db.go index 3bd0196..95b2d2f 100644 --- a/cmd/data/db.go +++ b/cmd/model/db.go @@ -1,4 +1,4 @@ -package data +package model import ( "database/sql" diff --git a/cmd/data/helpers.go b/cmd/model/helpers.go similarity index 98% rename from cmd/data/helpers.go rename to cmd/model/helpers.go index bc4b3f9..58fbde4 100644 --- a/cmd/data/helpers.go +++ b/cmd/model/helpers.go @@ -1,4 +1,4 @@ -package data +package model import ( "bufio" diff --git a/cmd/model/structs.go b/cmd/model/structs.go new file mode 100644 index 0000000..5b060ee --- /dev/null +++ b/cmd/model/structs.go @@ -0,0 +1,35 @@ +package model + +import ( + "time" +) + +const ( + Admin = iota + Editor + Writer +) + +type User struct { + UserName string + FirstName string + LastName string + RejectedArticles []*Article + ID int64 + Role int +} + +type Tag struct { + Name string + ID int64 +} + +type Article struct { + Title string + Created time.Time + Desc string + Content string + Published bool + ID int64 + AuthorID int64 +} diff --git a/cmd/ui/admin.go b/cmd/view/admin.go similarity index 89% rename from cmd/ui/admin.go rename to cmd/view/admin.go index 3ae134e..e79d7c9 100644 --- a/cmd/ui/admin.go +++ b/cmd/view/admin.go @@ -1,4 +1,4 @@ -package ui +package view import ( "fmt" @@ -7,15 +7,16 @@ import ( "net/http" "strconv" - "streifling.com/jason/cpolis/cmd/data" + "streifling.com/jason/cpolis/cmd/control" + "streifling.com/jason/cpolis/cmd/model" ) type AddUserData struct { - *data.User + *model.User Msg string } -func inputsEmpty(user *data.User, pass, pass2 string) bool { +func inputsEmpty(user *model.User, pass, pass2 string) bool { return len(user.UserName) == 0 || len(user.FirstName) == 0 || len(user.LastName) == 0 || @@ -23,7 +24,7 @@ func inputsEmpty(user *data.User, pass, pass2 string) bool { len(pass2) == 0 } -func checkUserStrings(user *data.User) (string, int, bool) { +func checkUserStrings(user *model.User) (string, int, bool) { userLen := 15 nameLen := 50 @@ -43,7 +44,7 @@ func CreateUser(w http.ResponseWriter, r *http.Request) { template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil) } -func AddUser(db *data.DB, s *data.CookieStore) http.HandlerFunc { +func AddUser(db *model.DB, s *control.CookieStore) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { role, err := strconv.Atoi(r.PostFormValue("role")) if err != nil { @@ -53,7 +54,7 @@ func AddUser(db *data.DB, s *data.CookieStore) http.HandlerFunc { } htmlData := AddUserData{ - User: &data.User{ + User: &model.User{ UserName: r.PostFormValue("username"), FirstName: r.PostFormValue("first-name"), LastName: r.PostFormValue("last-name"), @@ -99,9 +100,9 @@ func AddUser(db *data.DB, s *data.CookieStore) http.HandlerFunc { return } if num == 0 { - if htmlData.Role != data.Admin { + if htmlData.Role != model.Admin { htmlData.Msg = "Der erste Benutzer muss ein Administrator sein." - htmlData.Role = data.Admin + htmlData.Role = model.Admin tmpl, err := template.ParseFiles("web/templates/add-user.html") tmpl = template.Must(tmpl, err) tmpl.ExecuteTemplate(w, "page-content", htmlData) diff --git a/cmd/ui/articles.go b/cmd/view/articles.go similarity index 84% rename from cmd/ui/articles.go rename to cmd/view/articles.go index 1f9a7e7..d6e54e7 100644 --- a/cmd/ui/articles.go +++ b/cmd/view/articles.go @@ -1,4 +1,4 @@ -package ui +package view import ( "html/template" @@ -6,10 +6,11 @@ import ( "net/http" "strconv" - "streifling.com/jason/cpolis/cmd/data" + "streifling.com/jason/cpolis/cmd/control" + "streifling.com/jason/cpolis/cmd/model" ) -func ShowHub(s *data.CookieStore) http.HandlerFunc { +func ShowHub(s *control.CookieStore) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { session, err := s.Get(r, "cookie") if err != nil { @@ -23,7 +24,7 @@ func ShowHub(s *data.CookieStore) http.HandlerFunc { } } -func WriteArticle(db *data.DB) http.HandlerFunc { +func WriteArticle(db *model.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { tags, err := db.GetTagList() if err != nil { @@ -37,26 +38,26 @@ func WriteArticle(db *data.DB) http.HandlerFunc { } } -func FinishArticle(db *data.DB, s *data.CookieStore) http.HandlerFunc { +func FinishArticle(db *model.DB, s *control.CookieStore) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - article := new(data.Article) + article := new(model.Article) var err error - article.Title, err = data.ConvertToPlain(r.PostFormValue("editor-title")) + article.Title, err = control.ConvertToPlain(r.PostFormValue("editor-title")) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } - article.Desc, err = data.ConvertToPlain(r.PostFormValue("editor-desc")) + article.Desc, err = control.ConvertToPlain(r.PostFormValue("editor-desc")) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } - article.Content, err = data.ConvertToHTML(r.PostFormValue("editor-text")) + article.Content, err = control.ConvertToHTML(r.PostFormValue("editor-text")) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) @@ -101,7 +102,7 @@ func FinishArticle(db *data.DB, s *data.CookieStore) http.HandlerFunc { } } -func ShowUnpublishedArticles(db *data.DB) http.HandlerFunc { +func ShowUnpublishedArticles(db *model.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { articles, err := db.GetCertainArticles(false) if err != nil { @@ -114,7 +115,7 @@ func ShowUnpublishedArticles(db *data.DB) http.HandlerFunc { } } -func ReviewArticle(db *data.DB, s *data.CookieStore) http.HandlerFunc { +func ReviewArticle(db *model.DB, s *control.CookieStore) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { id, err := strconv.ParseInt(r.PostFormValue("id"), 10, 64) if err != nil { @@ -143,7 +144,7 @@ func ReviewArticle(db *data.DB, s *data.CookieStore) http.HandlerFunc { } } -func PublishArticle(db *data.DB, s *data.CookieStore) http.HandlerFunc { +func PublishArticle(db *model.DB, s *control.CookieStore) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { id, err := strconv.ParseInt(r.PostFormValue("id"), 10, 64) if err != nil { diff --git a/cmd/ui/editor.go b/cmd/view/editor.go similarity index 81% rename from cmd/ui/editor.go rename to cmd/view/editor.go index f09e6d0..7570d8a 100644 --- a/cmd/ui/editor.go +++ b/cmd/view/editor.go @@ -1,10 +1,11 @@ -package ui +package view import ( "html/template" "net/http" - "streifling.com/jason/cpolis/cmd/data" + "streifling.com/jason/cpolis/cmd/control" + "streifling.com/jason/cpolis/cmd/model" ) func CreateTag(w http.ResponseWriter, r *http.Request) { @@ -12,7 +13,7 @@ func CreateTag(w http.ResponseWriter, r *http.Request) { template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil) } -func AddTag(db *data.DB, s *data.CookieStore) http.HandlerFunc { +func AddTag(db *model.DB, s *control.CookieStore) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { db.AddTag(r.PostFormValue("tag")) diff --git a/cmd/ui/rss.go b/cmd/view/rss.go similarity index 92% rename from cmd/ui/rss.go rename to cmd/view/rss.go index b8ae1ff..1c1b2c1 100644 --- a/cmd/ui/rss.go +++ b/cmd/view/rss.go @@ -1,4 +1,4 @@ -package ui +package view import ( "html/template" @@ -7,10 +7,10 @@ import ( "time" "git.streifling.com/jason/rss" - "streifling.com/jason/cpolis/cmd/data" + "streifling.com/jason/cpolis/cmd/model" ) -func ShowRSS(db *data.DB, title, link, desc string) http.HandlerFunc { +func ShowRSS(db *model.DB, title, link, desc string) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { channel := &rss.Channel{ Title: title, diff --git a/cmd/ui/sessions.go b/cmd/view/sessions.go similarity index 86% rename from cmd/ui/sessions.go rename to cmd/view/sessions.go index 5a89573..4aad527 100644 --- a/cmd/ui/sessions.go +++ b/cmd/view/sessions.go @@ -1,4 +1,4 @@ -package ui +package view import ( "fmt" @@ -6,10 +6,11 @@ import ( "log" "net/http" - "streifling.com/jason/cpolis/cmd/data" + "streifling.com/jason/cpolis/cmd/control" + "streifling.com/jason/cpolis/cmd/model" ) -func saveSession(w http.ResponseWriter, r *http.Request, s *data.CookieStore, u *data.User) error { +func saveSession(w http.ResponseWriter, r *http.Request, s *control.CookieStore, u *model.User) error { session, err := s.Get(r, "cookie") if err != nil { return fmt.Errorf("error getting session: %v", err) @@ -26,7 +27,7 @@ func saveSession(w http.ResponseWriter, r *http.Request, s *data.CookieStore, u return nil } -func HomePage(db *data.DB, s *data.CookieStore) http.HandlerFunc { +func HomePage(db *model.DB, s *control.CookieStore) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { numRows, err := db.CountEntries() if err != nil { @@ -53,7 +54,7 @@ func HomePage(db *data.DB, s *data.CookieStore) http.HandlerFunc { } } -func Login(db *data.DB, s *data.CookieStore) http.HandlerFunc { +func Login(db *model.DB, s *control.CookieStore) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { userName := r.PostFormValue("username") password := r.PostFormValue("password")