Example #1
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));
            };
        }
Example #2
0
        public FavoritesModule(IFavoritesRepository favorites, IRepository documents)
            : base("/favorites")
        {
            this.RequiresAuthentication();

            Get["/"] = _ => favorites.GetFavorites(Context.GetUserInfo()).Select(MapToDto).ToArray();

            Delete["/{documentId}"] = args =>
            {
                var document = documents.GetDocument(args.documentId, true);

                favorites.RemoveFavorite(document, Context.GetUserInfo());
                
                return new RequestReplyDto { Success = true };
            };

            Put["/{documentId}"] = args =>
            {
                var document = documents.GetDocument(args.documentId, true);
                favorites.AddFavorite(document, Context.GetUserInfo());

                return new RequestReplyDto { Success = true };
            };
        }
Example #3
0
        public static ReservationDto Map(Reservation reservation, IRepository documents)
        {
            DateTime holdEnd;
            DateTime holdFrom;
            DateTime holdTo;

            DateTime.TryParse(reservation.HoldRequestEnd, out holdEnd);
            DateTime.TryParse(reservation.HoldRequestFrom, out holdFrom);
            DateTime.TryParse(reservation.HoldRequestTo, out holdTo);

            return new ReservationDto
            {
                Document = Map(documents.GetDocument(reservation.DocumentNumber, true)),
                Reserved = holdFrom,
                ReadyForPickup = !UnavailableStatuses.Contains(reservation.Status), // business logic should not be here! :(
                PickupLocation = reservation.PickupLocation,
                PickupDeadline = holdTo
            };
        }
Example #4
0
        public static LibrarylistDto Map(LibraryList list, IRepository documents = null)
        {
            if (null != documents)
            {
                return new LibrarylistDto
                {
                    Id = list.Id,
                    Name = list.Name,
                    Documents = list.Documents.Count > 0
                        ? list.Documents.Select(Map).ToList()
                        : list.DocumentNumbers.Keys.Select(dn => Map(documents.GetDocument(dn, true))).ToList()
                };
            }

            return new LibrarylistDto
            {
                Id = list.Id,
                Name = list.Name
            };
        }
Example #5
0
        public UserModule(IRepository documents) : base("/user")
        {
            this.RequiresAuthentication();

            Get["/notifications/count"] = args =>
            {
                //var since = Request.Query.since.HasValue ? Request.Query.since : DateTime.MinValue;
                
                // we have no idea when a notification was created. Or rather, all notifications are always created 'now'...

                return new NotificationCountDto {Count = Context.GetUserInfo().Notifications.Count()};
            };

            Get["/info"] = _ =>
            {
                var user = Context.GetAlephUserIdentity();
                UserInfo results = documents.GetUserInformation(user.UserName, user.Password);

                var reservationsList = results.Reservations ?? new List<Reservation>();
                var finesList = results.ActiveFines ?? new List<Fine>();
                var loansList = results.Loans ?? new List<Loan>();
                var notificationList = results.Notifications ?? new List<Notification>();
                var reservations = reservationsList.Select(r => DtoMaps.Map(r, documents));

                var fines = finesList.Select(f => new FineDto
                {
                    Date = ParseDateString(f.Date),
                    Description = f.Description,
                    Document = String.IsNullOrEmpty(f.DocumentNumber) ? null : DtoMaps.Map(documents.GetDocument(f.DocumentNumber, true)),
                    Status = f.Status,
                    Sum = f.Sum
                }).OrderByDescending(f => f.Date);

                var loans = loansList.Select(l => new LoanDto
                {
                    Document = String.IsNullOrEmpty(l.DocumentNumber) ? null : DtoMaps.Map(documents.GetDocument(l.DocumentNumber, true)),
                    AdminisrtativeDocumentNumber = l.AdminisrtativeDocumentNumber,
                    Barcode = l.Barcode,
                    DocumentNumber = l.DocumentNumber,
                    DocumentTitle = l.DocumentTitle,
                    DueDate = ParseDateString(l.DueDate),
                    ItemSequence = l.ItemSequence,
                    ItemStatus = l.ItemStatus,
                    LoanDate = ParseDateString(l.LoanDate),
                    LoanHour = l.LoanHour,
                    Material = l.Material,
                    OriginalDueDate = ParseDateString(l.OriginalDueDate),
                    SubLibrary = l.SubLibrary
                });

                var notifications = notificationList.Select(n => new NotificationDto
                {
                     Content = n.Content,
                     DocumentNumber = n.DocumentNumber,
                     DocumentTitle = n.DocumentTitle,
                     Title = n.Title,
                     Type = n.Type
                });

                var userDto = new UserInfoDto
                {
                    BorrowerId = results.BorrowerId,
                    Balance = results.Balance,
                    CashLimit = results.CashLimit,
                    CellPhoneNumber = results.CellPhoneNumber,
                    CityAddress = results.CityAddress,
                    DateOfBirth = results.DateOfBirth,
                    Email = results.Email,
                    HomeLibrary = results.HomeLibrary,
                    HomePhoneNumber = results.HomePhoneNumber,
                    Id = results.Id,
                    IsAuthorized = results.IsAuthorized,
                    Name = results.Name,
                    PrefixAddress = results.PrefixAddress,
                    StreetAddress = results.StreetAddress,
                    Zip = results.Zip,

                    Reservations = reservations,
                    Fines = fines,
                    Loans = loans,
                    Notifications = notifications
                };

                return userDto;

            };
        }