public SearchAttachmentPage()
 {
     InitializeComponent();
     BindingContext = new SearchAttachmentViewModel();
 }
Exemplo n.º 2
0
        public async Task <IHttpActionResult> LoadAttachment(SearchAttachmentViewModel info)
        {
            #region Parameter validation

            if (info == null)
            {
                info = new SearchAttachmentViewModel();
                Validate(info);
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            #endregion

            // Get all attachment from database
            var attachments = UnitOfWork.RepositoryAttachment.Search();

            // Id have been defined
            if (info.Ids != null && info.Ids.Count > 0)
            {
                info.Ids = info.Ids.Where(x => x > 0).ToList();
                if (info.Ids != null && info.Ids.Count > 0)
                {
                    attachments = attachments.Where(x => info.Ids.Contains(x.Id));
                }
            }

            // Student id have been defined
            if (info.StudentIds != null && info.StudentIds.Count > 0)
            {
                info.StudentIds = info.StudentIds.Where(x => x > 0).ToList();
                if (info.StudentIds != null && info.StudentIds.Count > 0)
                {
                    attachments = attachments.Where(x => info.StudentIds.Contains(x.StudentId));
                }
            }

            var attachmentDetails = attachments.Select(attachment => new AttachmentDetailsModel
            {
                Id        = attachment.Id,
                StudentId = attachment.StudentId,
                Name      = attachment.Name,
                Type      = attachment.Type
            });

            // Do sorting.
            var sorting = info.Sort;
            if (sorting != null)
            {
                attachmentDetails = UnitOfWork.Sort(attachmentDetails, sorting.Direction, sorting.Property);
            }
            else
            {
                attachmentDetails = UnitOfWork.Sort(attachmentDetails, SortDirection.Ascending,
                                                    AttachmentPropertySort.Name);
            }

            // Paginate.
            var result = new SearchResult <IList <AttachmentDetailsModel> >
            {
                Total   = attachmentDetails.Count(),
                Records = await UnitOfWork.Paginate(attachmentDetails, info.Pagination).ToListAsync()
            };


            return(Ok(result));
        }