public ReservationsModule(IRepository repository)
            : base("/reservations")
        {
            this.RequiresAuthentication();

            Get["/"] = _ =>
            {
                var user = Context.GetAlephUserIdentity();

                var reservations = repository.GetUserInformation(user.UserName, user.Password).Reservations;
                if (reservations == null)
                {
                    return new ReservationDto[0];
                }
                return reservations.Select(r => DtoMaps.Map(r, repository)).ToArray();
            };

            Delete["/{documentId}"] = args =>
            {
                var user = Context.GetAlephUserIdentity();

                var userReservations = repository.GetUserInformation(user.UserName, user.Password).Reservations;
                var resToRemove = userReservations.FirstOrDefault(r => r.DocumentNumber == args.documentId);

                if (resToRemove != null)
                {
                    var response = repository.CancelReservation(resToRemove.ItemDocumentNumber, resToRemove.ItemSeq, resToRemove.CancellationSequence);
                    if (response.Success) Context.RequireUserInfoRefresh();

                    return response;
                }

                return "Kunne ikke finne dokument i liste over reservasjoner";
            };

            Put["/{documentId}"] = args =>
            {
                string branch = Request.Form.branch.HasValue ? Request.Form.branch : Request.Query.branch;

                var response = repository.RequestReservation(args.documentId, Context.GetUserInfo().Id, branch);
                if (response.Success) Context.RequireUserInfoRefresh();

                return response;
            };
        }
Example #2
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;

            };
        }