aboutsummaryrefslogtreecommitdiff
path: root/internal/acct/acct.go
blob: 48a71e6d890b566947dfcad39bdc203cf2922ca4 (plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package acct

import (
	"context"
	"net/http"

	"git.ofmax.li/iserv/internal/auth"
	"git.ofmax.li/iserv/internal/goog"
	"github.com/alexedwards/scs/v2"
	"golang.org/x/oauth2"
)

// handler

// Handler interface to http handler
type Handler interface {
	Register(w http.ResponseWriter, r *http.Request)
}

type acctHandler struct {
	svc Servicer
	ses *scs.SessionManager
}

func (h *acctHandler) Register(w http.ResponseWriter, r *http.Request) {
	userID := h.ses.GetString(r.Context(), "profid")
	if userID == "" {
		http.Redirect(w, r, "/l", http.StatusFound)
	}
	h.svc.GetGoogProfile(r.Context(), userID)
	return
}

// NewHandler create new instance of handler
// Servicer, session, and oauth client required
func NewHandler(service Servicer, session *scs.SessionManager) Handler {
	return &acctHandler{
		service,
		session,
	}
}

// endhandler

// model

// Profile application account profile
type Profile interface {
	ProfileKeyValues() (string, []string)
}

// endmodel

// repo
// Repo storage interface

type Repo interface {
	UpdateAcctProfile(p Profile) error
}

// endrepo

// service

type Servicer interface {
	UpdateProfile(p *Profile) error
	GetGoogProfile(ctx context.Context, id string) error
}

func NewService(repo Repo, authRepo auth.Repo, goog goog.Servicer) *service {
	return &service{
		repo,
		authRepo,
		goog,
	}
}

type service struct {
	repo     Repo
	authRepo auth.Repo
	goog     goog.Servicer
}

func (s *service) UpdateProfile(p *Profile) error {
	return nil
}

func (s *service) GetGoogProfile(ctx context.Context, id string) error {
	token, err := s.authRepo.GetProfileToken(id)
	if err != nil {
		return err
	}
	client := s.goog.UserClient(ctx, token)
	gp, _, err := s.goog.Profile(client)
	if err != nil {
		return err
	}
	s.repo.UpdateAcctProfile(gp)
	return nil
}

func (s *service) GetProfileToken(id string) (*oauth2.Token, error) {
	return s.authRepo.GetProfileToken(id)
}

// endservice