Exemple #1
0
        public async Task <UpsertModel> UpsertMessage(UpsertMode mode, MessageViewModel model)
        {
            var upsert = new UpsertModel();

            try
            {
                // Apply changes
                Message = model.ParseAsEntity(Message);

                if (model.SendNow)
                {
                    var term = db.Terms.FirstOrDefault(x => x.IsCurrentTerm);

                    var parents = db.StudentParents.ToList().Where(x => x.Student.CurrentTermId == term.TermId && x.Parent.HasContact).ToList() /*.Select(m => m.Parent)*/;

                    foreach (var parent in parents)
                    {
                        var    result = sendTextMessage(parent.Parent.Contacts, model.MessageDescription);
                        string msgTitle;
                        System.Text.StringBuilder msgBuilder;

                        if (result.Contains("1701"))
                        {
                            msgTitle   = "Message Sent";
                            msgBuilder = new System.Text.StringBuilder()
                                         .Append("The following Message has been sent:")
                                         .AppendLine();
                        }
                        else
                        {
                            msgTitle   = "Message Sending Failed";
                            msgBuilder = new System.Text.StringBuilder()
                                         .Append("The following Message has failed to be sent:")
                                         .AppendLine();
                        }

                        msgBuilder.AppendLine().AppendFormat("Message: {0}", Message.MessageDescription)
                        .AppendLine().AppendFormat("Student: {0}", parent.Student.FullName)
                        .AppendLine().AppendFormat("Parent: {0}", parent.Parent.FullName);


                        // Record activity
                        var msgActivity = CreateActivity(msgTitle, msgBuilder.ToString());
                        msgActivity.UserId    = ServiceUserId;
                        msgActivity.ParentId  = parent.ParentId;
                        msgActivity.StudentId = parent.StudentId;
                    }

                    Message.Sent = DateTime.Now;
                    // make sure you create activity here after testing
                }

                Activity activity;
                string   title;
                System.Text.StringBuilder builder;

                builder = new System.Text.StringBuilder();

                if (model.MessageId == 0)
                {
                    db.Messages.Add(Message);

                    title = "Message Recorded";
                    builder.Append("A Message record has been made. ").AppendLine();
                }
                else
                {
                    db.Entry(Message).State = System.Data.Entity.EntityState.Modified;

                    title = "Message Updated";
                    builder.Append("The following changes have been made to the Message details");

                    if (mode == UpsertMode.Admin)
                    {
                        builder.Append(" (by the Admin)");
                    }

                    builder.Append(":").AppendLine();
                }

                await db.SaveChangesAsync();

                MessageId = Message.MessageId;


                // Save activity now so we have a MessageId. Not ideal, but hey
                activity        = CreateActivity(title, builder.ToString());
                activity.UserId = ServiceUserId;

                await db.SaveChangesAsync();

                if (model.MessageId == 0)
                {
                    upsert.ErrorMsg = "Message record created successfully";
                }
                else
                {
                    upsert.ErrorMsg = "Message record updated successfully";
                }

                upsert.RecordId = Message.MessageId.ToString();
            }
            catch (Exception ex)
            {
                upsert.ErrorMsg = ex.Message;
                //RecordException("Update Message Error", ex);
            }

            return(upsert);
        }