예제 #1
0
        public async Task <IActionResult> Post([FromBody] FieldChangeGroup item)
        {
            return(await FunctionWrapper.ExecuteFunction(this, async() => {
                if (item.ItemId == ObjectId.Empty)
                {
                    throw new ApplicationException("Required fields not supplied.");
                }

                return await _fieldChangeGroupRepository.Add(item);
            }));
        }
예제 #2
0
        public async Task <Item> Update(Item item)
        {
            var filter = Builders <Item> .Filter.Eq("Id", item.Id);

            try
            {
                ResetTemporaryCaches();

                foreach (ItemImage img in item.Images)
                {
                    if (img.TargetItemId == ObjectId.Empty || img.TargetItemId != item.Id)
                    {
                        item.Images.Remove(img);
                    }
                }

                item = await HydrateForSave(item);

                // Compare old and new, save to change history
                var oldItem = await Get(item.Id);

                var newItem = item;
                List <FieldChange> fieldChanges = new List <FieldChange>();

                if (oldItem.Name != newItem.Name)
                {
                    fieldChanges.Add(new FieldChange()
                    {
                        FieldId   = new ObjectId("000000000000000000000000"),
                        FieldName = "Name",
                        OldValue  = oldItem.Name,
                        NewValue  = newItem.Name
                    });
                }

                if (oldItem.TypeId != newItem.TypeId)
                {
                    fieldChanges.Add(new FieldChange()
                    {
                        FieldId   = new ObjectId("000000000000000000000000"),
                        FieldName = "Type",
                        OldValue  = oldItem.TypeId.ToString(),
                        NewValue  = newItem.TypeId.ToString()
                    });
                }

                if (oldItem.WorkflowNode.Id != newItem.WorkflowNode.Id)
                {
                    fieldChanges.Add(new FieldChange()
                    {
                        FieldId   = new ObjectId("000000000000000000000000"),
                        FieldName = "Status",
                        OldValue  = (await _workflowNodeRepository.Get(oldItem.WorkflowNode.Id)).Name,
                        NewValue  = (await _workflowNodeRepository.Get(newItem.WorkflowNode.Id)).Name
                    });
                }

                // Collect old values that have changed
                foreach (FieldValue oldFv in oldItem.FieldValues)
                {
                    var newFv = newItem.FieldValues.Where(f => f.FieldId == oldFv.FieldId).FirstOrDefault();

                    if (newFv == null || newFv.Value != oldFv.Value)
                    {
                        fieldChanges.Add(new FieldChange()
                        {
                            FieldId   = oldFv.FieldId,
                            FieldName = oldFv.FieldName,
                            OldValue  = oldFv.Value,
                            NewValue  = newFv.Value
                        });
                    }
                }

                // Collect new values that weren't saved before
                foreach (FieldValue newFv in newItem.FieldValues)
                {
                    var oldFv = oldItem.FieldValues.Where(f => f.FieldId == newFv.FieldId).FirstOrDefault();

                    if (oldFv == null)
                    {
                        fieldChanges.Add(new FieldChange()
                        {
                            FieldId   = newFv.FieldId,
                            FieldName = newFv.FieldName,
                            OldValue  = "",
                            NewValue  = newFv.Value
                        });
                    }
                }

                if (fieldChanges.Count > 0)
                {
                    await _fieldChangeGroupRepository.Add(new FieldChangeGroup()
                    {
                        ChangedByUserId = new ObjectId("000000000000000000000000"),
                        ChangedDateTime = DateTime.UtcNow,
                        ItemId          = oldItem.Id,
                        FieldChanges    = fieldChanges
                    });
                }

                await _context.Items.ReplaceOneAsync(filter, item);

                return(await Get(item.Id));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }