public NotificationCollection SaveShift(Shift shift)
        {
            Guard.ArgumentNotNull(shift, "shift");

            var waiterIds = new List <int>();

            //Existing shift may already be assigned
            if (!shift.IsTransient())
            {
                var existingShift = LoadShift(shift.Id);
                if (existingShift.IsNotNull() && existingShift.Staff.IsNotNull())
                {
                    waiterIds.Add(existingShift.Staff.Id);
                }
            }
            //The shift being saved (new or not) may be assigned
            if (shift.Staff.IsNotNull())
            {
                waiterIds.Add(shift.Staff.Id);
            }

            var result = repository.Save(shift);

            if (!result.HasErrors())
            {
                publisher.Publish(MessageFactory.CreateAppMessage(shift.Id, MessageTopics.ShiftScheduleChanged, string.Empty));

                UpdateShiftAssignmentTracking(waiterIds, shift);
            }

            return(result);
        }
        private void UpdateShiftTrackingForDelete(Shift existingShift)
        {
            //Step 1: Set relevant shift assignment notifications to no longer valid
            var trackedAssignments  = repository.FindBy <TrackedShiftAssignment>(s => s.ShiftId == existingShift.Id);
            var assignmentsToUpdate = new List <TrackedShiftAssignment>();

            foreach (var shiftAssignment in trackedAssignments)
            {
                shiftAssignment.IsStillValid = false;
                assignmentsToUpdate.Add(shiftAssignment);
            }

            repository.Save(assignmentsToUpdate);

            //Step 2: Save a record for the deleted shift tracking and publish the message
            var deletedShift = new TrackedDeletedShift
            {
                DateDeleted    = DateTime.Now,
                RestaurantId   = existingShift.Restaurant.Id,
                ShiftEndTime   = existingShift.EndTime,
                ShiftStartTime = existingShift.StartTime,
                ShiftId        = existingShift.Id,
                StaffId        = existingShift.Staff.IsNotNull() ? existingShift.Staff.Id : (int?)null,
            };

            repository.Save(deletedShift);

            publisher.Publish(MessageFactory.CreateAppMessage(string.Empty, MessageTopics.ShiftDeleted, string.Empty));
        }
        public NotificationCollection DeleteShift(int shiftId)
        {
            var existingShift = LoadShift(shiftId);

            existingShift.IsCancelled = true;
            var result = repository.Save(existingShift);

            if (!result.HasErrors())
            {
                publisher.Publish(MessageFactory.CreateAppMessage(shiftId, MessageTopics.ShiftScheduleChanged, string.Empty));

                UpdateShiftTrackingForDelete(existingShift);
            }

            return(result);
        }
        static void Main(string[] args)
        {
            var publisher = new Publisher(".\\private$\\pubsub.test");

            Console.WriteLine("Publisher Started...");
            Console.WriteLine("Insert shiftid and press <enter> to add to queue. Type exit to quit.");

            var input = Console.ReadLine();

            while (input != "exit")
            {
                var applicationMessage = MessageFactory.CreateAppMessage(int.Parse(input), "ShiftUpdated");
                publisher.Publish(applicationMessage);

                input = Console.ReadLine();
            }
        }
        public NotificationCollection AssignShift(int staffId, int shiftId, string clientKey)
        {
            var notifications = NotificationCollection.CreateEmpty();

            var shift = repository.FindById <Shift>(shiftId);

            var waiterIds = new List <int>();

            waiterIds.Add(staffId);

            if (shift.IsNotNull() && shift.Staff.IsNotNull())
            {
                waiterIds.Add(shift.Staff.Id);
            }

            var toWaiter = repository.FindById <Staff>(staffId);

            if (shift.IsNull())
            {
                notifications.AddError("Shift does not exist.");
            }

            if (toWaiter.IsNull())
            {
                notifications.AddError("Waiter does not exist.");
            }

            if (!notifications.HasErrors())
            {
                shift.Staff       = toWaiter;
                shift.IsAvailable = false;

                notifications += repository.Save(shift);

                //TODO: Remove static factory
                publisher.Publish(MessageFactory.CreateAppMessage(shift.Id, MessageTopics.ShiftScheduleChanged, clientKey));
                UpdateShiftAssignmentTracking(waiterIds, shift, clientKey);
            }

            return(notifications);
        }
        private NotificationCollection UpdateShiftAssignmentTracking(IEnumerable <int> waiterIds, Shift shift, string clientKey = "")
        {
            Guard.ArgumentNotNull(shift, "shift");

            var result = NotificationCollection.CreateEmpty();

            var shiftAssignments = new List <TrackedShiftAssignment>();

            if (waiterIds.Any() && shift.Restaurant.IsNotNull())
            {
                foreach (var waiterId in waiterIds)
                {
                    result += SetExistingAssignmentsAsNoLongerValid(shift.Id, waiterId);

                    var shiftAssignment = new TrackedShiftAssignment()
                    {
                        ShiftId      = shift.Id,
                        RestaurantId = shift.Restaurant.Id,
                        StaffId      = waiterId,
                        DateAssigned = DateTime.Now,
                        IsStillValid = true
                    };

                    shiftAssignments.Add(shiftAssignment);
                }

                result += repository.Save(shiftAssignments);

                if (!result.HasErrors())
                {
                    foreach (var shiftAssignment in shiftAssignments)
                    {
                        publisher.Publish(MessageFactory.CreateAppMessage(shiftAssignment.StaffId, MessageTopics.ShiftAssigned, clientKey));
                    }
                }
            }

            return(result);
        }
        public NotificationCollection UpdateShiftAvailablity(int shiftId, bool makeAvailable, string clientKey)
        {
            var notifications = NotificationCollection.CreateEmpty();

            var shift = repository.FindById <Shift>(shiftId);

            if (shift.IsNull())
            {
                notifications.AddError("Shift does not exist.");
            }

            if (!notifications.HasErrors())
            {
                shift.IsAvailable = makeAvailable;

                notifications += repository.Save(shift);

                //TODO: Remove static factory
                publisher.Publish(MessageFactory.CreateAppMessage(shift.Id, MessageTopics.ShiftScheduleChanged, clientKey));
            }

            return(notifications);
        }
 public static void PublishApplicationMessage(string topic, object messageBody)
 {
     publisher.Publish(MessageFactory.CreateAppMessage(messageBody, topic));
 }