示例#1
0
        public ServiceResult ImportStagedItem(string uuid)
        {
            if (string.IsNullOrWhiteSpace(uuid))
            {
                return(ServiceResponse.Error("No uuid sent."));
            }

            if (CurrentUser == null)
            {
                return(ServiceResponse.Error("You must be logged in to access this function."));
            }

            if (CurrentUser.RoleWeight < 92 && CurrentUser.SiteAdmin == false)
            {
                return(ServiceResponse.Error("You authorized access to this function."));
            }


            StageDataManager sdm = new StageDataManager(Globals.DBConnectionKey, "");

            var result = sdm.Get(uuid);

            if (result.Code != 200)
            {
                return(result);
            }

            var     stagedItem = result.Result as StageData;
            dynamic stagedType = null;

            using (var context = new GreenWerxDbContext(Globals.DBConnectionKey))
            {
                switch (stagedItem.DataType)
                {
                case "account":
                    stagedType        = JsonConvert.DeserializeObject <Account>(stagedItem.StageResults);
                    stagedType.Status = "new";
                    Initialize(ref stagedType, this.CurrentUser.UUID, this.CurrentUser.AccountUUID, this.CurrentUser.RoleWeight);

                    if (!context.Insert <Account>((Account)stagedType))
                    {
                        return(ServiceResponse.Error("Failed to import item."));
                    }
                    break;
                }
                context.Delete <StageData>(stagedItem);

                var stagedAttributes = context.GetAll <StageData>().Where(w => w.DataType.EqualsIgnoreCase("attribute"));

                foreach (var stageAttribute in stagedAttributes)
                {
                    var attribute = JsonConvert.DeserializeObject <TMG.Attribute>(stageAttribute.StageResults);
                    if (attribute.ReferenceUUID == stagedType.UUID)
                    {
                        if (!context.Insert <TMG.Attribute>((TMG.Attribute)attribute))
                        {
                            stageAttribute.Result = "import failed";
                            context.Update <StageData>(stageAttribute);
                        }
                    }
                }
            }
            return(ServiceResponse.OK("", stagedType));
        }
示例#2
0
        public ServiceResult DeleteStagedData(string uuid)
        {
            if (CurrentUser == null)
            {
                return(ServiceResponse.Error("You must be logged in to access this function."));
            }

            if (CurrentUser.RoleWeight < 92 && CurrentUser.SiteAdmin == false)
            {
                return(ServiceResponse.Error("You authorized access to this function."));
            }

            string error = "";

            try
            {
                StageDataManager sdm = new StageDataManager(Globals.DBConnectionKey, "");
                var res = sdm.Get(uuid);
                if (res.Code != 200)
                {
                    return(res);
                }

                var result = sdm.Get(uuid);

                if (result.Code != 200)
                {
                    return(result);
                }

                var stagedItem = result.Result as StageData;


                //var delRes = sdm.Delete((INode)stagedItem);

                //if (delRes.Code != 200)
                //    return delRes;
                using (var context = new GreenWerxDbContext(Globals.DBConnectionKey))
                {
                    context.Delete <StageData>(stagedItem);

                    var stagedAttributes = context.GetAll <StageData>().Where(w => w.DataType.EqualsIgnoreCase("attribute"));

                    foreach (var stageAttribute in stagedAttributes)
                    {
                        var attribute = JsonConvert.DeserializeObject <TMG.Attribute>(stageAttribute.StageResults);
                        if (attribute.ReferenceUUID == uuid)
                        {
                            if (context.Delete <TMG.Attribute>(attribute) == false)
                            {
                                stageAttribute.Result = "delete failed";
                                context.Update <StageData>(stageAttribute);
                            }
                        }
                    }
                }

                return(ServiceResponse.OK());
            }
            catch (Exception ex)
            {
                error = ex.DeserializeException();
            }
            return(ServiceResponse.Error(error));
        }