private entity_group_versions MapVersions(entity_groups entityGroup)
 {
     return(new entity_group_versions
     {
         CreatedAt = entityGroup.CreatedAt,
         MicrotingUid = entityGroup.MicrotingUid,
         Name = entityGroup.Name,
         Type = entityGroup.Type,
         UpdatedAt = entityGroup.UpdatedAt,
         Version = entityGroup.Version,
         WorkflowState = entityGroup.WorkflowState,
         EntityGroupId = entityGroup.Id
     });
 }
        public async Task Delete(MicrotingDbContext dbContext)
        {
            entity_groups entityGroups = await dbContext.entity_groups.FirstOrDefaultAsync(x => x.Id == Id);

            if (entityGroups == null)
            {
                throw new NullReferenceException($"Could not find Entity Group with Id: {Id}");
            }

            entityGroups.WorkflowState = Constants.Constants.WorkflowStates.Removed;

            if (dbContext.ChangeTracker.HasChanges())
            {
                entityGroups.UpdatedAt = DateTime.UtcNow;
                entityGroups.Version  += 1;

                dbContext.entity_group_versions.Add(MapVersions(entityGroups));
                await dbContext.SaveChangesAsync().ConfigureAwait(false);
            }
        }
        public static async Task <EntityGroup> ReadSorted(MicrotingDbContext dbContext, string entityGroupMUId, string sort,
                                                          string nameFilter)
        {
            entity_groups eG =
                await dbContext.entity_groups.SingleOrDefaultAsync(x => x.MicrotingUid == entityGroupMUId);

            if (eG == null)
            {
                return(null);
            }

            List <EntityItem> lst   = new List <EntityItem>();
            EntityGroup       rtnEG = new EntityGroup
            {
                Id                 = eG.Id,
                Name               = eG.Name,
                Type               = eG.Type,
                MicrotingUUID      = eG.MicrotingUid,
                EntityGroupItemLst = lst,
                WorkflowState      = eG.WorkflowState,
                CreatedAt          = eG.CreatedAt,
                UpdatedAt          = eG.UpdatedAt
            };

            List <entity_items> eILst = null;

            if (string.IsNullOrEmpty(nameFilter))
            {
                eILst = dbContext.entity_items.Where(x => x.EntityGroupId == eG.Id &&
                                                     x.WorkflowState !=
                                                     Constants.Constants.WorkflowStates.Removed &&
                                                     x.WorkflowState != Constants.Constants.WorkflowStates
                                                     .FailedToSync).CustomOrderBy(sort).ToList();
            }
            else
            {
                eILst = dbContext.entity_items.Where(x => x.EntityGroupId == eG.Id &&
                                                     x.WorkflowState !=
                                                     Constants.Constants.WorkflowStates.Removed &&
                                                     x.WorkflowState != Constants.Constants.WorkflowStates
                                                     .FailedToSync &&
                                                     x.Name.Contains(nameFilter)).CustomOrderBy(sort).ToList();
            }

            if (eILst.Count > 0)
            {
                foreach (entity_items item in eILst)
                {
                    EntityItem eI = new EntityItem
                    {
                        Id            = item.Id,
                        Name          = item.Name,
                        Description   = item.Description,
                        EntityItemUId = item.EntityItemUid,
                        MicrotingUUID = item.MicrotingUid,
                        WorkflowState = item.WorkflowState,
                        DisplayIndex  = item.DisplayIndex
                    };
                    lst.Add(eI);
                }
            }

            return(rtnEG);
        }