public CMSResult SendAppNotification(StudentTimetableViewModel viewModel, int studentClassTimetableId)
        {
            CMSResult cmsResult        = new CMSResult();
            var       notificationList = new List <SendNotificationByPlayerId>();

            try
            {
                var studentsList = _studentService.GetAllStudentParentList();
                if (viewModel.SelectedBranches != null)
                {
                    var branches = viewModel.SelectedBranches.Split(',').Select(x => int.Parse(x)).ToList();
                    studentsList = studentsList.Where(x => branches.Contains(x.BranchId));
                }
                if (viewModel.SelectedClasses != null)
                {
                    var classes = viewModel.SelectedClasses.Split(',').Select(x => int.Parse(x)).ToList();
                    studentsList = studentsList.Where(x => classes.Contains(x.ClassId));
                }
                if (viewModel.SelectedBatches != null)
                {
                    var batches = viewModel.SelectedBatches.Split(',').Select(x => int.Parse(x)).ToList();
                    studentsList = studentsList.Where(x => batches.Contains(x.BatchId));
                }

                var parentPlayerIds = studentsList.Where(x => !string.IsNullOrEmpty(x.parentAppPlayerId)).ToList();
                foreach (var playerId in parentPlayerIds)
                {
                    var studentSId          = playerId.SId;
                    var sendAppNotification = new SendNotificationByPlayerId
                    {
                        Message    = "CTT-" + viewModel.Description + "$^$" + playerId.SId + "@" + studentClassTimetableId,
                        PlayerIds  = playerId.parentAppPlayerId,
                        AppIds     = ConfigurationManager.AppSettings[Common.Constants.ParentAppId],
                        RestApiKey = ConfigurationManager.AppSettings[Common.Constants.ParentRestAppId]
                    };
                    notificationList.Add(sendAppNotification);
                }
                var notification = notificationList.ToArray();
                if (notificationList.Count > 0)
                {
                    HostingEnvironment.QueueBackgroundWorkItem(cancellationToken => _sendNotificationService.StartProcessingByPlayerId(notification, cancellationToken));
                    cmsResult.Results.Add(new Result {
                        Message = "App Notification sent successfully.", IsSuccessful = true
                    });
                }
                else
                {
                    cmsResult.Results.Add(new Result {
                        Message = "No one is registered in parent app.", IsSuccessful = true
                    });
                }
            }
            catch (Exception ex)
            {
                cmsResult.Results.Add(new Result {
                    Message = ex.Message, IsSuccessful = true
                });
            }
            return(cmsResult);
        }
        public void ReturnViewModel(StudentTimetableViewModel viewModel, string roles, string roleUserId)
        {
            if (roles == "Admin")
            {
                var branchList = (from b in _branchService.GetAllBranches()
                                  select new SelectListItem
                {
                    Value = b.BranchId.ToString(),
                    Text = b.Name
                }).ToList();
                viewModel.Branches = branchList;
                ViewBag.BranchId   = 0;
            }
            else if (roles == "BranchAdmin")
            {
                var projection = _branchAdminService.GetBranchAdminById(roleUserId);
                ViewBag.BranchId = projection.BranchId;
                var classList = (from c in _studentService.GetStudentsByBranchId(projection.BranchId)
                                 select new SelectListItem
                {
                    Value = c.ClassId.ToString(),
                    Text = c.ClassName
                }).ToList();

                viewModel.BranchId   = projection.BranchId;
                viewModel.BranchName = projection.BranchName;
                viewModel.Classes    = classList;
            }

            viewModel.CurrentUserRole = roles;
        }
        public CMSResult SendTimetable(StudentTimetableViewModel viewModel)
        {
            var cmsResult = new CMSResult();

            try
            {
                var    projection  = _branchAdminService.GetBranchAdminById(User.Identity.GetUserId());
                var    bodySubject = "Student Time Table Created";
                string body        = string.Empty;
                using (StreamReader reader = new StreamReader(Server.MapPath("~/MailDesign/CommonMailDesign.html")))
                {
                    body = reader.ReadToEnd();
                }
                body = body.Replace("{BranchName}", projection.BranchName);
                body = body.Replace("{ModuleName}", User.Identity.GetUserName() + "<br/>" + "Student Time Table:" + viewModel.Description + "<br/>");
                body = body.Replace("{BranchAdminEmail}", User.Identity.GetUserName());

                var emailMessage = new MailModel
                {
                    IsBranchAdmin = true,
                    Body          = body,
                    Subject       = bodySubject,
                    To            = ConfigurationManager.AppSettings[Common.Constants.AdminEmail]
                };
                var result = _emailService.Send(emailMessage);
                if (result)
                {
                    cmsResult.Results.Add(new Result {
                        Message = "Sent Successfully.", IsSuccessful = true
                    });
                }
                else
                {
                    cmsResult.Results.Add(new Result {
                        Message = "Something went wrong.", IsSuccessful = false
                    });
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex.Message + "catch SendTimetable");
                throw;
            }

            return(cmsResult);
        }
        public ActionResult Create(StudentTimetableViewModel viewModel)
        {
            var roleUserId = User.Identity.GetUserId();
            var roles      = _aspNetRolesService.GetCurrentUserRole(roleUserId);
            var cmsResult  = new CMSResult();

            if (ModelState.IsValid)
            {
                string fileName = "";
                Guid   guid     = Guid.NewGuid();
                if (viewModel.FilePath != null)
                {
                    if (viewModel.AttachmentDescription == null)
                    {
                        _logger.Warn("Please enter attachment description.");
                        Warning("Please enter attachment description.", true);
                        ReturnViewModel(viewModel, roles, roleUserId);
                        return(View(viewModel));
                    }
                    else if (Common.Constants.ImageTypes.Contains(viewModel.FilePath.ContentType) ||
                             Common.Constants.PdfType.Contains(viewModel.FilePath.ContentType))
                    {
                        if (viewModel.FilePath != null)
                        {
                            if (Common.Constants.ImageTypes.Contains(viewModel.FilePath.ContentType))
                            {
                                fileName = string.Format("{0}.jpg", guid);
                            }
                            else
                            {
                                fileName = string.Format("{0}.pdf", guid);
                            }
                        }
                    }
                    else
                    {
                        _logger.Warn("Please choose either a JPEG, JPG, PNG image or pdf file.");
                        Warning("Please choose either a JPEG, JPG, PNG image or pdf file", true);
                        ReturnViewModel(viewModel, roles, roleUserId);
                        return(View(viewModel));
                    }
                }
                var description = viewModel.Description.Replace("\r\n", "<br />");
                viewModel.Description = description;
                var studentClassTimetable = new StudentTimetable
                {
                    Description           = viewModel.Description,
                    SelectedBranches      = viewModel.SelectedBranches != null ? viewModel.SelectedBranches : "",
                    SelectedClasses       = viewModel.SelectedClasses != null ? viewModel.SelectedClasses : "",
                    SelectedBatches       = viewModel.SelectedBatches != null ? viewModel.SelectedBatches : "",
                    FileName              = fileName,
                    Category              = Common.Enums.TimetableCategory.ClassTimetable,
                    AttachmentDescription = viewModel.AttachmentDescription != null ? viewModel.AttachmentDescription : "",
                    StudentTimetableDate  = viewModel.StudentTimetableDate
                };
                var result = _studentTimetableService.Save(studentClassTimetable);

                if (result.Success)
                {
                    var studentClassTimetableId = studentClassTimetable.StudentTimetableId;

                    if (viewModel.FilePath != null)
                    {
                        var    pathToSaveQI = "";
                        string folderPath   = Server.MapPath(string.Concat("~/PDF/", Common.Constants.StudentTimeTableFile));
                        if (!Directory.Exists(folderPath))
                        {
                            Directory.CreateDirectory(folderPath);
                        }

                        if (Common.Constants.ImageTypes.Contains(viewModel.FilePath.ContentType))
                        {
                            pathToSaveQI = Path.Combine(folderPath, string.Format("{0}.jpg", guid));
                        }
                        else
                        {
                            pathToSaveQI = Path.Combine(folderPath, string.Format("{0}.pdf", guid));
                        }

                        if (viewModel.FilePath != null)
                        {
                            viewModel.FilePath.SaveAs(pathToSaveQI);
                        }
                    }

                    var sendAppNotification = SendAppNotification(viewModel, studentClassTimetableId);

                    if (roles == "BranchAdmin")
                    {
                        SendTimetable(viewModel);
                    }

                    Success(result.Results.FirstOrDefault().Message + "<br />" + sendAppNotification.Results[0].Message);
                    ModelState.Clear();
                    viewModel = new StudentTimetableViewModel();
                }
                else
                {
                    _logger.Warn(result.Results.FirstOrDefault().Message);
                    Warning(result.Results.FirstOrDefault().Message, true);
                }
            }
            ReturnViewModel(viewModel, roles, roleUserId);
            return(View(viewModel));
        }