Ejemplo n.º 1
0
        /// <summary>
        /// Page Handler (from Index Page) - submit (as final)
        ///     a. Update Group Message in the database, Note: Board 0 = Multiple
        ///     Note: Files will be saved in wwwroot/uploads - 'TempFolderSingle'/'TempFolderMultiple'
        ///     Note: Clients uploaded files will be saved and copied from 'TempFolderClient'
        ///     b. Then, in this TempFolder directory, create VSN, attempt to send program
        ///     c. If fails, throw an error message
        ///     d. If passes, copy files to respective board folder, update Message Assignment, History
        /// </summary>
        public async Task <IActionResult> OnPostUpdateAsync()
        {
            int id = Board.ID; //bindproperty (hidden input), id returned from editor page

            if (!ModelState.IsValid)
            {
                return(Page());
            }

            #region Update EditorMessage; Board 0 = multiple
            for (int i = 0; i < totalmessages; i++)
            {
                int GMid = i + 1;
                var editorMessageToUpdate = await _context.EditorMessages
                                            .FirstOrDefaultAsync(m => m.ID == GMid);

                editorMessageToUpdate.Date = dateArray[i];
                //If timer is null/0, set default 1s
                if ((timerArray[i] == null) || (timerArray[i] == 0))
                {
                    editorMessageToUpdate.Timer = 1; //1s
                }
                else
                {
                    editorMessageToUpdate.Timer = timerArray[i];
                }
                editorMessageToUpdate.EditorMessageTypeID = codeArray[i];
                editorMessageToUpdate.CodePlus            = codePlusArray[i];
                editorMessageToUpdate.ImageFileName       = editorMessageToUpdate.Message;
                editorMessageToUpdate.ThisBoard           = id; // id = 0 for multiple

                _context.Update(editorMessageToUpdate);
                _context.SaveChanges();
            }
            #endregion

            #region Long process ...
            //Get selected or valid messages first
            int arrayIndex = 0;
            foreach (var item in codeArray)
            {
                int messageNumber = arrayIndex + 1;

                if (item != null)
                { //if type code is not null, it's a valid message
                    SelectedMessages.Add(messageNumber);
                }
                arrayIndex++;
            }
            //If no (valid) messages found
            if (SelectedMessages.Count == 0)
            {
                return(Page());
            }


            //Check if it's single ids (Board x) or multiple ids (Board 0)
            if ((id != 0)) //single
            {
                try { await LongProcess(id, false, SelectedMessages); }
                catch (Exception e) {
                    var    logPath = Path.Combine(_webHostEnvironment.WebRootPath, "data", "TextFile.txt");
                    string str;
                    using (StreamReader sreader = new StreamReader(logPath))
                    {
                        str = sreader.ReadToEnd();
                    }
                    System.IO.File.Delete(logPath);
                    using (StreamWriter swriter = new StreamWriter(logPath, false))
                    {
                        swriter.WriteLine("--------------------------------");
                        swriter.WriteLine(DateTime.Now);
                        swriter.WriteLine(e.ToString());
                        swriter.WriteLine(str);
                    }

                    ErrorMessage = "Something went wrong! Contact maintenance team.";
                }

                if (Upload_NOTOK.Count == 0 && Display_NOTOK.Count == 0) //All OK
                {
                    return(RedirectToPage("/VMS/Table"));
                }
                else //NOT OK
                {
                    var faultyUpload  = String.Join(",", Upload_NOTOK.ToArray());
                    var faultyDisplay = String.Join(",", Display_NOTOK.ToArray());

                    if (Upload_NOTOK.Count != 0)
                    {
                        FailMessage = "Error: Upload failed: (" + faultyUpload + ") @ " + UploadTime;
                    }
                    else
                    {
                        FailMessage = "Error: Display failed: (" + faultyDisplay + ") @ " + UploadTime;
                    }

                    return(Page());
                }
            }
            else //mutliple
            {
                string[] selectedBoardIds_string = SelectedBoardIds.Split(',');
                int[]    ids = Array.ConvertAll(selectedBoardIds_string, int.Parse);

                foreach (var thisId in ids)
                {
                    await LongProcess(thisId, true, SelectedMessages);
                }

                if (Upload_NOTOK.Count == 0 && Display_NOTOK.Count == 0) //All OK
                {
                    return(RedirectToPage("/VMS/Table"));
                }
                else //NOT OK
                {
                    var faultyUpload  = String.Join(",", Upload_NOTOK.ToArray());
                    var faultyDisplay = String.Join(",", Display_NOTOK.ToArray());

                    FailMessage = "Error: Upload failed: (" + faultyUpload + "), Display failed: (" + faultyDisplay + ")  @ " + UploadTime;
                    return(Page());
                }
            }
            #endregion
        }
Ejemplo n.º 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"));;
        }