Hi,
Hello, if we want to rewrite this code again, how can we write it? I would be happy if you help.
Code
public string GetCountOfFilesForCharts(FileCountForChartRequest req)
{
if (req.Type == FileCountType.Opened)
{
if (req.Grouping == FileCountGroup.Daily)
{
var dates = Enumerable.Range(0, (DateTime.Now - req.StartDate).Days + 1)
.Select(offset => req.StartDate.AddDays(offset))
.ToList();
var dailyLogCounts = dates.Select(cartcurt => new
{
Date = cartcurt,
Count = _fileLogService.Entities.Count(fl => fl.CreateDate <= cartcurt.Date.AddDays(1) && fl.CreateDate >= cartcurt.Date && fl.StatusId == 1 && fl.FileId != 0)
});
string sonuc = "";
foreach (var logCount in dailyLogCounts)
{
sonuc += $"{logCount.Date.ToShortDateString()}: {logCount.Count} adet dosya açıldı.\n";
}
return sonuc;
}
else if (req.Grouping == FileCountGroup.Weekly)
{
var dates = Enumerable.Range(0, (DateTime.Now - req.StartDate).Days / 7 + 1)
.Select(offset => req.StartDate.AddDays(offset * 7))
.ToList();
var weeklyLogCounts = dates.Select(weekStart => new
{
WeekStart = weekStart,
Count = _fileLogService.Entities.Count(fl => fl.CreateDate < weekStart.Date.AddDays(7) && fl.CreateDate >= weekStart.Date && fl.StatusId == 1 && fl.FileId != 0)
});
string sonuc = "";
foreach (var logCount in weeklyLogCounts)
{
sonuc += $"Hafta başı {logCount.WeekStart.ToShortDateString()}: {logCount.Count} adet dosya açıldı.\n";
}
return sonuc;
}
else if (req.Grouping == FileCountGroup.Monthly)
{
var dates = Enumerable.Range(0, (DateTime.Now.Year - req.StartDate.Year) * 12 + DateTime.Now.Month - req.StartDate.Month + 1)
.Select(offset => new DateTime(req.StartDate.Year, req.StartDate.Month, 1).AddMonths(offset))
.ToList();
var monthlyLogCounts = dates.Select(monthStart => new
{
MonthStart = monthStart,
Count = _fileLogService.Entities.Count(fl =>
fl.CreateDate >= monthStart &&
fl.CreateDate < monthStart.AddMonths(1) &&
fl.StatusId == 1 &&
fl.FileId != 0)
});
string sonuc = "";
foreach (var logCount in monthlyLogCounts)
{
sonuc += $"Ay başı {logCount.MonthStart.ToShortDateString()}: {logCount.Count} adet dosya açıldı.\n";
}
return sonuc;
}
else if (req.Grouping == FileCountGroup.Yearly)
{
var dates = Enumerable.Range(0, DateTime.Now.Year - req.StartDate.Year + 1)
.Select(offset => new DateTime(req.StartDate.Year, 1, 1).AddYears(offset))
.ToList();
var yearlyLogCounts = dates.Select(yearStart => new
{
YearStart = yearStart,
Count = _fileLogService.Entities.Count(fl =>
fl.CreateDate >= yearStart &&
fl.CreateDate < yearStart.AddYears(1) &&
fl.StatusId == 1 &&
fl.FileId != 0)
});
string sonuc = "";
foreach (var logCount in yearlyLogCounts)
{
sonuc += $"Yıl başı {logCount.YearStart.ToShortDateString()}: {logCount.Count} adet dosya açıldı.\n";
}
return sonuc;
}
return "";
}
if (req.Type == FileCountType.Closed)
{
if (req.Grouping == FileCountGroup.Daily)
{
var dates = Enumerable.Range(0, (DateTime.Now - req.StartDate).Days + 1)
.Select(offset => req.StartDate.AddDays(offset))
.ToList();
var dailyLogCounts = dates.Select(cartcurt => new
{
Date = cartcurt,
Count = _fileLogService.Entities.Count(fl => fl.CreateDate <= cartcurt.Date.AddDays(1) && fl.CreateDate >= cartcurt.Date && fl.StatusId == 10 && fl.FileId != 0)
});
string sonuc = "";
foreach (var logCount in dailyLogCounts)
{
sonuc += $"{logCount.Date.ToShortDateString()}: {logCount.Count} adet dosya açıldı.\n";
}
return sonuc;
}
else if (req.Grouping == FileCountGroup.Weekly)
{
var dates = Enumerable.Range(0, (DateTime.Now - req.StartDate).Days / 7 + 1)
.Select(offset => req.StartDate.AddDays(offset * 7))
.ToList();
var weeklyLogCounts = dates.Select(weekStart => new
{
WeekStart = weekStart,
Count = _fileLogService.Entities.Count(fl => fl.CreateDate < weekStart.Date.AddDays(7) && fl.CreateDate >= weekStart.Date && fl.StatusId == 10 && fl.FileId != 0)
});
string sonuc = "";
foreach (var logCount in weeklyLogCounts)
{
sonuc += $"Hafta başı {logCount.WeekStart.ToShortDateString()}: {logCount.Count} adet dosya açıldı.\n";
}
return sonuc;
}
else if (req.Grouping == FileCountGroup.Monthly)
{
var dates = Enumerable.Range(0, (DateTime.Now.Year - req.StartDate.Year) * 12 + DateTime.Now.Month - req.StartDate.Month + 1)
.Select(offset => new DateTime(req.StartDate.Year, req.StartDate.Month, 1).AddMonths(offset))
.ToList();
var monthlyLogCounts = dates.Select(monthStart => new
{
MonthStart = monthStart,
Count = _fileLogService.Entities.Count(fl =>
fl.CreateDate >= monthStart &&
fl.CreateDate < monthStart.AddMonths(1) &&
fl.StatusId == 10 &&
fl.FileId != 0)
});
string sonuc = "";
foreach (var logCount in monthlyLogCounts)
{
sonuc += $"Ay başı {logCount.MonthStart.ToShortDateString()}: {logCount.Count} adet dosya açıldı.\n";
}
return sonuc;
}
else if (req.Grouping == FileCountGroup.Yearly)
{
var dates = Enumerable.Range(0, DateTime.Now.Year - req.StartDate.Year + 1)
.Select(offset => new DateTime(req.StartDate.Year, 1, 1).AddYears(offset))
.ToList();
var yearlyLogCounts = dates.Select(yearStart => new
{
YearStart = yearStart,
Count = _fileLogService.Entities.Count(fl =>
fl.CreateDate >= yearStart &&
fl.CreateDate < yearStart.AddYears(1) &&
fl.StatusId == 10 &&
fl.FileId != 0)
});
string sonuc = "";
foreach (var logCount in yearlyLogCounts)
{
sonuc += $"Yıl başı {logCount.YearStart.ToShortDateString()}: {logCount.Count} adet dosya açıldı.\n";
}
return sonuc;
}
}
return "";
}