Пример #1
0
        /// <summary>
        /// Publishes the specified FormModel to the specified <see cref="Altask.Data.Model.Form"/>.
        /// </summary>
        /// <param name="formId"></param>
        /// <param name="formModel"></param>
        /// <returns>Returns a <see cref="Altask.Data.EntityResult"/> indicating success or failure.</returns>
        public virtual async Task <ActionResult> Publish(Altask.Data.Dto.Form form)
        {
            ThrowIfDisposed();

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var formEntity = await Context.Forms.FindAsync(form.Id);

            if (formEntity == null)
            {
                return(BadRequest(ErrorDescriber.DoesNotExist("Form")));
            }

            var beforeModel   = formEntity.PublishedModel;
            var beforeVersion = formEntity.Version;

            formEntity.FromDto(form);
            formEntity.PublishedModel = formEntity.DraftModel;
            formEntity.Version        = DateTime.Now.ToString("yyyy.MM.dd.HH.mm.ss");
            formEntity.Logs.Add(new FormLog()
            {
                AfterModel    = formEntity.PublishedModel,
                AfterVersion  = formEntity.Version,
                BeforeModel   = beforeModel,
                BeforeVersion = beforeVersion,
                Type          = "Publish"
            });

            Context.Entry(formEntity).State = EntityState.Modified;
            var result = await Context.SaveChangesAsync();

            Context.Entry(formEntity).Collection(e => e.Logs).Load();

            if (result.Succeeded)
            {
                return(Ok(new { form = formEntity.ToDto() }));
            }
            else
            {
                return(BadRequest(result));
            }
        }
Пример #2
0
        /// <summary>
        /// Maps a <see cref='Altask.Data.Model.Form'/> object to a <see cref='Altask.Data.Dto.Form'/> object.
        /// </summary>
        /// <param name="includeLogs">Indicates whether to load any logs associated with the object when mapping.</param>
        public static Altask.Data.Dto.Form ToDto(this Altask.Data.Model.Form entity, bool includeLogs = false)
        {
            var dto = new Altask.Data.Dto.Form();

            dto.Id             = entity.Id;
            dto.Active         = entity.Active;
            dto.Name           = entity.Name;
            dto.Description    = entity.Description;
            dto.DraftModel     = entity.DraftModel;
            dto.PublishedModel = entity.PublishedModel;
            dto.Metadata       = JsonConvert.DeserializeObject("{Properties: []}");

            if (!string.IsNullOrEmpty(entity.Metadata))
            {
                try {
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(entity.Metadata);
                    string json = Json.SerizlieXmlDocument(doc);
                    dto.Metadata = JsonConvert.DeserializeObject(json);
                } catch (Exception) {}
            }

            dto.Version   = entity.Version;
            dto.CreatedBy = entity.CreatedBy;
            dto.CreatedOn = entity.CreatedOn;
            dto.UpdatedBy = entity.UpdatedBy;
            dto.UpdatedOn = entity.UpdatedOn;

            dto.Logs = new List <Altask.Data.Dto.FormLog>();

            if (includeLogs)
            {
                if (entity.Logs != null)
                {
                    foreach (var item in entity.Logs)
                    {
                        dto.Logs.Add(item.ToDto());
                    }
                }
            }

            return(dto);
        }
Пример #3
0
        internal static void NotifyFormUpdate(Guid?clientId, Altask.Data.Dto.Form form)
        {
            if (!clientId.HasValue)
            {
                _context.Clients.All.notifyFormUpdate(new { form = form });
            }
            else
            {
                SignalRConnection connection;

                if (_connections.TryGetValue(clientId.Value, out connection))
                {
                    _context.Clients.AllExcept(connection.ConnectionId).notifyFormUpdate(new { connection = connection, form = form });
                }
                else
                {
                    _context.Clients.All.notifyFormCreate(new { form = form });
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Maps all the non-primary key and tracking properties of a <see cref='Altask.Data.Dto.Form'/> object to a <see cref='Altask.Data.Model.Form'/> object.
        /// </summary>
        public static Altask.Data.Model.Form FromDto(this Altask.Data.Model.Form model, Altask.Data.Dto.Form entity)
        {
            model.Active         = entity.Active;
            model.Name           = entity.Name;
            model.Description    = entity.Description;
            model.DraftModel     = entity.DraftModel;
            model.PublishedModel = entity.PublishedModel;
            model.Metadata       = string.Empty;

            try {
                model.Metadata = ((XmlDocument)JsonConvert.DeserializeXmlNode(entity.Metadata.ToString(), "Properties")).OuterXml;
            } catch (Exception) {}

            model.Version = entity.Version;
            return(model);
        }