private List <FormElementItem> GetForm(NoteTypeCache noteType, int entityId) { var form = new List <FormElementItem>(); RockContext rockContext = new RockContext(); NoteService noteService = new NoteService(rockContext); var currentPersonAliasIds = CurrentPerson.Aliases.Select(a => a.Id); var note = noteService.Queryable() .Where(n => n.NoteTypeId == noteType.Id && n.EntityId == entityId && currentPersonAliasIds.Contains(n.CreatedByPersonAliasId ?? 0)) .FirstOrDefault(); if (note == null) { note = new Rock.Model.Note { NoteTypeId = noteType.Id, EntityId = entityId, Text = "" }; } var hidden = new FormElementItem { Type = FormElementType.Hidden, Key = "entityId", Value = entityId.ToString() }; form.Add(hidden); var editor = new FormElementItem { Label = GetAttributeValue("NoteFieldLabel"), HeightRequest = GetAttributeValue("NoteFieldHeight").AsInteger(), Type = FormElementType.Editor, Key = "note", Value = note.Text, }; form.Add(editor); var button = new FormElementItem { Key = "save", Type = FormElementType.Button, Label = "Save" }; form.Add(button); return(form); }
public override MobileBlockResponse HandleRequest(string request, Dictionary <string, string> Body) { var noteTypeString = GetAttributeValue("NoteType"); var noteType = NoteTypeCache.Read(noteTypeString.AsGuid()); RockContext rockContext = new RockContext(); NoteService noteService = new NoteService(rockContext); var entityId = Body["entityId"].AsInteger(); var currentPersonAliasIds = CurrentPerson.Aliases.Select(a => a.Id); var note = noteService.Queryable() .Where(n => n.NoteTypeId == noteType.Id && n.EntityId == entityId && currentPersonAliasIds.Contains(n.CreatedByPersonAliasId ?? 0)) .FirstOrDefault(); if (note == null) { note = new Rock.Model.Note { NoteTypeId = noteType.Id, EntityId = entityId, Text = "", IsPrivateNote = true }; noteService.Add(note); } note.Text = Body["note"]; rockContext.SaveChanges(); var response = new FormResponse { Success = true, FormElementItems = GetForm(noteType, entityId), Message = "Note Saved" }; return(new MobileBlockResponse() { Request = "save", Response = JsonConvert.SerializeObject(response), TTL = 0 }); }