예제 #1
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            Board = await _context.Boards
                    .Include(b => b.Display)
                    .Include(b => b.Location)
                    .AsNoTracking()
                    .FirstOrDefaultAsync(m => m.ID == id);

            var displayToUpdate = await _context.Displays.FindAsync(id);

            var user = await _userManager.GetUserAsync(User);

            var userName = await _userManager.GetUserNameAsync(user);

            //Try updating with posted values
            if (await TryUpdateModelAsync <Display>(
                    displayToUpdate,
                    "display", // Prefix for form value. (Bind with Display)
                    s => s.Screenshot_URL))
            {
                await _context.SaveChangesAsync();
            }

            //Save in History
            _context.Histories.Add( //Add
                new History
            {
                H_Name        = Board.Name,
                H_Address     = Board.Location.Address,
                H_NowDateTime = DateTime.Now,
                H_User        = userName
            });
            await _context.SaveChangesAsync();

            Message = "Successfully Edited. Please check history.";

            return(RedirectToPage("/VMS/Table"));
        }
예제 #2
0
        public async Task <IActionResult> OnPostUpdateAsync(int[] selectedMessageIds)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            //1. Check selected Boards ; 2. Check selected messages
            string[] selectedBoardIds  = SelectedBoardIds.Split(',');
            var      allEditorMessages = _context.EditorMessages;

            var user = await _userManager.GetUserAsync(User);

            var userName = await _userManager.GetUserNameAsync(user);

            foreach (var boardId in selectedBoardIds) //Selected Boards
            {
                int boardIdInt           = Int16.Parse(boardId);
                var boardDisplayToUpdate = await _context.Displays
                                           .Include(d => d.MessageAssignments)
                                           .ThenInclude(ma => ma.EditorMessage)
                                           .FirstOrDefaultAsync(d => d.BoardID == boardIdInt);

                //Try updating with posted values
                if (await TryUpdateModelAsync <Display>(
                        boardDisplayToUpdate,
                        "Display",
                        d => d.Screenshot_URL))
                {
                    var selectedMessagesHS = new HashSet <int>(selectedMessageIds);
                    var boardMessages      = new HashSet <int>(boardDisplayToUpdate.MessageAssignments.Select(ms => ms.EditorMessageID));
                    foreach (var editorMessage in allEditorMessages)
                    {
                        if (selectedMessageIds.Contains(editorMessage.ID))   //if message selected and found
                        {
                            if (!boardMessages.Contains(editorMessage.ID))   //if new and non-existing in the MessageAssignment Table
                            {
                                boardDisplayToUpdate.MessageAssignments.Add( //Add
                                    new MessageAssignment
                                {
                                    DisplayBoardID  = boardDisplayToUpdate.BoardID,
                                    EditorMessageID = editorMessage.ID
                                });
                            }
                        }
                        else //if message NOT selected
                        {
                            if (boardMessages.Contains(editorMessage.ID)) //if exists in the MessageAssignment Table
                            {
                                MessageAssignment editorMessageToRemove //Remove
                                    = boardDisplayToUpdate
                                      .MessageAssignments
                                      .SingleOrDefault(b => b.EditorMessageID == editorMessage.ID);
                                _context.Remove(editorMessageToRemove);
                            }
                        }
                    }
                    await _context.SaveChangesAsync();
                }
            }

            //Save in History
            _context.Histories.Add( //Add
                new History
            {
                H_Name        = SelectedBoardNames,
                H_Address     = "[ADDRESS]",
                H_NowDateTime = DateTime.Now,
                H_User        = userName
            });
            await _context.SaveChangesAsync();

            Message = "Successfully Edited. Please check history.";

            return(RedirectToPage("/VMS/Table"));;
        }