Exemplo n.º 1
0
        public ListModule(ILibraryListRepository lists, IRepository documents, IImageRepository images, IEnvironmentPathProvider pathProvider)
            : base("/lists")
        {
            _documents = documents;
            
            Get["/"] = _ =>
            {
                var results = lists.GetAll().Select(doc => DtoMaps.Map(doc));
                return Response.AsJson(results).AsCacheable(DateTime.Now.AddDays(1)); // f***s up CORS... ?
            };

            Get["/{id}"] = args =>
            {
                LibrarylistDto dto = DtoMaps.Map(lists.Get(args.id), _documents);
                return Response.AsJson(dto).AsCacheable(DateTime.Now.AddDays(1));
            };

            Get["/{id}/thumbnail"] = args =>
            {
                LibraryList list = lists.Get(args.id);

                foreach (var docNo in list.DocumentNumbers.Keys)
                {
                    var img = images.GetDocumentImage(docNo);

                    if (String.IsNullOrEmpty(img)) continue;

                    return Response.AsFile(Path.Combine(pathProvider.GetImageCachePath(), img));
                }

                return TextResponse.NoBody; // todo: placeholder img
            };
        }
Exemplo n.º 2
0
        public DocumentModule(IRepository documents, IImageRepository images, IRatingRepository ratings, IReviewRepository reviews, IFavoritesRepository favorites, IEnvironmentPathProvider pathProvider)
            : base("/documents")
        {
            Get["/{id}/thumbnail"] = args =>
            {
                var doc = documents.GetDocument(args.id, true);
                string img = images.GetDocumentImage(args.id);

                if (String.IsNullOrEmpty(img))
                {
                    return ResolvePlaceHolderImageForDocumentType(pathProvider, doc);
                }

                return Response.AsFile(Path.Combine(pathProvider.GetImageCachePath(), img));
            };

            Get["/{id}"] = args =>
            {
                Document document = documents.GetDocument(args.id, false);
                return Response.AsJson(DtoMaps.Map(document, favorites, Context.GetUserInfo()));
            };
            
            Get["/{id}/rating"] = args =>
            {
                try
                {
                    DocumentRating rating = ratings.GetDocumentRating(args.id);

                    return Response.AsJson(new DocumentRatingDto
                    {
                        MaxScore = rating.MaxScore,
                        Score = rating.Score,
                        Source = rating.Source,
                        SourceUrl = rating.SourceUrl,
                        HasRating = true
                    }).AsCacheable(DateTime.Now.AddDays(1));
                }
                catch
                {
                    return new DocumentRatingDto {Success = true, HasRating = false};
                }
            };

            Get["/{id}/review"] = args =>
            {
                string review = reviews.GetDocumentReview(args.id);
                return Response.AsJson(new DocumentReviewDto{ Review = review, Url = "" }).AsCacheable(DateTime.Now.AddDays(1));
            };

            Get["/search"] = _ =>
            {
                string query = Request.Query.query.HasValue ? Request.Query.query : null;

                if (null == query) throw new InvalidOperationException("Ingenting å søke etter.");

                return Response.AsJson(documents.Search(query).Select(doc => DtoMaps.Map(doc, favorites, Context.GetUserInfo())).ToArray()).AsCacheable(DateTime.Now.AddHours(12));
            };
        }