First table is working

This commit is contained in:
2022-09-17 00:35:39 +02:00
parent 2485133661
commit 9df45cf83a
13 changed files with 264 additions and 187 deletions
+79 -9
View File
@@ -5,6 +5,7 @@ import (
"html/template"
"io"
"git.phc.dm.unipi.it/phc/website/appunti"
"git.phc.dm.unipi.it/phc/website/articles"
"git.phc.dm.unipi.it/phc/website/auth"
"git.phc.dm.unipi.it/phc/website/lista_utenti"
@@ -16,22 +17,51 @@ import (
)
type Service interface {
//
// Pages
//
HandleStaticPage(w io.Writer, view string, ctx Context) error
HandleUtenti() ([]*model.User, error)
HandleListaUtenti() ([]*model.ListUser, error)
// Storia
HandleStoriaPage(w io.Writer, ctx Context) error
HandleQueryAppunti(w io.Writer, query string, ctx Context) error
// Appunti
HandleAppuntiPage(w io.Writer, query string, ctx Context) error
HandleAppuntiCondivisiPage(w io.Writer, ctx Context) error
HandleDispensaPage(w io.Writer, id string, ctx Context) error
// News
HandleNewsPage(w io.Writer, ctx Context) error
// Guide
HandleGuidePage(w io.Writer, ctx Context) error
// User
HandleProfilePage(w io.Writer, ctx Context) error
// Article Pages
HandleNewsArticlePage(w io.Writer, articleID string, ctx Context) error
HandleGuideArticlePage(w io.Writer, articleID string, ctx Context) error
// RSS
HandleNewsFeedPage(w io.Writer) error
HandleGuideFeedPage(w io.Writer) error
//
// API
//
// User
HandleLogin(username, password string) (*model.Session, error)
HandleUser(token string) *model.User
HandleRequiredUser(ctx Context) (*model.User, error)
HandleProfilePage(w io.Writer, ctx Context) error
HandleNewsArticlePage(w io.Writer, articleID string, ctx Context) error
HandleGuideArticlePage(w io.Writer, articleID string, ctx Context) error
HandleNewsFeedPage(w io.Writer) error
HandleGuideFeedPage(w io.Writer) error
// User Listing
HandleUtenti() ([]*model.User, error)
HandleListaUtenti() ([]*model.ListUser, error)
// Appunti
HandleCreateDispensa(template model.Dispensa, ctx Context) (*model.Dispensa, error)
}
//
@@ -48,6 +78,7 @@ func (ctx Context) getUser() *model.User {
// Handler holds references to abstract services for easy testing provided by every module (TODO: Make every field an interface of -Service)
type DefaultHandler struct {
AuthService auth.Service
AppuntiService appunti.Service
Renderer *templates.TemplateRenderer
NewsArticlesRegistry *articles.Registry
GuideArticlesRegistry *articles.Registry
@@ -55,6 +86,8 @@ type DefaultHandler struct {
Storia storia.StoriaService
}
var _ Service = &DefaultHandler{}
func (h *DefaultHandler) HandleStaticPage(w io.Writer, view string, ctx Context) error {
return h.Renderer.Render(w, view, util.Map{
"User": ctx.getUser(),
@@ -91,7 +124,7 @@ func (h *DefaultHandler) HandleStoriaPage(w io.Writer, ctx Context) error {
})
}
func (h *DefaultHandler) HandleQueryAppunti(w io.Writer, query string, ctx Context) error {
func (h *DefaultHandler) HandleAppuntiPage(w io.Writer, query string, ctx Context) error {
return h.Renderer.Render(w, "appunti.html", util.Map{
"User": ctx.getUser(),
"Query": query,
@@ -104,6 +137,18 @@ func (h *DefaultHandler) HandleAppuntiCondivisiPage(w io.Writer, ctx Context) er
})
}
func (h *DefaultHandler) HandleDispensaPage(w io.Writer, id string, ctx Context) error {
dispensa, err := h.AppuntiService.GetDispensa(id)
if err != nil {
return err
}
return h.Renderer.Render(w, "dispensa.html", util.Map{
"User": ctx.getUser(),
"Dispensa": dispensa,
})
}
func (h *DefaultHandler) HandleNewsPage(w io.Writer, ctx Context) error {
articles, err := h.NewsArticlesRegistry.GetArticles()
if err != nil {
@@ -221,3 +266,28 @@ func (h *DefaultHandler) HandleGuideFeedPage(w io.Writer) error {
return guideFeed.WriteRss(w)
}
//
// API
//
func (h *DefaultHandler) HandleCreateDispensa(template model.Dispensa, ctx Context) (*model.Dispensa, error) {
user := ctx.getUser()
if user == nil {
return nil, ErrNoUser
}
template.OwnerId = user.Username
id, err := h.AppuntiService.CreateDispensa(template)
if err != nil {
return nil, err
}
template.Id = id
if len(template.Tags) > 0 {
h.AppuntiService.SetDispensaTags(id, template.Tags)
}
return &template, nil
}