Files
Backend-Api/CompanyManagment.Application/CameraBugReportApplication.cs

228 lines
8.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using _0_Framework.Application;
using CompanyManagment.App.Contracts.CameraBugReport;
using Company.Domain.CameraBugReportAgg;
namespace CompanyManagment.Application
{
public class CameraBugReportApplication : ICameraBugReportApplication
{
private readonly ICameraBugReportRepository _repository;
public CameraBugReportApplication(ICameraBugReportRepository repository)
{
_repository = repository;
}
public OperationResult Create(CreateCameraBugReportCommand command)
{
var op = new OperationResult();
try
{
var bugReport = new CameraBugReport(
command.Title,
command.Description,
command.UserEmail,
command.DeviceModel,
command.OsVersion,
command.Manufacturer,
command.BuildNumber,
command.AppVersion,
command.ScreenResolution,
command.IsCharging,
command.BatteryLevel,
command.StorageInMB,
command.MemoryInMB,
command.NetworkType,
command.Platform,
command.DeviceId,
command.PackageName,
command.InstallTime,
command.LastUpdateTime,
command.Flavor,
command.Type,
command.Priority,
command.AccountId,
command.StackTrace
);
// اضافه کردن لاگ‌ها
if (command.Logs != null && command.Logs.Any())
{
foreach (var log in command.Logs)
{
bugReport.AddLog(log);
}
}
// اضافه کردن تصاویر
if (command.Screenshots != null && command.Screenshots.Any())
{
foreach (var screenshot in command.Screenshots)
{
bugReport.AddScreenshot(screenshot, $"screenshot_{Guid.NewGuid()}.jpg");
}
}
_repository.Create(bugReport);
_repository.SaveChanges();
return op.Succcedded();
}
catch (Exception ex)
{
return op.Failed($"خطا در ثبت گزارش خرابی: {ex.Message}");
}
}
public OperationResult Edit(EditCameraBugReportCommand command)
{
var op = new OperationResult();
try
{
var bugReport = _repository.Get(command.Id);
if (bugReport == null)
return op.Failed("گزارش خرابی یافت نشد.");
bugReport.ChangePriority(command.Priority);
bugReport.ChangeStatus(command.Status);
_repository.SaveChanges();
return op.Succcedded();
}
catch (Exception ex)
{
return op.Failed($"خطا در ویرایش گزارش خرابی: {ex.Message}");
}
}
public OperationResult Delete(long id)
{
var op = new OperationResult();
try
{
var bugReport = _repository.Get(id);
if (bugReport == null)
return op.Failed("گزارش خرابی یافت نشد.");
_repository.Remove(bugReport);
_repository.SaveChanges();
return op.Succcedded();
}
catch (Exception ex)
{
return op.Failed($"خطا در حذف گزارش خرابی: {ex.Message}");
}
}
public List<CameraBugReportViewModel> GetAll(CameraBugReportSearchModel searchModel)
{
var query = _repository.GetAllAsNoTracking();
// فیلتر کردن بر اساس Type
if (searchModel.Type.HasValue)
query = query.Where(x => x.Type == searchModel.Type.Value);
// فیلتر کردن بر اساس Priority
if (searchModel.Priority.HasValue)
query = query.Where(x => x.Priority == searchModel.Priority.Value);
// فیلتر کردن بر اساس Status
if (searchModel.Status.HasValue)
query = query.Where(x => x.Status == searchModel.Status.Value);
// فیلتر کردن بر اساس SearchTerm
if (!string.IsNullOrEmpty(searchModel.SearchTerm))
{
var searchLower = searchModel.SearchTerm.ToLower();
query = query.Where(x =>
x.Title.ToLower().Contains(searchLower) ||
x.Description.ToLower().Contains(searchLower) ||
x.UserEmail.ToLower().Contains(searchLower)
);
}
var bugReports = query
.OrderByDescending(x => x.CreationDate)
.Skip((searchModel.PageNumber) * searchModel.PageSize)
.Take(searchModel.PageSize)
.ToList();
return bugReports.Select(x => new CameraBugReportViewModel
{
Id = x.id,
Title = x.Title,
Description = x.Description,
UserEmail = x.UserEmail,
AccountId = x.AccountId,
DeviceModel = x.DeviceModel,
AppVersion = x.AppVersion,
Type = x.Type,
Priority = x.Priority,
Status = x.Status,
CreationDate = x.CreationDate,
UpdateDate = x.UpdateDate,
LogsCount = x.Logs?.Count ?? 0,
ScreenshotsCount = x.Screenshots?.Count ?? 0
}).ToList();
}
public CameraBugReportDetailViewModel GetDetails(long id)
{
var bugReport = _repository.Get(id);
if (bugReport == null)
return null;
return new CameraBugReportDetailViewModel
{
Id = bugReport.id,
Title = bugReport.Title,
Description = bugReport.Description,
UserEmail = bugReport.UserEmail,
AccountId = bugReport.AccountId,
DeviceModel = bugReport.DeviceModel,
OsVersion = bugReport.OsVersion,
Platform = bugReport.Platform,
Manufacturer = bugReport.Manufacturer,
DeviceId = bugReport.DeviceId,
ScreenResolution = bugReport.ScreenResolution,
MemoryInMB = bugReport.MemoryInMB,
StorageInMB = bugReport.StorageInMB,
BatteryLevel = bugReport.BatteryLevel,
IsCharging = bugReport.IsCharging,
NetworkType = bugReport.NetworkType,
AppVersion = bugReport.AppVersion,
BuildNumber = bugReport.BuildNumber,
PackageName = bugReport.PackageName,
InstallTime = bugReport.InstallTime,
LastUpdateTime = bugReport.LastUpdateTime,
Flavor = bugReport.Flavor,
Type = bugReport.Type,
Priority = bugReport.Priority,
Status = bugReport.Status,
StackTrace = bugReport.StackTrace,
CreationDate = bugReport.CreationDate,
UpdateDate = bugReport.UpdateDate,
Logs = bugReport.Logs?.Select(x => x.Message).ToList() ?? new List<string>(),
Screenshots = bugReport.Screenshots?.Select(x => new CameraBugReportScreenshotViewModel
{
Id = x.id,
FileName = x.FileName,
UploadDate = x.UploadDate,
Base64Data = x.Base64Data
}).ToList() ?? new List<CameraBugReportScreenshotViewModel>()
};
}
public bool IsExist(long id)
{
return _repository.IsExist(id);
}
}
}