/
basic.handler.go
50 lines (42 loc) · 1.21 KB
/
basic.handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package handler
import (
"net/http"
"github.com/crowmw/risiti/service"
"github.com/crowmw/risiti/view/home"
"github.com/crowmw/risiti/view/signin"
"github.com/crowmw/risiti/view/signup"
"github.com/crowmw/risiti/view/uploadForm"
)
type BasicHandler struct {
ReceiptService service.IReceiptService
UserService service.IUserService
AuthService service.AuthService
}
func NewBasicHandler(s service.IReceiptService, u service.IUserService, a service.AuthService) *BasicHandler {
return &BasicHandler{
ReceiptService: s,
UserService: u,
AuthService: a,
}
}
func (h *BasicHandler) GetHome(w http.ResponseWriter, r *http.Request) {
// Check is any user exists in system
anyUserExists, err := h.UserService.AnyExists()
if err != nil {
OnError(w, err, "Internal Server Error", http.StatusInternalServerError)
return
}
if !anyUserExists {
RenderView(w, r, signup.Show("", ""), "/signup")
return
}
// Authorize request
if err := h.AuthService.Authorize(r); err != nil {
RenderView(w, r, signin.Show("", ""), "/signin")
return
}
RenderView(w, r, home.Show(), "/")
}
func (h *BasicHandler) GetUpload(w http.ResponseWriter, r *http.Request) {
RenderView(w, r, uploadForm.Show(""), "/upload")
}