コード例 #1
0
ファイル: MainView.cs プロジェクト: undyings/Basketball
        public static Func <object, JsonData[], HttpRequestMessage, HtmlResult <HElement> > HViewCreator(
            string kind, int?id)
        {
            return(delegate(object _state, JsonData[] jsons, HttpRequestMessage context)
            {
                SiteState state = _state as SiteState;
                if (state == null)
                {
                    state = new SiteState();
                }

                LinkInfo link = null;
                if (kind == "news" || kind == "user" || (kind == "novosti" && id != null) ||
                    kind == "article" || kind == "topic" || kind == "tags" || kind == "dialog" || kind == "confirmation")
                {
                    link = new LinkInfo("", kind, id);
                }
                else
                {
                    if (StringHlp.IsEmpty(kind))
                    {
                        link = new LinkInfo("", "", null);
                    }
                    else if (id == null)
                    {
                        link = store.Links.FindLink(kind, "");
                        if (link == null)
                        {
                            link = store.Links.FindLink("page", kind);
                        }
                    }
                }

                if (link == null)
                {
                    return new HtmlResult
                    {
                        RawResponse = new HttpResponseMessage()
                        {
                            StatusCode = HttpStatusCode.NotFound
                        }
                    };
                }

                if (jsons.Length > 0)
                {
                    state.AccessTime = DateTime.UtcNow;
                }

                foreach (JsonData json in jsons)
                {
                    try
                    {
                        if (state.IsRattling(json))
                        {
                            continue;
                        }

                        try
                        {
                            string command = json.JPath("data", "command")?.ToString();
                            if (command != null && StringHlp.IsEmpty(state.BlockHint))
                            {
                                if (command.StartsWith("save_"))
                                {
                                    object id1 = json.JPath("data", "id1");

                                    string hint = command.Substring(5);
                                    if (id != null)
                                    {
                                        hint = string.Format("{0}_{1}", hint, id1);
                                    }
                                    state.BlockHint = hint;

                                    Logger.AddMessage("Restore: {0}", hint);
                                }
                                else if (command == "tag_add" && kind == "")
                                {
                                    state.BlockHint = "news_add";

                                    Logger.AddMessage("Restore: news_add");
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Logger.WriteException(ex);
                        }

                        state.Operation.Reset();

                        HElement cachePage = Page(HttpContext.Current, state, link.Kind, link.Id);

                        hevent eventh = cachePage.FindEvent(json, true);
                        if (eventh != null)
                        {
                            eventh.Execute(json);
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.WriteException(ex);
                        state.Operation.Message = string.Format("Непредвиденная ошибка: {0}", ex.Message);
                    }
                }

                HElement page = Page(HttpContext.Current, state, link.Kind, link.Id);
                if (page == null)
                {
                    return new HtmlResult
                    {
                        RawResponse = new HttpResponseMessage()
                        {
                            StatusCode = HttpStatusCode.NotFound
                        }
                    };
                }
                return HtmlHlp.FirstHtmlResult(page, state, TimeSpan.FromHours(1));
            });
        }