| 1 |
// Copyright 2017 Google Inc. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
import Bookstore |
| 16 |
|
| 17 |
class Server : Service { |
| 18 |
private var shelves : [Int64:Shelf] = [:] |
| 19 |
private var books : [Int64:[Int64:Book]] = [:] |
| 20 |
private var lastShelfIndex : Int64 = 0 |
| 21 |
private var lastBookIndex : Int64 = 0 |
| 22 |
|
| 23 |
// Return all shelves in the bookstore. |
| 24 |
func listShelves () throws -> ListShelvesResponses { |
| 25 |
let responses = ListShelvesResponses() |
| 26 |
let response = ListShelvesResponse() |
| 27 |
var shelves : [Shelf] = [] |
| 28 |
for pair in self.shelves { |
| 29 |
shelves.append(pair.value) |
| 30 |
} |
| 31 |
response.shelves = shelves |
| 32 |
responses.ok = response |
| 33 |
return responses |
| 34 |
} |
| 35 |
// Create a new shelf in the bookstore. |
| 36 |
func createShelf (_ parameters : CreateShelfParameters) throws -> CreateShelfResponses { |
| 37 |
lastShelfIndex += 1 |
| 38 |
let shelf = parameters.shelf |
| 39 |
shelf.name = "shelves/\(lastShelfIndex)" |
| 40 |
shelves[lastShelfIndex] = shelf |
| 41 |
let responses = CreateShelfResponses() |
| 42 |
responses.ok = shelf |
| 43 |
return responses |
| 44 |
} |
| 45 |
// Delete all shelves. |
| 46 |
func deleteShelves () throws { |
| 47 |
shelves = [:] |
| 48 |
books = [:] |
| 49 |
lastShelfIndex = 0 |
| 50 |
lastBookIndex = 0 |
| 51 |
} |
| 52 |
// Get a single shelf resource with the given ID. |
| 53 |
func getShelf (_ parameters : GetShelfParameters) throws -> GetShelfResponses { |
| 54 |
let responses = GetShelfResponses() |
| 55 |
if let shelf : Shelf = shelves[parameters.shelf] { |
| 56 |
responses.ok = shelf |
| 57 |
} else { |
| 58 |
let err = Error() |
| 59 |
err.code = 404 |
| 60 |
err.message = "not found" |
| 61 |
responses.error = err |
| 62 |
} |
| 63 |
return responses |
| 64 |
} |
| 65 |
// Delete a single shelf with the given ID. |
| 66 |
func deleteShelf (_ parameters : DeleteShelfParameters) throws { |
| 67 |
shelves[parameters.shelf] = nil |
| 68 |
books[parameters.shelf] = nil |
| 69 |
} |
| 70 |
// Return all books in a shelf with the given ID. |
| 71 |
func listBooks (_ parameters : ListBooksParameters) throws -> ListBooksResponses { |
| 72 |
let responses = ListBooksResponses() |
| 73 |
let response = ListBooksResponse() |
| 74 |
var books : [Book] = [] |
| 75 |
if let shelfBooks = self.books[parameters.shelf] { |
| 76 |
for pair in shelfBooks { |
| 77 |
books.append(pair.value) |
| 78 |
} |
| 79 |
} |
| 80 |
response.books = books |
| 81 |
responses.ok = response |
| 82 |
return responses |
| 83 |
} |
| 84 |
// Create a new book on the shelf. |
| 85 |
func createBook (_ parameters : CreateBookParameters) throws -> CreateBookResponses { |
| 86 |
let responses = CreateBookResponses() |
| 87 |
lastBookIndex += 1 |
| 88 |
let shelf = parameters.shelf |
| 89 |
let book = parameters.book |
| 90 |
book.name = "shelves/\(shelf)/books/\(lastBookIndex)" |
| 91 |
if var shelfBooks = self.books[shelf] { |
| 92 |
shelfBooks[lastBookIndex] = book |
| 93 |
self.books[shelf] = shelfBooks |
| 94 |
} else { |
| 95 |
var shelfBooks : [Int64:Book] = [:] |
| 96 |
shelfBooks[lastBookIndex] = book |
| 97 |
self.books[shelf] = shelfBooks |
| 98 |
} |
| 99 |
responses.ok = book |
| 100 |
return responses |
| 101 |
} |
| 102 |
// Get a single book with a given ID from a shelf. |
| 103 |
func getBook (_ parameters : GetBookParameters) throws -> GetBookResponses { |
| 104 |
let responses = GetBookResponses() |
| 105 |
if let shelfBooks = self.books[parameters.shelf], |
| 106 |
let book = shelfBooks[parameters.book] { |
| 107 |
responses.ok = book |
| 108 |
} else { |
| 109 |
let err = Error() |
| 110 |
err.code = 404 |
| 111 |
err.message = "not found" |
| 112 |
responses.error = err |
| 113 |
} |
| 114 |
return responses |
| 115 |
} |
| 116 |
// Delete a single book with a given ID from a shelf. |
| 117 |
func deleteBook (_ parameters : DeleteBookParameters) throws { |
| 118 |
if var shelfBooks = self.books[parameters.shelf] { |
| 119 |
shelfBooks[parameters.book] = nil |
| 120 |
self.books[parameters.shelf] = shelfBooks |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
initialize(service:Server(), port:8080) |
| 126 |
|
| 127 |
run() |
| 128 |
|