Exemplo n.º 1
0
        public PageResult <object> FindByAffectedUser(ODataQueryOptions <UserHistoryDto> options, string searchTerm, int userK)
        {
            var userHistories = new List <UserHistoryDto>();
            var systemLogs    = systemLogService.FindByAffectedUser(searchTerm, userK);

            foreach (var systemLog in systemLogs)
            {
                var user = systemLog.UserFK != null?userService.Read(systemLog.UserFK.Value) : null;

                foreach (var item in systemLog.SystemLogItems)
                {
                    if (item.FieldName != "ModifiedAtUtc" && item.FieldName != "ModifiedByUserFK" && item.FieldName != "CreatedByUserFK" && item.FieldName != "CreatedAtUtc")
                    {
                        var userHistory = new UserHistoryDto();
                        userHistory.DateTime        = systemLog.TimestampUtc.ToLocalTime();
                        userHistory.FieldName       = item.FieldName;
                        userHistory.PreChangeValue  = item.PreChangeValue;
                        userHistory.PostChangeValue = item.PostChangeValue;
                        userHistory.ChangedBy       = user != null ? (user.FirstName + " " + user.LastName) : "N/A";
                        userHistories.Add(userHistory);
                    }
                }
            }

            return(ApplyPaging(options, userHistories.AsQueryable()));
        }
Exemplo n.º 2
0
        public HttpResponseMessage ExportUserHistoryToCsv(int userK)
        {
            var userHistories = new List <UserHistoryDto>();
            var systemLogs    = systemLogService.FindByAffectedUser(null, userK);

            foreach (var systemLog in systemLogs)
            {
                var user = systemLog.UserFK != null?userService.Read(systemLog.UserFK.Value) : null;

                foreach (var item in systemLog.SystemLogItems)
                {
                    if (item.FieldName != "ModifiedAtUtc" && item.FieldName != "ModifiedByUserFK" && item.FieldName != "CreatedByUserFK" && item.FieldName != "CreatedAtUtc")
                    {
                        var userHistory = new UserHistoryDto();
                        userHistory.DateTime        = systemLog.TimestampUtc.ToLocalTime();
                        userHistory.FieldName       = item.FieldName;
                        userHistory.PreChangeValue  = item.PreChangeValue;
                        userHistory.PostChangeValue = item.PostChangeValue;
                        userHistory.ChangedBy       = user != null ? (user.FirstName + " " + user.LastName) : "N/A";
                        userHistories.Add(userHistory);
                    }
                }
            }

            var csv = new CsvExport();

            foreach (var userHistory in userHistories)
            {
                csv.AddRow();
                csv["Date Time"]         = userHistory.DateTime.ToString("dd-MMM-yyyy hh:mm tt");
                csv["Field Name"]        = userHistory.FieldName;
                csv["Pre Change Value"]  = userHistory.PreChangeValue;
                csv["Post Change Value"] = userHistory.PostChangeValue;
                csv["Changed By"]        = userHistory.ChangedBy;
            }

            var result = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new ByteArrayContent(csv.ExportToBytes())
            };

            result.Content.Headers.ContentType        = new MediaTypeHeaderValue("application/octet-stream");
            result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = "User_" + userK + "_Histories_" + DateTime.UtcNow.ToLocalTime().ToString("MMddyyyy_hhmm") + ".csv"
            };
            return(result);
        }