Ejemplo n.º 1
0
 private NotesViewModel CreateNotesViewModel(string searchedStringInText, int?categoryId,
                                             string exactDate, string fromDate, string toDate,
                                             string creator, NotesSearchByStatus searchByStatus, string sortOption, bool sortIsAscending,
                                             List <UserEntityDTO> entities, int selectedEntityId, List <CategoryViewModel> entityCategories,
                                             int pageSize, AddNoteViewModel addNoteVm, string previousPageLink, string nextPageLink)
 {
     return(new NotesViewModel()
     {
         UserEntites = _mapper.Map <List <UserEntityDTO>, List <UserEntityViewModel> >(entities),
         SelectedEntityId = selectedEntityId,
         Notes = new PaginatedList <NoteViewModel>(new List <NoteViewModel>(), 0, 1, pageSize),
         AddNote = addNoteVm,
         SearchNotes = new SearchNotesViewModel
         {
             EntityId = selectedEntityId,
             EntityCategories = entityCategories,
             Creator = creator,
             SearchByStatus = searchByStatus,
             SearchedStringInText = searchedStringInText,
             SearchCategoryId = categoryId,
             ExactDate = ParseNullableDate(exactDate),
             FromDate = ParseNullableDate(fromDate),
             ToDate = ParseNullableDate(toDate),
             SortOption = sortOption,
             SortIsAscending = sortIsAscending
         },
         PreviousPageLink = previousPageLink,
         NextPageLink = nextPageLink
     });
 }
Ejemplo n.º 2
0
        public async Task <NotesSearchResultDTO> SearchAsync(int entityId, string searchedString, int?categoryId,
                                                             DateTime?exactDate, DateTime?fromDate, DateTime?toDate, string creator, NotesSearchByStatus searchByStatus, string sortOption, bool sortIsAscending, int?skip, int?take)
        {
            if (fromDate != null && toDate != null)
            {
                if (fromDate > toDate)
                {
                    throw new ApplicationException("From date should be greater than to date.");
                }
            }

            return(new NotesSearchResultDTO
            {
                NotesCount = await _repository.CountAsync(entityId, searchedString, categoryId, exactDate, fromDate, toDate, creator, searchByStatus),
                Notes = (await _repository.SearchAsync(entityId, searchedString, categoryId, exactDate, fromDate, toDate, creator, searchByStatus, sortOption, sortIsAscending, skip, take))
                        .Select(note => ConvertToNoteDTO(note))
                        .ToList()
            });
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Notes(int?entityId, string searchedStringInText,
                                                int?categoryId, string exactDate, string fromDate, string toDate, string creator,
                                                NotesSearchByStatus searchByStatus, string sortOption, bool sortIsAscending, int?pageNumber)
        {
            string userId   = GetLoggedUserId();
            var    userName = HttpContext.User.Identity.Name;

            var entities = await _userEntitiesService.GetAllUserEntitiesAsync(userId);

            var selectedEntityId = entityId ?? entities[0].EntityId;
            var entityCategories = await GetEntityCategoriesAsync(selectedEntityId);

            int pageSize  = 5;
            int pageIndex = pageNumber ?? 1;
            int?skip      = (pageIndex - 1) * (pageSize);
            int?take      = pageSize;

            AddNoteViewModel addNoteVm = new AddNoteViewModel()
            {
                Note = new NoteViewModel
                {
                    EntityId = selectedEntityId,
                    NoteUser = new NoteUserViewModel
                    {
                        Id   = userId,
                        Name = userName
                    }
                },
                EntityCategories = entityCategories,
            };

            NotesSearchResultDTO notesSearchResult = null;
            string errorMessage = null;

            try
            {
                notesSearchResult = await _notesService.SearchAsync(selectedEntityId, searchedStringInText,
                                                                    categoryId, ParseNullableDate(exactDate), ParseNullableDate(fromDate),
                                                                    ParseNullableDate(toDate), creator, searchByStatus, sortOption, sortIsAscending,
                                                                    skip, pageSize);
            }
            catch (ApplicationException ex)
            {
                errorMessage = ex.Message;
            }
            catch (Exception ex)
            {
                errorMessage = "Error loading notes. Please try again.";
            }
            NotesViewModel vm = CreateNotesViewModel(searchedStringInText, categoryId,
                                                     exactDate, fromDate, toDate,
                                                     creator, searchByStatus, sortOption, sortIsAscending,
                                                     entities, selectedEntityId, entityCategories,
                                                     pageSize, addNoteVm, null, null);

            if (!string.IsNullOrEmpty(errorMessage))
            {
                vm.ErrorMessage = errorMessage;
                return(View(vm));
            }

            var notes      = notesSearchResult.Notes;
            var notesCount = notesSearchResult.NotesCount;


            var previousPageLink = Url.Action(nameof(Notes), "Notes", new
            {
                entityId,
                searchedStringInText,
                categoryId,
                exactDate,
                fromDate,
                toDate,
                creator,
                sortOption,
                sortIsAscending,
                pageNumber = (pageNumber ?? 1) - 1
            });


            var nextPageLink = Url.Action(nameof(Notes), "Notes", new
            {
                entityId,
                searchedStringInText,
                categoryId,
                exactDate,
                fromDate,
                toDate,
                creator,
                sortOption,
                sortIsAscending,
                pageNumber = (pageNumber ?? 1) + 1
            });

            vm.Notes = new PaginatedList <NoteViewModel>(
                notes.Select(x => ConvertNoteDtoToNoteViewModel(x, userId)).ToList(),
                notesCount, pageIndex, pageSize);
            vm.PreviousPageLink = previousPageLink;
            vm.NextPageLink     = nextPageLink;

            return(View(vm));
        }