public async Task <GetBeritasResponse> Handle(GetBeritasRequest request, CancellationToken cancellationToken) { try { List <Berita> records; List <File> files; int totalRecords; records = await _context.Beritas .AsNoTracking() .Where(x => x.Title.Contains(request.FilterByName ?? "")) .Skip((request.PageNumber - 1) * request.PageSize) .Take(request.PageSize) .ToListAsync(cancellationToken); totalRecords = _context.Beritas.AsNoTracking().Count(x => x.Title.Contains(request.FilterByName ?? "")); files = await _context.Files.Where(x => x.EntityType == FileEntityType.Berita).ToListAsync(cancellationToken); List <BeritaDTO> listOfDTO = new List <BeritaDTO>(); int no = 1; foreach (var record in records) { var file = files.Where(x => x.EntityID == record.BeritaID).FirstOrDefault(); string fileName = file != null ? $"images/{file.Name}" : $"images/image_not_available.png"; BeritaDTO dto = new BeritaDTO { No = no++, BeritaID = record.BeritaID, UrlOriginPhoto = fileName, Title = string.IsNullOrEmpty(record.Title) ? "Unknown" : record.Title, }; if (dto != null) { listOfDTO.Add(dto); } } return(new GetBeritasResponse { Data = listOfDTO, Pagination = new PaginationResponse() { TotalCount = totalRecords, PageSize = request.PageSize, CurrentPage = request.PageNumber } }); } catch (System.Exception) { throw; } }
public async Task <GetAllBeritaResponse> Handle(GetAllBeritaRequest request, CancellationToken cancellationToken) { try { var records = await _context.Beritas .AsNoTracking() .ToListAsync(cancellationToken); List <BeritaDTO> listOfDTO = new List <BeritaDTO>(); if (records.Count > 0) { var users = await _context.Users.ToListAsync(cancellationToken); int no = 1; foreach (var record in records) { string fullName = ""; if (!string.IsNullOrEmpty(record.CreatedBy)) { fullName = users.FirstOrDefault(x => x.UserID == record.CreatedBy)?.Fullname; } var data = new BeritaDTO { No = no++, BeritaID = record.BeritaID, Title = string.IsNullOrEmpty(record.Title) ? "Unknown" : record.Title, Description = record.Description, Created = record.Created, UserName = fullName }; if (data != null) { listOfDTO.Add(data); } } } return(new GetAllBeritaResponse { Data = listOfDTO }); } catch (Exception ex) { throw ex; } }