Esempio n. 1
0
        public MessagesNancyModule(IMessageListViewModelRetriever messageListViewModelRetriever
                                   , IHandlerFactory handlerFactory)
            : base("/messages")
        {
            Get("/{storeName}/{pageNumber?1}", parameters =>
            {
                var logger = LogProvider.GetLogger("MessagesNancyModule");
                logger.Log(LogLevel.Debug, () => "GET on messages");

                string storeName = parameters.storeName;
                int pageNumber   = parameters.pageNumber;
                ViewModelRetrieverResult <MessageListModel, MessageListModelError> messageListModelResult = messageListViewModelRetriever.Get(storeName, pageNumber);

                if (!messageListModelResult.IsError)
                {
                    return(Response.AsJson(messageListModelResult.Result));
                }
                switch (messageListModelResult.Error)
                {
                case (MessageListModelError.StoreNotFound):
                    return(Response.AsJson(new MessageViewerError(
                                               string.Format("Unknown store {0}", storeName)), HttpStatusCode.NotFound));

                case (MessageListModelError.StoreMessageViewerNotImplemented):
                    return(Response.AsJson(new MessageViewerError(
                                               string.Format("Found store {0} does not implement IMessageStoreViewer", storeName)), HttpStatusCode.NotFound));

                case (MessageListModelError.StoreMessageViewerGetException):
                    return(Response.AsJson(new MessageViewerError(
                                               string.Format("Unable to retrieve messages for store {0}", storeName)), HttpStatusCode.InternalServerError));

                case (MessageListModelError.GetActivationStateFromConfigError):
                    return(Response.AsJson(new MessageViewerError(
                                               string.Format("Misconfigured Message Viewer, unable to retrieve messages for store {0}", storeName)), HttpStatusCode.InternalServerError));

                default:
                    throw new Exception("Code can't reach here");
                }
            });

            Post("/{storeName}/repost/{msgList}", parameters =>
            {
                try
                {
                    var handler        = handlerFactory.GetHandler <RepostCommand>();
                    string ids         = parameters.msgList;
                    var repostModelIds = ids.Split(',');
                    var repostCommand  = new RepostCommand {
                        StoreName = parameters.storeName, MessageIds = repostModelIds.ToList()
                    };
                    handler.Handle(repostCommand);

                    return(Response.AsJson <int>(0, HttpStatusCode.OK));
                }
                catch (Exception e)
                {
                    return(e);
                }
            });
        }
Esempio n. 2
0
        public IHandler GetPipeline()
        {
            var pipeLineHandlres = new List <IHandler>();
            HandlersCollection handlersCollection = _configurationProvider.GetConfig();

            foreach (PipelineConfigItem handler in handlersCollection.Handlers)
            {
                pipeLineHandlres.Add(_handlerFactory.GetHandler(handler));
            }

            if (pipeLineHandlres.Count > 0)
            {
                IHandler pipeline = null;
                pipeLineHandlres.ForEach(h =>
                {
                    if (pipeline != null)
                    {
                        pipeline.Next = h;
                    }
                    pipeline = h;
                });

                return(pipeLineHandlres[0]);
            }
            return(null);
        }
Esempio n. 3
0
 /// <summary>
 /// Handles an specific request based on type
 /// </summary>
 /// <typeparam name="TRequest">The type of the request</typeparam>
 /// <param name="request">The request to be handled</param>
 /// <returns>Returns an instance of IHttpActionResult</returns>
 protected IHttpActionResult Handle <TRequest>(TRequest request)
     where TRequest : ApiRequest
 {
     return(_handlerFactory
            .GetHandler <TRequest>()
            .Handle(request));
 }
Esempio n. 4
0
 public void RouteReply(string replyXml)
 {
     using (var reader = new StringReader(replyXml))
     {
         var reply     = (Reply)_serializer.Deserialize(reader);
         var replyType = _replyTypeMapper.GetReplyType(reply.Name);
         var handler   = _handlerFactory.GetHandler(replyType);
         handler.HandleReply(reply);
     }
 }
Esempio n. 5
0
 /// <summary>
 /// Runs the code of the actual handler after all the common operations that are
 /// performed for each message regardless of its type
 /// </summary>
 /// <param name="message">The message to be handled</param>
 public async Task HandleAsync <TMessage>(TMessage message)
 {
     try
     {
         // Handles the message
         await _handlerFactory.GetHandler <TMessage>().HandleAsync(message);
     }
     catch (AggregateException e)
     {
         HandleException(e.Flatten());
     }
     catch (Exception e)
     {
         HandleException(e);
     }
 }
        public MessagesNancyModule(IMessageListViewModelRetriever messageListViewModelRetriever
                                    , IHandlerFactory handlerFactory)
            : base("/messages")
        {
            Get["/{storeName}/{pageNumber?1}"] = parameters =>
            {
                var logger = LogProvider.GetLogger("MessagesNancyModule");
                logger.Log(LogLevel.Debug, () => "GET on messages");

                string storeName = parameters.storeName;
                int pageNumber = parameters.pageNumber;
                ViewModelRetrieverResult<MessageListModel, MessageListModelError> messageListModelResult = messageListViewModelRetriever.Get(storeName, pageNumber);

                if (!messageListModelResult.IsError)
                {
                    return Response.AsJson(messageListModelResult.Result);                    
                }
                switch (messageListModelResult.Error)
                {
                    case(MessageListModelError.StoreNotFound):
                        return Response.AsJson(new MessageViewerError(
                            string.Format("Unknown store {0}", storeName)), HttpStatusCode.NotFound);
                    
                    case (MessageListModelError.StoreMessageViewerNotImplemented):
                        return Response.AsJson(new MessageViewerError(
                            string.Format("Found store {0} does not implement IMessageStoreViewer", storeName)), HttpStatusCode.NotFound);

                    case (MessageListModelError.StoreMessageViewerGetException):
                        return Response.AsJson(new MessageViewerError(
                            string.Format("Unable to retrieve messages for store {0}", storeName)), HttpStatusCode.InternalServerError);

                    case (MessageListModelError.GetActivationStateFromConfigError):
                        return Response.AsJson(new MessageViewerError(
                            string.Format("Misconfigured Message Viewer, unable to retrieve messages for store {0}", storeName)), HttpStatusCode.InternalServerError);
                    default:
                        throw new SystemException("Code can't reach here");
                }
            };
            Post["/{storeName}/repost/{msgList}"] = parameters =>
            {
                var handler = handlerFactory.GetHandler<RepostCommand>();
                string ids = parameters.msgList;
                var repostModelIds = ids.Split(',');
                var repostCommand = new RepostCommand { StoreName = parameters.storeName, MessageIds = repostModelIds.ToList() };
                handler.Handle(repostCommand);

                return Response.AsJson<int>(0, HttpStatusCode.OK);
            };
        }