Ejemplo n.º 1
0
        /// <summary>
        /// Removes the given hotel from the list of bookmarks
        /// </summary>
        /// <param name="bookmark">The item from the bookmarked hotels list to remove</param>
        public void RemoveBookmark(HotelListCellModel bookmark)
        {
            using (var document = UserSession.FetchGuestBookmarkDocument()?.ToMutable()) {
                if (document == null)
                {
                    throw new InvalidOperationException("Bookmark document not found");
                }

                var currentIds = document.GetArray("hotels");
                if (currentIds == null)
                {
                    throw new InvalidOperationException("Bookmark document contains no hotels entry");
                }

                for (var i = 0; i < currentIds.Count; i++)
                {
                    if (bookmark.Source["id"] as string == currentIds[i].ToString())
                    {
                        currentIds.RemoveAt(i);
                        break;
                    }
                }

                document.Set("hotels", currentIds);
                UserSession.Database.Save(document);
                if (bookmark.Source["id"] is string idToRemove)
                {
                    var doc = UserSession.Database.GetDocument(idToRemove);
                    if (doc != null)
                    {
                        UserSession.Database.Delete(doc);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Either bookmarks or unbookmarks the given hotel
        /// </summary>
        /// <param name="hotel">The item from the hotel list to operate on</param>
        public void ToggleBookmark(HotelListCellModel hotel)
        {
            using (var document = UserSession.FetchGuestBookmarkDocument()?.ToMutable()) {
                var doc = document;
                if (document == null)
                {
                    if (hotel.IsBookmarked)
                    {
                        throw new InvalidOperationException("Guest bookmark document not found");
                    }

                    doc = new MutableDocument(new Dictionary <string, object> {
                        ["type"] = "bookmarkedhotels"
                    });
                }

                var bookmarked = doc.GetArray("hotels") ?? new MutableArrayObject();
                if (hotel.IsBookmarked)
                {
                    // Remove the bookmark
                    for (int i = 0; i < bookmarked.Count(); i++)
                    {
                        if (bookmarked.GetString(i) == (hotel.Source.ContainsKey("id") ? hotel.Source["id"] as String : null))
                        {
                            bookmarked.RemoveAt(i);
                            break;
                        }
                    }
                }
                else
                {
                    bookmarked.AddString(hotel.Source.ContainsKey("id") ? hotel.Source["id"] as String : null);
                }

                doc.SetArray("hotels", bookmarked);
                UserSession.Database.Save(doc);

                // Add the hotel details document
                if (hotel.Source["id"] is string id)
                {
                    using (var detailDoc = UserSession.Database.GetDocument(id)?.ToMutable() ?? new MutableDocument(id)) {
                        detailDoc.SetData(hotel.Source.ToDictionary(x => x.Key, x => x.Value));
                        UserSession.Database.Save(detailDoc);
                    }
                }
            }
        }