public CMSResult SendAppNotification(DailyPracticePaperViewModel viewModel, int dailyPracticePaperId)
        {
            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    = "DPP-" + viewModel.Description + "$^$" + playerId.SId + "@" + dailyPracticePaperId,
                        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 = false
                });
            }
            return(cmsResult);
        }
 public void ReturnViewModel(DailyPracticePaperViewModel 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 SendDailyPracticePaper(DailyPracticePaperViewModel viewModel)
        {
            var cmsResult = new CMSResult();

            try
            {
                var    projection  = _branchAdminService.GetBranchAdminById(User.Identity.GetUserId());
                var    bodySubject = "Daily Practice Paper 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/>" + "Dily Practice Paper:" + 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 SendDailyPracticePaper");
                throw;
            }

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

            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 (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 dailyPracticePaper = new DailyPracticePaper
                {
                    Description            = viewModel.Description,
                    SelectedBranches       = viewModel.SelectedBranches != null ? viewModel.SelectedBranches : "",
                    SelectedClasses        = viewModel.SelectedClasses != null ? viewModel.SelectedClasses : "",
                    SelectedBatches        = viewModel.SelectedBatches != null ? viewModel.SelectedBatches : "",
                    FileName               = fileName,
                    AttachmentDescription  = viewModel.AttachmentDescription != null ? viewModel.AttachmentDescription : "",
                    DailyPracticePaperDate = viewModel.DailyPracticePaperDate
                };
                var result = _dailyPracticePaperService.Save(dailyPracticePaper);

                if (result.Success)
                {
                    var dailyPracticePaperId = dailyPracticePaper.DailyPracticePaperId;
                    if (viewModel.FilePath != null)
                    {
                        var    pathToSaveQI = "";
                        string folderPath   = Server.MapPath(string.Concat("~/PDF/", Common.Constants.DailyPracticePaperFile));
                        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, dailyPracticePaperId);

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

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