Example #1
0
func putVolumeSingle(call *Call, isbn isbn13.ISBN13) (info *data.BookMetaData, err error) {
	call.Context.Debugf("Updating %d", isbn)
	decode := json.NewDecoder(call.Request.Body)
	info = new(data.BookMetaData)
	if err = decode.Decode(info); err == nil {
		call.Context.Debugf("Raw decoded entity: %v", info)
		info.ISBN = isbn.String()
		normalize(call, info)
		call.Context.Debugf("Normalized decoded entity: %v", info)

		persistence.UpdateBookshelf(call.Context, func(t *tx.Transaction, shelf *data.Bookshelf) error {

			if ptr := shelf.LookupInfo(isbn); ptr != nil {
				*ptr = *info
				t.Put = []tx.KeyDeriver{ptr}
			} else {
				info.Parent = shelf
				shelf.Books = append(shelf.Books, *info)
				t.Put = []tx.KeyDeriver{info}
			}

			return nil
		})
	}
	return info, err
}
Example #2
0
func deleteVolumeSingle(call *Call, isbn isbn13.ISBN13) (info *data.BookMetaData, err error) {
	call.Context.Infof("Deleting ISBN %d", isbn)
	err = persistence.UpdateBookshelf(call.Context, func(t *tx.Transaction, shelf *data.Bookshelf) error {
		for i := range shelf.Books {
			if ptr := &shelf.Books[i]; ptr.ISBN == isbn.String() {
				shelf.Books = append(shelf.Books[:i], shelf.Books[i+1:]...)
				t.Delete = []tx.KeyDeriver{ptr}
				return nil
			}

		}
		call.StatusCode = http.StatusNotFound
		return errors.New("ISBN not found")
	})

	return
}