/// <summary> /// The SendPushNotificationToStudent. /// </summary> /// <param name="model">The model<see cref="AssessmentShare"/>.</param> /// <returns>The <see cref="Task"/>.</returns> private async Task SendPushNotificationToStudent(AssessmentShare model) { try { var assessments = await _tableStorage.GetAllAsync <Entites.Assessment>("Assessments"); var assessment = assessments.SingleOrDefault(a => a.RowKey == model.AssessmentId); var pushNotification = new PushNotification { Title = $"{assessment.SubjectName}", Body = $"Assessment available, ${assessment.AssessmentTitle}" }; var studentData = await _tableStorage.GetAllAsync <Entites.Student>("Student"); var students = studentData.Where(s => s.PartitionKey == model.SchoolId && s.ClassId == model.ClassId); foreach (var student in students) { if (!String.IsNullOrEmpty(student.NotificationToken)) { pushNotification.RecipientDeviceToken = student.NotificationToken; await _pushNotificationService.SendAsync(pushNotification); } } } catch (Exception ex) { throw new AppException("Exception thrown in Notify Service: ", ex.InnerException); } }
public async Task <IActionResult> AssessmentShare( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "assessment/share")] [RequestBodyType(typeof(AssessmentShare), "Assessment share")] HttpRequest request) { var validateStatus = base.AuthorizationStatus(request); if (validateStatus != System.Net.HttpStatusCode.Accepted) { return(new BadRequestObjectResult(validateStatus)); } string requestBody = await new StreamReader(request.Body).ReadToEndAsync(); AssessmentShare requestData = JsonConvert.DeserializeObject <AssessmentShare>(requestBody); string result; try { result = await _assessmentService.AssessmentShare(requestData); } catch (HttpResponseException ex) { return(new ConflictObjectResult(ex.Response.Content)); } return(new OkObjectResult(new { message = result })); }
/// <summary> /// The AssessmentShare. /// </summary> /// <param name="model">The model<see cref="AssessmentShare"/>.</param> /// <returns>The <see cref="Task"/>.</returns> public async Task <string> AssessmentShare(AssessmentShare model) { var sharedAssessments = await _tableStorage.GetAllAsync <Entites.AssessmentShare>("AssessmentShare"); var isShared = sharedAssessments.Any(assessment => assessment.ClassId == model.ClassId && assessment.AssessmentId == model.AssessmentId); if (isShared) { return("Already shared assessment."); } // Create new content var assessmentSharedId = String.IsNullOrEmpty(model.Id) ? Guid.NewGuid().ToString() : model.Id; var assessmentShare = new Entites.AssessmentShare(model.SchoolId, assessmentSharedId) { ClassId = model.ClassId, AssessmentId = model.AssessmentId, Active = true, CreatedBy = model.CreatedBy, UpdatedOn = DateTime.UtcNow, UpdatedBy = model.CreatedBy, }; try { await _tableStorage.AddAsync("AssessmentShare", assessmentShare); await SendPushNotificationToStudent(model); return("Assessment shared successfully."); } catch (Exception ex) { throw new AppException("Assessment share error: ", ex.InnerException); } }