Exemplo n.º 1
0
        public async Task <LogSearchResult> GetModelListAsync(string keyword, DateTime?startTime, DateTime?endTime, int pageIndex, int pageSize)
        {
            using (MyDbContext dbc = new MyDbContext())
            {
                LogSearchResult result = new LogSearchResult();
                var             logs   = dbc.GetAll <LogEntity>().AsNoTracking();
                if (!string.IsNullOrEmpty(keyword))
                {
                    logs = logs.Where(a => a.LogName.Contains(keyword) || a.Description.Contains(keyword));
                }

                if (startTime != null)
                {
                    logs = logs.Where(a => a.CreateTime >= startTime);
                }
                if (endTime != null)
                {
                    logs = logs.Where(a => SqlFunctions.DateDiff("day", endTime, a.CreateTime) <= 0);
                }
                result.PageCount = (int)Math.Ceiling((await logs.LongCountAsync()) * 1.0f / pageSize);
                var logsResult = await logs.OrderByDescending(a => a.CreateTime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();

                result.log = logsResult.Select(a => ToDTO(a)).ToArray();
                return(result);
            }
        }
        public LogSearchResult Logs([FromBody] SearchParameters searchData)
        {
            LogSearchResult objLogSearchResult = new LogSearchResult();

            objLogSearchResult.LogList = new List <DTOLog>();

            // Must be a Super Administrator to call this Method
            if (!UtilitySecurity.IsSuperUser(this.User.Identity.Name, GetConnectionString()))
            {
                objLogSearchResult.errorMessage = "Must be a Super Administrator to call this Method";
                return(objLogSearchResult);
            }

            var optionsBuilder = new DbContextOptionsBuilder <ADefHelpDeskContext>();

            optionsBuilder.UseSqlServer(GetConnectionString());

            using (var context = new ADefHelpDeskContext(optionsBuilder.Options))
            {
                int intTaskId = Convert.ToInt32(searchData.searchString);

                var QueryResult = (from Log in context.AdefHelpDeskLog
                                   where Log.TaskId == intTaskId
                                   select Log).OrderByDescending(l => l.LogId)
                                  .Skip(searchData.rowsPerPage * (searchData.pageNumber - 1))
                                  .Take(searchData.rowsPerPage).ToList();

                List <DTOLog> colDTOLog = new List <DTOLog>();

                foreach (var item in QueryResult)
                {
                    // Get user
                    DTOUser objDTOUSer = UtilitySecurity.UserFromUserId(item.UserId, GetConnectionString());

                    // Create Log
                    DTOLog objDTOLog = new DTOLog();

                    objDTOLog.LogID          = item.LogId;
                    objDTOLog.TaskID         = item.TaskId;
                    objDTOLog.LogDescription = item.LogDescription;
                    objDTOLog.UserName       = (objDTOUSer != null) ? objDTOUSer.userName : "******";
                    objDTOLog.DateCreated    = item.DateCreated.ToShortDateString() + ' ' + item.DateCreated.ToShortTimeString();

                    colDTOLog.Add(objDTOLog);
                }

                objLogSearchResult.LogList      = colDTOLog;
                objLogSearchResult.totalRows    = context.AdefHelpDeskLog.Where(x => x.TaskId == intTaskId).Count();
                objLogSearchResult.errorMessage = string.Empty;
            }

            return(objLogSearchResult);
        }