OnlineLibrary
118 строк · 2.5 Кб
1package library
2
3import (
4"errors"
5"fmt"
6
7"OnlineLibrary/internal/config"
8"OnlineLibrary/internal/content"
9
10"gitverse.ru/kvark128/dodp"
11)
12
13type Library struct {
14*dodp.Client
15service *config.Service
16serviceAttributes *dodp.ServiceAttributes
17conf *config.Config
18}
19
20func NewLibrary(conf *config.Config, service *config.Service) (*Library, error) {
21client := dodp.NewClient(service.URL, config.HTTPTimeout)
22success, err := client.LogOn(service.Username, service.Password)
23if err != nil {
24return nil, err
25}
26
27if !success {
28return nil, fmt.Errorf("logOn operation returned false")
29}
30
31serviceAttributes, err := client.GetServiceAttributes()
32if err != nil {
33return nil, err
34}
35
36success, err = client.SetReadingSystemAttributes(&config.ReadingSystemAttributes)
37if err != nil {
38return nil, err
39}
40
41if !success {
42return nil, fmt.Errorf("setReadingSystemAttributes operation returned false")
43}
44
45library := &Library{
46Client: client,
47service: service,
48serviceAttributes: serviceAttributes,
49conf: conf,
50}
51
52return library, nil
53}
54
55func (l *Library) ContentList(id string) (*content.List, error) {
56contentList, err := l.GetContentList(id, 0, -1)
57if err != nil {
58return nil, err
59}
60
61lst := &content.List{
62Name: contentList.Label.Text,
63ID: contentList.ID,
64}
65
66for _, contentItem := range contentList.ContentItems {
67item := NewContentItemWithLabel(l, contentItem.ID, contentItem.Label.Text)
68lst.Items = append(lst.Items, item)
69}
70
71l.service.OpenBookshelfOnLogin = id == dodp.Issued
72return lst, nil
73}
74
75func (l *Library) LastContentListID() (string, error) {
76if !l.service.OpenBookshelfOnLogin {
77return "", errors.New("last content list not available")
78}
79return dodp.Issued, nil
80}
81
82func (l *Library) ContentItem(id string) (content.Item, error) {
83item := NewContentItem(l, id)
84return item, nil
85}
86
87func (l *Library) LastContentItemID() (string, error) {
88book, err := l.service.RecentBooks.LastBook()
89if err != nil {
90return "", err
91}
92return book.ID, nil
93}
94
95func (l *Library) Tidy(ids []string) {
96l.service.RecentBooks.Tidy(ids)
97}
98
99func (l *Library) Terminate() error {
100_, err := l.LogOff()
101return err
102}
103
104func (l *Library) Service() *config.Service {
105return l.service
106}
107
108func (l *Library) ServiceAttributes() *dodp.ServiceAttributes {
109return l.serviceAttributes
110}
111
112func (l *Library) GetQuestions(ur *dodp.UserResponses) (*dodp.Questions, error) {
113questions, err := l.Client.GetQuestions(ur)
114if err == nil {
115l.service.OpenBookshelfOnLogin = false
116}
117return questions, err
118}
119