public async Task <IActionResult> Content(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            List <NoteHeader> headers = await _context.NoteHeader
                                        .Include("NoteContent")
                                        .Where(p => p.NoteFileId == id)
                                        .OrderBy(p => p.Id)
                                        .ToListAsync();

            List <NoteContent> content = new List <NoteContent>();

            foreach (var item in headers)
            {
                content.Add(item.NoteContent);
            }

            await AccessManager.Audit(_context, "Content", User.Identity.Name,
                                      User.Identity.Name, "View NoteContent of NotesFileID " + id);

            return(View(content));
        }
        public async Task <IActionResult> Edit(int id, [Bind("Id,NumberArchives,OwnerId,NoteFileName,NoteFileTitle,LastEdited")] NoteFile noteFile)
        {
            if (id != noteFile.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    await AccessManager.Audit(_db, "Edit", User.Identity.Name, _userManager.GetUserId(User), "Edit NotesFile " + noteFile.NoteFileName);

                    noteFile.LastEdited = DateTime.Now.ToUniversalTime();
                    _db.Update(noteFile);
                    await _db.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!NoteFileExists(noteFile.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewBag.OwnerId = new SelectList(_db.UserData, "UserId", "DisplayName", noteFile.OwnerId);
            return(View(noteFile));
        }
        // GET: DataSpy
        public async Task <IActionResult> Index()
        {
            await AccessManager.Audit(_context, "Index", User.Identity.Name,
                                      User.Identity.Name, "View list of NotesFiles");

            return(View(await _context.NoteFile.ToListAsync()));
        }
        public async Task <IActionResult> Users()
        {
            List <UserData> s = await _context.UserData.ToListAsync();    //_context.ApplicationUser.ToListAsync();

            await AccessManager.Audit(_context, "Users", User.Identity.Name,
                                      User.Identity.Name, "View Users");

            return(View("Users2", s));
        }
        public async Task <IActionResult> TimeZones()
        {
            List <TZone> s = await _context.TZone.OrderBy(p => p.OffsetHours).ThenBy(p => p.OffsetMinutes).ToListAsync();

            await AccessManager.Audit(_context, "TZone", User.Identity.Name,
                                      User.Identity.Name, "View TZone");

            return(View(s));
        }
        public async Task <IActionResult> Audit()
        {
            List <Audit> s = await _context.Audit.OrderByDescending(p => p.AuditID).ToListAsync();

            await AccessManager.Audit(_context, "Audit", User.Identity.Name,
                                      User.Identity.Name, "View Audit");

            return(View(s));
        }
        public async Task <IActionResult> Sequencer()
        {
            List <Sequencer> s = await _context.Sequencer.OrderBy(p => p.UserId).ToListAsync();

            await AccessManager.Audit(_context, "Sequencer", User.Identity.Name,
                                      User.Identity.Name, "View Sequencer");

            return(View(s));
        }
        public async Task <IActionResult> Marks()
        {
            List <Mark> mark = await _context.Mark.OrderBy(p => p.UserId).ToListAsync();

            await AccessManager.Audit(_context, "Marks", User.Identity.Name,
                                      User.Identity.Name, "View Marks");

            return(View(mark));
        }
        public async Task <IActionResult> DeleteConfirmed(int id)
        {
            NoteFile noteFile = await NoteDataManager.GetFileById(_db, id);

            await AccessManager.Audit(_db, "Delete", User.Identity.Name, _userManager.GetUserId(User), "Delete NotesFile " + noteFile.NoteFileName);

            await NoteDataManager.DeleteNoteFile(_db, id);

            return(RedirectToAction("Index"));
        }
        public async Task <IActionResult> Access(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            List <NoteAccess> access = await _context.NoteAccess.Where(m => m.NoteFileId == id).ToListAsync();

            await AccessManager.Audit(_context, "Access", User.Identity.Name,
                                      User.Identity.Name, "View NoteAccess of NotesFileID " + id);

            return(View(access));
        }
        public async Task <IActionResult> EditContent(NoteContent nc)
        {
            NoteContent edited = await _context.NoteContent.SingleAsync(p => p.NoteHeaderId == nc.NoteHeaderId);

            edited.NoteBody        = nc.NoteBody;
            edited.DirectorMessage = nc.DirectorMessage;

            await AccessManager.Audit(_context, "EditContent", User.Identity.Name,
                                      User.Identity.Name, "Edit Content NoteID " + edited.NoteHeaderId);

            _context.Entry(edited).State = EntityState.Modified;
            await _context.SaveChangesAsync();

            return(RedirectToAction("Index", new { id = await NoteDataManager.GetNoteById(_context, nc.NoteHeaderId) }));
        }
        public async Task <IActionResult> Create(/*[Bind(Include = "NoteFileID,NoteFileName,NoteFileTitle,LastEdited")]*/ NoteFile noteFile)
        {
            if (ModelState.IsValid)
            {
                await AccessManager.Audit(_db, "Create", User.Identity.Name, _userManager.GetUserId(User), "Create NotesFile " + noteFile.NoteFileName);

                if (!await NoteDataManager.CreateNoteFile(_db, _userManager, _userManager.GetUserId(User), noteFile.NoteFileName, noteFile.NoteFileTitle))
                {
                    return(View(noteFile));
                }

                await AccessManager.Audit(_db, "Failed", User.Identity.Name, _userManager.GetUserId(User), "Create NotesFile " + noteFile.NoteFileName);

                return(RedirectToAction("Index"));
            }

            return(View(noteFile));
        }
        public async Task <IActionResult> Headers(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            //List<NoteHeader> headers = await NoteDataManager.GetBaseNoteHeadersForFile(_context, (int)id);

            List <NoteHeader> headers = await _context.NoteHeader
                                        .Where(p => p.NoteFileId == id)
                                        .OrderBy(p => p.Id)
                                        .ToListAsync();

            await AccessManager.Audit(_context, "Headers", User.Identity.Name,
                                      User.Identity.Name, "View BaseNoteHeaders of NotesFileID " + id);

            return(View(headers));
        }
        // GET: DataSpy/Details/5
        public async Task <IActionResult> Details(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            NoteFile noteFile = await _context.NoteFile.SingleAsync(m => m.Id == id);

            if (noteFile == null)
            {
                return(NotFound());
            }

            await AccessManager.Audit(_context, "Details", User.Identity.Name,
                                      User.Identity.Name, "View details of NotesFile " + noteFile.NoteFileName);

            return(View(noteFile));
        }
        /// <summary>
        /// Create a New NoteFile with default Access Controls.
        /// "Other" has no access.  Creator has full access.
        /// </summary>
        /// <param name="name">Name for the file</param>
        /// <param name="title">Title of the file</param>
        /// <returns></returns>
        public async Task <bool> CreateNoteFile(string name, string title)
        {
            await AccessManager.Audit(_db, "Create", User.Identity.Name, _userManager.GetUserId(User), "Create NotesFile " + name);

            return(await NoteDataManager.CreateNoteFile(_db, _userManager, _userManager.GetUserId(User), name, title));
        }