public async Task<NotesIndexModel> Build(Guid userId, NotesFilterModel arguments)
        {
            if (arguments == null)
                throw new ArgumentNullException();

            var user = await _userService.GetUser(userId);
            if (user == null)
                throw new ModelBuilderException("user not found");

            IQueryable<Note> notes;
            Guid? userIdFilter = null;
            var model = new NotesIndexModel 
            { 
                Filter = arguments,
                IsAdmin = user.IsAdmin
            };

            userIdFilter = user.IsAdmin ? userIdFilter : userId;          

            notes = _notesService
                   .GetFilteredNotes(userIdFilter, arguments.IsPublic, arguments.KeyPhrase);

            model.Data = notes.Select(x => new NoteTableModel
            {
                Id = x.Id,
                CreatingDate = x.CreatingDate,
                IsPublic = x.IsPublic,
                Title = x.Title,
                UserId = x.OwnerId
            });

            model.PublicDropDownList = GetList(arguments);

            return model;
        }
 private IEnumerable<SelectListItem> GetList(NotesFilterModel arguments)
 {
     return new List<SelectListItem>()
     {
         new SelectListItem
         {
             Selected = arguments.IsPublic == null, 
             Text = NotesIndexUIResources.AllNotes, 
             Value = ""
         },
         new SelectListItem
         {
             Selected = arguments.IsPublic == true,
             Text = NotesIndexUIResources.OnlyPublic, 
             Value = true.ToString()
         },
         new SelectListItem
         {
             Selected = arguments.IsPublic == false,
             Text = NotesIndexUIResources.OnlyNotPublic, 
             Value = false.ToString()
         }
     };
 }
Exemplo n.º 3
0
 public async Task<ActionResult> Index(NotesFilterModel filter = null)
 {
     var builder = DependencyResolver.Current.GetService<NotesIndexModelBuilder>();
     var model = await builder.Build(GetUserId().Value, filter);
     return View(model);
 }