public async Task <IEnumerable <Book> > GetReadingList(string listName, string label = null)
        {
            var stopWatch = Stopwatch.StartNew();
            var list      = await _readingListService.GetReadingList(listName, label);

            stopWatch.Stop();
            _logger.Info($"{nameof(GetReadingList)}: Call for list {listName} took {stopWatch.ElapsedMilliseconds} ms");
            return(list);
        }
        public async Task <IEnumerable <Book> > GetReadingList(string listName, string label = null)
        {
            IEnumerable <Book> cachedBooks;

            // The KeyValuePair is used to make the cache key unique for the list name and label.
            if (_readingListCache.TryGetValue(new KeyValuePair <string, Label>(listName, BookMapper.MapBookTypeLabel(label)), out cachedBooks))
            {
                return(!string.IsNullOrEmpty(label) ? cachedBooks.Where(b => b.Label.ToString().ToLower().Equals(label.ToLower())) : cachedBooks);
            }

            _logger.Info($"Cache miss for {listName}, {label}");
            var booksFromService = await _readingListService.GetReadingList(listName, label);

            _readingListCache.TryAdd(new KeyValuePair <string, Label>(listName, BookMapper.MapBookTypeLabel(label)), booksFromService);
            return(booksFromService);
        }
示例#3
0
        public async Task <ReadingListCollection> GetAllReadingLists(string label)
        {
            await _readingListBoard.Lists.Refresh();

            IEnumerable <string> listNames = new List <string>(_readingListBoard.Lists.Select(l => l.Name).ToList());
            var readingBoard = new ReadingListCollection {
                ReadingLists = new Dictionary <string, IEnumerable <Book> >()
            };

            foreach (var listName in listNames)
            {
                var list = await _readingListService.GetReadingList(listName, label);

                readingBoard.ReadingLists.Add(listName, list);
            }

            return(readingBoard);
        }
示例#4
0
        public void AddRoutes(IEndpointRouteBuilder app)
        {
            app.MapGet($"{BaseUri}/readingList", async(HttpRequest req, HttpResponse res) =>
            {
                string requestLabel = req.Query["label"];
                var readingList     = await _readingListService.GetReadingList(ReadingListConstants.CurrentlyReading, requestLabel);
                await res.AsJson(readingList);
            });

            app.MapGet($"{BaseUri}/backlogList", async(HttpRequest req, HttpResponse res) =>
            {
                string requestLabel = req.Query["label"];
                var readingList     = await _readingListService.GetReadingList(ReadingListConstants.Backlog, requestLabel);
                await res.AsJson(readingList);
            });

            app.MapGet($"{BaseUri}/doneList", async(HttpRequest req, HttpResponse res) =>
            {
                string requestLabel = req.Query["label"];
                var readingList     = await _readingListService.GetReadingList(ReadingListConstants.DoneReading, requestLabel);
                await res.AsJson(readingList);
            });

            app.MapGet($"{BaseUri}/allLists", async(HttpRequest req, HttpResponse res) =>
            {
                string requestLabel = req.Query["label"];
                var allLists        = await _readingListCollectionService.GetAllReadingLists(requestLabel);
                await res.AsJson(allLists);
            });

            app.MapPost($"{BaseUri}/backlogList", async(HttpRequest req, HttpResponse res) =>
            {
                string author    = req.Query["author"];
                string bookTitle = req.Query["title"];
                string bookLabel = req.Query["label"];

                if (string.IsNullOrWhiteSpace(author) || string.IsNullOrWhiteSpace(bookTitle) || string.IsNullOrWhiteSpace(bookLabel))
                {
                    res.StatusCode = 422;
                    await res.AsJson("author, title and label is required.");
                    return;
                }

                var addBookToBacklog = await _readingListService.AddBookToBacklog(bookTitle, author, bookLabel);
                res.StatusCode       = addBookToBacklog ? 201 : 500;
                await res.AsJson(addBookToBacklog);
            }).RequireAuthorization();

            app.MapPut($"{BaseUri}/doneList", async(HttpRequest req, HttpResponse res) =>
            {
                string bookTitle = req.Query["title"];
                if (string.IsNullOrWhiteSpace(bookTitle))
                {
                    res.StatusCode = 422;
                    await res.AsJson("title is needed to move card from reading to done");
                    return;
                }

                var updateStatus = await _readingListService.UpdateDoneListFromReadingList(bookTitle);

                await res.AsJson(updateStatus);
            }).RequireAuthorization();
        }