Beispiel #1
0
        public ActionResult List()
        {
            try
            {
                w2uiGridJsonPostModel postModel = null;
                try
                {
                    postModel = RequestConverter.FromJsonRequest <w2uiGridJsonPostModel>(Request.Body);
                }
                catch (Exception ex)
                {
                    return(Json(w2uiBaseModel.Error(ex)));
                }

                var search      = postModel.search != null ? postModel.search[0].value : "";
                var searchField = postModel.search != null ? postModel.search[0].field : "Title";
                var searchType  = postModel.search != null ? postModel.search[0].type : "text";

                var sortdir   = postModel.sort != null ? postModel.sort[0].direction : "asc";
                var sortfield = postModel.sort != null ? postModel.sort[0].field : "Title";

                SearchOptions options =
                    new SearchOptions(sortfield, searchField, searchType)
                    .Limit(postModel.offset, postModel.limit)
                    .Sort(sortdir)
                    .Search(search);

                w2uiGridModel <GroupAPI> model = new w2uiGridModel <GroupAPI>();


                var allGroups = _db
                                .Groups
                                .ApplyFilterByType(options);

                model.records = allGroups
                                .ApplyPaging(options)
                                .Select(el => new GroupAPI
                {
                    Id        = el.Id,
                    Title     = el.Title,
                    UserCount = _db.UserInGroups.Where(g => g.GroupId == el.Id).Count()
                })
                                .ToList();

                model.total = allGroups.Count();

                return(Json(model));
            }
            catch (Exception ex)
            {
                return(Json(w2uiBaseModel.Error(ex)));
            }
        }
        public ActionResult List()
        {
            try
            {
                w2uiGridJsonPostModel postModel = null;
                try
                {
                    postModel = RequestConverter.FromJsonRequest <w2uiGridJsonPostModel>(Request.Body);
                }
                catch (Exception ex)
                {
                    return(Json(w2uiBaseModel.Error(ex)));
                }

                var search      = "";
                var searchField = "Id";
                var searchType  = "int";
                var sortdir     = "asc";
                var sortfield   = "LastName";

                var specialFilters = new List <string>()
                {
                    "gender", "group"
                };
                var genderFilter = false;
                var groupFilter  = false;
                var specVal      = "";

                if (postModel.search != null)
                {
                    var s = postModel.search[0];

                    if (!specialFilters.Contains(s.field))
                    {
                        search      = s.value;
                        searchField = s.field;
                        searchType  = s.type;
                    }
                    else
                    {
                        specVal      = s.value;
                        genderFilter = s.field == "gender";
                        groupFilter  = s.field == "group";
                    }
                }

                if (postModel.sort != null)
                {
                    sortdir   = postModel.sort[0].direction;
                    sortfield = postModel.sort[0].field;
                }

                SearchOptions options = null;

                if (searchField != "groups")
                {
                    options =
                        new SearchOptions(sortfield, searchField, searchType)
                        .Limit(postModel.offset, postModel.limit)
                        .Sort(sortdir)
                        .Search(search);
                }
                else
                {
                    options =
                        new SearchOptions(sortfield, searchField, searchType)
                        .Limit(postModel.offset, postModel.limit)
                        .Sort(sortdir);
                }

                w2uiGridModel <StudentAPI> model = new w2uiGridModel <StudentAPI>();


                var allStudents = _db.Students
                                  .Where(el => genderFilter == false || el.Gender.Title == specVal)
                                  .Where(el => groupFilter == false || el.UserInGroups.Select(gr => gr.Group).Where(gr => gr.Title == specVal).Any())
                                  .ApplyFilterByType(options);

                model.records = allStudents
                                .ApplyPaging(options)
                                .Select(el => new StudentAPI
                {
                    CallSign  = el.CallSign,
                    FirstName = el.FirstName,
                    Gender    = el.Gender,
                    GenderId  = el.GenderId,
                    Groups    = _db.UserInGroups
                                .Where(g => g.UserId == el.Id)
                                .Select(g => new IdText {
                        id = g.Id, text = g.Group.Title
                    })
                                .ToList(),
                    Id           = el.Id,
                    LastName     = el.LastName,
                    SecondName   = el.SecondName,
                    UserInGroups = el.UserInGroups
                })
                                .ToList();

                model.total = allStudents.Count();

                return(Json(model));
            }
            catch (Exception ex)
            {
                return(Json(w2uiBaseModel.Error(ex)));
            }
        }