Debian Codegraph @

plugins/gnostic-go-generator/examples/v3.0/bookstore/service/service.go

raw symbols SCIP
1 /*
2 Copyright 2017 Google Inc. All Rights Reserved.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package main
18
19 import (
20 "errors"
21 "fmt"
22 "net/http"
23 "sync"
24
25 "github.com/googleapis/gnostic/plugins/gnostic-go-generator/examples/v3.0/bookstore/bookstore"
26 )
27
28 //
29 // The Service type implements a bookstore service.
30 // All objects are managed in an in-memory non-persistent store.
31 //
32 type Service struct {
33 // shelves are stored in a map keyed by shelf id
34 // books are stored in a two level map, keyed first by shelf id and then by book id
35 Shelves map[int64]*bookstore.Shelf
36 Books map[int64]map[int64]*bookstore.Book
37 LastShelfID int64 // the id of the last shelf that was added
38 LastBookID int64 // the id of the last book that was added
39 Mutex sync.Mutex // global mutex to synchronize service access
40 }
41
42 func NewService() *Service {
43 return &Service{
44 Shelves: make(map[int64]*bookstore.Shelf),
45 Books: make(map[int64]map[int64]*bookstore.Book),
46 }
47 }
48
49 func (service *Service) ListShelves(responses *bookstore.ListShelvesResponses) (err error) {
50 service.Mutex.Lock()
51 defer service.Mutex.Unlock()
52 // copy shelf ids from Shelves map keys
53 shelves := make([]bookstore.Shelf, 0, len(service.Shelves))
54 for _, shelf := range service.Shelves {
55 shelves = append(shelves, *shelf)
56 }
57 response := &bookstore.ListShelvesResponse{}
58 response.Shelves = shelves
59 (*responses).OK = response
60 return err
61 }
62
63 func (service *Service) CreateShelf(parameters *bookstore.CreateShelfParameters, responses *bookstore.CreateShelfResponses) (err error) {
64 service.Mutex.Lock()
65 defer service.Mutex.Unlock()
66 // assign an id and name to a shelf and add it to the Shelves map.
67 shelf := parameters.Shelf
68 service.LastShelfID++
69 sid := service.LastShelfID
70 shelf.Name = fmt.Sprintf("shelves/%d", sid)
71 service.Shelves[sid] = &shelf
72 (*responses).OK = &shelf
73 return err
74 }
75
76 func (service *Service) DeleteShelves() (err error) {
77 service.Mutex.Lock()
78 defer service.Mutex.Unlock()
79 // delete everything by reinitializing the Shelves and Books maps.
80 service.Shelves = make(map[int64]*bookstore.Shelf)
81 service.Books = make(map[int64]map[int64]*bookstore.Book)
82 service.LastShelfID = 0
83 service.LastBookID = 0
84 return nil
85 }
86
87 func (service *Service) GetShelf(parameters *bookstore.GetShelfParameters, responses *bookstore.GetShelfResponses) (err error) {
88 service.Mutex.Lock()
89 defer service.Mutex.Unlock()
90 // look up a shelf from the Shelves map.
91 shelf, err := service.getShelf(parameters.Shelf)
92 if err != nil {
93 (*responses).Default = &bookstore.Error{Code: int32(http.StatusNotFound), Message: err.Error()}
94 return nil
95 } else {
96 (*responses).OK = shelf
97 return nil
98 }
99 }
100
101 func (service *Service) DeleteShelf(parameters *bookstore.DeleteShelfParameters) (err error) {
102 service.Mutex.Lock()
103 defer service.Mutex.Unlock()
104 // delete a shelf by removing the shelf from the Shelves map and the associated books from the Books map.
105 delete(service.Shelves, parameters.Shelf)
106 delete(service.Books, parameters.Shelf)
107 return nil
108 }
109
110 func (service *Service) ListBooks(parameters *bookstore.ListBooksParameters, responses *bookstore.ListBooksResponses) (err error) {
111 service.Mutex.Lock()
112 defer service.Mutex.Unlock()
113 // list the books in a shelf
114 _, err = service.getShelf(parameters.Shelf)
115 if err != nil {
116 (*responses).Default = &bookstore.Error{Code: int32(http.StatusNotFound), Message: err.Error()}
117 return nil
118 }
119 shelfBooks := service.Books[parameters.Shelf]
120 books := make([]bookstore.Book, 0, len(shelfBooks))
121 for _, book := range shelfBooks {
122 books = append(books, *book)
123 }
124 response := &bookstore.ListBooksResponse{}
125 response.Books = books
126 (*responses).OK = response
127 return nil
128 }
129
130 func (service *Service) CreateBook(parameters *bookstore.CreateBookParameters, responses *bookstore.CreateBookResponses) (err error) {
131 service.Mutex.Lock()
132 defer service.Mutex.Unlock()
133 // return "not found" if the shelf doesn't exist
134 shelf, err := service.getShelf(parameters.Shelf)
135 if err != nil {
136 (*responses).Default = &bookstore.Error{Code: int32(http.StatusNotFound), Message: err.Error()}
137 return nil
138 }
139 // assign an id and name to a book and add it to the Books map.
140 service.LastBookID++
141 bid := service.LastBookID
142 book := parameters.Book
143 book.Name = fmt.Sprintf("%s/books/%d", shelf.Name, bid)
144 if service.Books[parameters.Shelf] == nil {
145 service.Books[parameters.Shelf] = make(map[int64]*bookstore.Book)
146 }
147 service.Books[parameters.Shelf][bid] = &book
148 (*responses).OK = &book
149 return err
150 }
151
152 func (service *Service) GetBook(parameters *bookstore.GetBookParameters, responses *bookstore.GetBookResponses) (err error) {
153 service.Mutex.Lock()
154 defer service.Mutex.Unlock()
155 // get a book from the Books map
156 book, err := service.getBook(parameters.Shelf, parameters.Book)
157 if err != nil {
158 (*responses).Default = &bookstore.Error{Code: int32(http.StatusNotFound), Message: err.Error()}
159 } else {
160 (*responses).OK = book
161 }
162 return nil
163 }
164
165 func (service *Service) DeleteBook(parameters *bookstore.DeleteBookParameters) (err error) {
166 service.Mutex.Lock()
167 defer service.Mutex.Unlock()
168 // delete a book by removing the book from the Books map.
169 delete(service.Books[parameters.Shelf], parameters.Book)
170 return nil
171 }
172
173 // internal helpers
174
175 func (service *Service) getShelf(sid int64) (shelf *bookstore.Shelf, err error) {
176 shelf, ok := service.Shelves[sid]
177 if !ok {
178 return nil, errors.New(fmt.Sprintf("Couldn't find shelf %d", sid))
179 } else {
180 return shelf, nil
181 }
182 }
183
184 func (service *Service) getBook(sid int64, bid int64) (book *bookstore.Book, err error) {
185 _, err = service.getShelf(sid)
186 if err != nil {
187 return nil, err
188 }
189 book, ok := service.Books[sid][bid]
190 if !ok {
191 return nil, errors.New(fmt.Sprintf("Couldn't find book %d on shelf %d", bid, sid))
192 } else {
193 return book, nil
194 }
195 }