Esempio n. 1
0
 public async Task<IHttpActionResult> CreateOrUpdateBook(JObject rawBook)
 {
     try
     {
         var book = new DatabaseJObject(rawBook);
         if (book.ObjectTypeIsNot(DatabaseObjectType.BOOK, DatabaseObjectType.NONE))
         {
             return Ok(new Result(DatabaseObjectType.BOOK, book.ObjectType));
         }
         else
         {
             book.ObjectType = DatabaseObjectType.BOOK;
             book = await this.databaseClient.CreateOrUpdateBookAsync(book);
             if (book.ObjectId.HasValue)
             {
                 await this.searchClient.CreateOrUpdateBookIndexAsync(book);
                 return Ok(new Result<ObjectIdResult>(new ObjectIdResult { ObjectId = book.ObjectId.Value }));
             }
             else
             {
                 return Ok(new Result("No object ID could be found or created for the book."));
             }
         }
     }
     catch (Exception ex)
     {
         return Ok(new Result(ex.ToString()));
     }
 }
Esempio n. 2
0
        public async Task<DatabaseJObject> CreateOrUpdateBookAsync(DatabaseJObject book)
        {
            if (!book.ObjectId.HasValue)
            {
                book.ObjectId = await this.GetNextObjectId();
            }

            var document = Document.FromJson(book.JObject.ToString(Newtonsoft.Json.Formatting.None));
            await DatabaseTable.PutItemAsync(document);
            return book;
        }
 public async Task<bool> CreateOrUpdateBookIndexAsync(DatabaseJObject book)
 {
     try
     {
         var indexObject = await this.PrimaryIndex.GetObjectAsync(book.ObjectId.ToString(), new string[] { DatabaseFields.OBJECT_ID });
         if (indexObject != null)
         {
             await PrimaryIndex.SaveObjectAsync(book.JObject);
         }
     }
     catch (AlgoliaException ex)
     {
         if (ex.Message.Equals("ObjectID does not exist"))
         {
             await PrimaryIndex.AddObjectAsync(book.JObject, objectId: book.ObjectId.ToString());
         }
         else
         {
             throw;
         }
     }
     return true;
 }