OnlineLibrary

Форк
0
118 строк · 2.5 Кб
1
package library
2

3
import (
4
	"errors"
5
	"fmt"
6

7
	"OnlineLibrary/internal/config"
8
	"OnlineLibrary/internal/content"
9

10
	"gitverse.ru/kvark128/dodp"
11
)
12

13
type Library struct {
14
	*dodp.Client
15
	service           *config.Service
16
	serviceAttributes *dodp.ServiceAttributes
17
	conf              *config.Config
18
}
19

20
func NewLibrary(conf *config.Config, service *config.Service) (*Library, error) {
21
	client := dodp.NewClient(service.URL, config.HTTPTimeout)
22
	success, err := client.LogOn(service.Username, service.Password)
23
	if err != nil {
24
		return nil, err
25
	}
26

27
	if !success {
28
		return nil, fmt.Errorf("logOn operation returned false")
29
	}
30

31
	serviceAttributes, err := client.GetServiceAttributes()
32
	if err != nil {
33
		return nil, err
34
	}
35

36
	success, err = client.SetReadingSystemAttributes(&config.ReadingSystemAttributes)
37
	if err != nil {
38
		return nil, err
39
	}
40

41
	if !success {
42
		return nil, fmt.Errorf("setReadingSystemAttributes operation returned false")
43
	}
44

45
	library := &Library{
46
		Client:            client,
47
		service:           service,
48
		serviceAttributes: serviceAttributes,
49
		conf:              conf,
50
	}
51

52
	return library, nil
53
}
54

55
func (l *Library) ContentList(id string) (*content.List, error) {
56
	contentList, err := l.GetContentList(id, 0, -1)
57
	if err != nil {
58
		return nil, err
59
	}
60

61
	lst := &content.List{
62
		Name: contentList.Label.Text,
63
		ID:   contentList.ID,
64
	}
65

66
	for _, contentItem := range contentList.ContentItems {
67
		item := NewContentItemWithLabel(l, contentItem.ID, contentItem.Label.Text)
68
		lst.Items = append(lst.Items, item)
69
	}
70

71
	l.service.OpenBookshelfOnLogin = id == dodp.Issued
72
	return lst, nil
73
}
74

75
func (l *Library) LastContentListID() (string, error) {
76
	if !l.service.OpenBookshelfOnLogin {
77
		return "", errors.New("last content list not available")
78
	}
79
	return dodp.Issued, nil
80
}
81

82
func (l *Library) ContentItem(id string) (content.Item, error) {
83
	item := NewContentItem(l, id)
84
	return item, nil
85
}
86

87
func (l *Library) LastContentItemID() (string, error) {
88
	book, err := l.service.RecentBooks.LastBook()
89
	if err != nil {
90
		return "", err
91
	}
92
	return book.ID, nil
93
}
94

95
func (l *Library) Tidy(ids []string) {
96
	l.service.RecentBooks.Tidy(ids)
97
}
98

99
func (l *Library) Terminate() error {
100
	_, err := l.LogOff()
101
	return err
102
}
103

104
func (l *Library) Service() *config.Service {
105
	return l.service
106
}
107

108
func (l *Library) ServiceAttributes() *dodp.ServiceAttributes {
109
	return l.serviceAttributes
110
}
111

112
func (l *Library) GetQuestions(ur *dodp.UserResponses) (*dodp.Questions, error) {
113
	questions, err := l.Client.GetQuestions(ur)
114
	if err == nil {
115
		l.service.OpenBookshelfOnLogin = false
116
	}
117
	return questions, err
118
}
119

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.