Changed everything to MVC

This commit is contained in:
Jason Streifling 2024-03-09 10:25:20 +01:00
parent 88e0d5086c
commit c6b2a17220
14 changed files with 78 additions and 75 deletions

View File

@ -1,4 +1,4 @@
package data package control
import ( import (
"bytes" "bytes"

View File

@ -1,13 +1,14 @@
package data package control
import ( import (
"fmt" "fmt"
"time" "time"
"git.streifling.com/jason/rss" "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{ channel := &rss.Channel{
Title: title, Title: title,
Link: link, Link: link,

View File

@ -1,4 +1,4 @@
package data package control
import ( import (
"crypto/rand" "crypto/rand"

View File

@ -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
}

View File

@ -1,6 +0,0 @@
package data
type Tag struct {
Name string
ID int64
}

View File

@ -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
}

View File

@ -1,4 +1,4 @@
package data package model
import ( import (
"database/sql" "database/sql"

View File

@ -1,4 +1,4 @@
package data package model
import ( import (
"bufio" "bufio"

35
cmd/model/structs.go Normal file
View File

@ -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
}

View File

@ -1,4 +1,4 @@
package ui package view
import ( import (
"fmt" "fmt"
@ -7,15 +7,16 @@ import (
"net/http" "net/http"
"strconv" "strconv"
"streifling.com/jason/cpolis/cmd/data" "streifling.com/jason/cpolis/cmd/control"
"streifling.com/jason/cpolis/cmd/model"
) )
type AddUserData struct { type AddUserData struct {
*data.User *model.User
Msg string 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 || return len(user.UserName) == 0 ||
len(user.FirstName) == 0 || len(user.FirstName) == 0 ||
len(user.LastName) == 0 || len(user.LastName) == 0 ||
@ -23,7 +24,7 @@ func inputsEmpty(user *data.User, pass, pass2 string) bool {
len(pass2) == 0 len(pass2) == 0
} }
func checkUserStrings(user *data.User) (string, int, bool) { func checkUserStrings(user *model.User) (string, int, bool) {
userLen := 15 userLen := 15
nameLen := 50 nameLen := 50
@ -43,7 +44,7 @@ func CreateUser(w http.ResponseWriter, r *http.Request) {
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil) 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) { return func(w http.ResponseWriter, r *http.Request) {
role, err := strconv.Atoi(r.PostFormValue("role")) role, err := strconv.Atoi(r.PostFormValue("role"))
if err != nil { if err != nil {
@ -53,7 +54,7 @@ func AddUser(db *data.DB, s *data.CookieStore) http.HandlerFunc {
} }
htmlData := AddUserData{ htmlData := AddUserData{
User: &data.User{ User: &model.User{
UserName: r.PostFormValue("username"), UserName: r.PostFormValue("username"),
FirstName: r.PostFormValue("first-name"), FirstName: r.PostFormValue("first-name"),
LastName: r.PostFormValue("last-name"), LastName: r.PostFormValue("last-name"),
@ -99,9 +100,9 @@ func AddUser(db *data.DB, s *data.CookieStore) http.HandlerFunc {
return return
} }
if num == 0 { if num == 0 {
if htmlData.Role != data.Admin { if htmlData.Role != model.Admin {
htmlData.Msg = "Der erste Benutzer muss ein Administrator sein." 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, err := template.ParseFiles("web/templates/add-user.html")
tmpl = template.Must(tmpl, err) tmpl = template.Must(tmpl, err)
tmpl.ExecuteTemplate(w, "page-content", htmlData) tmpl.ExecuteTemplate(w, "page-content", htmlData)

View File

@ -1,4 +1,4 @@
package ui package view
import ( import (
"html/template" "html/template"
@ -6,10 +6,11 @@ import (
"net/http" "net/http"
"strconv" "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) { return func(w http.ResponseWriter, r *http.Request) {
session, err := s.Get(r, "cookie") session, err := s.Get(r, "cookie")
if err != nil { 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) { return func(w http.ResponseWriter, r *http.Request) {
tags, err := db.GetTagList() tags, err := db.GetTagList()
if err != nil { 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) { return func(w http.ResponseWriter, r *http.Request) {
article := new(data.Article) article := new(model.Article)
var err error var err error
article.Title, err = data.ConvertToPlain(r.PostFormValue("editor-title")) article.Title, err = control.ConvertToPlain(r.PostFormValue("editor-title"))
if err != nil { if err != nil {
log.Println(err) log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
article.Desc, err = data.ConvertToPlain(r.PostFormValue("editor-desc")) article.Desc, err = control.ConvertToPlain(r.PostFormValue("editor-desc"))
if err != nil { if err != nil {
log.Println(err) log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
article.Content, err = data.ConvertToHTML(r.PostFormValue("editor-text")) article.Content, err = control.ConvertToHTML(r.PostFormValue("editor-text"))
if err != nil { if err != nil {
log.Println(err) log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError) 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) { return func(w http.ResponseWriter, r *http.Request) {
articles, err := db.GetCertainArticles(false) articles, err := db.GetCertainArticles(false)
if err != nil { 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) { return func(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PostFormValue("id"), 10, 64) id, err := strconv.ParseInt(r.PostFormValue("id"), 10, 64)
if err != nil { 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) { return func(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PostFormValue("id"), 10, 64) id, err := strconv.ParseInt(r.PostFormValue("id"), 10, 64)
if err != nil { if err != nil {

View File

@ -1,10 +1,11 @@
package ui package view
import ( import (
"html/template" "html/template"
"net/http" "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) { 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) 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) { return func(w http.ResponseWriter, r *http.Request) {
db.AddTag(r.PostFormValue("tag")) db.AddTag(r.PostFormValue("tag"))

View File

@ -1,4 +1,4 @@
package ui package view
import ( import (
"html/template" "html/template"
@ -7,10 +7,10 @@ import (
"time" "time"
"git.streifling.com/jason/rss" "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) { return func(w http.ResponseWriter, r *http.Request) {
channel := &rss.Channel{ channel := &rss.Channel{
Title: title, Title: title,

View File

@ -1,4 +1,4 @@
package ui package view
import ( import (
"fmt" "fmt"
@ -6,10 +6,11 @@ import (
"log" "log"
"net/http" "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") session, err := s.Get(r, "cookie")
if err != nil { if err != nil {
return fmt.Errorf("error getting session: %v", err) 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 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) { return func(w http.ResponseWriter, r *http.Request) {
numRows, err := db.CountEntries() numRows, err := db.CountEntries()
if err != nil { 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) { return func(w http.ResponseWriter, r *http.Request) {
userName := r.PostFormValue("username") userName := r.PostFormValue("username")
password := r.PostFormValue("password") password := r.PostFormValue("password")