示例#1
0
        public ServiceResult Update(INode n, bool authorize = true)
        {
            if (n == null)
            {
                return(ServiceResponse.Error("No record was sent."));
            }

            var u = (User)n;

            if (authorize)
            {
                if (!this.DataAccessAuthorized(n, "PATCH", false))
                {
                    return(ServiceResponse.Error("You are not authorized this action."));
                }
            }

            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                if (context.Update <User>(u) > 0)
                {
                    return(ServiceResponse.OK());
                }
            }

            return(ServiceResponse.Error("No records were updated."));
        }
示例#2
0
        public ServiceResult Update(INode n)
        {
            if (!this.DataAccessAuthorized(n, "PATCH", false))
            {
                return(ServiceResponse.Error("You are not authorized this action."));
            }

            var p = (InventoryItem)n;

            if (p.ItemDate == DateTime.MinValue)
            {
                p.ItemDate = DateTime.UtcNow;
            }

            if (p.DateCreated == DateTime.MinValue)
            {
                p.DateCreated = DateTime.UtcNow;
            }


            ServiceResult res = ServiceResponse.OK();

            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                if (context.Update <InventoryItem>(p) == 0)
                {
                    return(ServiceResponse.Error(p.Name + " failed to update. "));
                }
            }
            return(res);
        }
示例#3
0
        public ServiceResult Delete(INode n, bool purge = false)
        {
            if (n == null)
            {
                return(ServiceResponse.Error("No record sent."));
            }

            if (!this.DataAccessAuthorized(n, "delete", false))
            {
                return(ServiceResponse.Error("You are not authorized this action."));
            }

            var s = (StatusMessage)n;

            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                if (purge)
                {
                    if (context.Delete <StatusMessage>(s) == 0)
                    {
                        return(ServiceResponse.Error(s.Name + " failed to delete. "));
                    }
                }
                else
                {
                    s.Deleted = false;
                    if (context.Update <StatusMessage>(s) == 0)
                    {
                        return(ServiceResponse.Error(s.Name + " failed to delete. "));
                    }
                }
            }
            return(ServiceResponse.OK());
        }
示例#4
0
        public ServiceResult Update(INode n)
        {
            if (n == null)
            {
                return(ServiceResponse.Error("No record sent."));
            }

            var p = (Vendor)n;

            if (!this.DataAccessAuthorized(p, "PATCH", false))
            {
                return(ServiceResponse.Error("You are not authorized this action."));
            }

            ServiceResult res = ServiceResponse.OK();

            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                if (context.Update <Vendor>(p) == 0)
                {
                    return(ServiceResponse.Error(p.Name + " failed to update. "));
                }
            }
            return(res);
        }
示例#5
0
        public int Delete(AnatomyTag s, bool purge = false)
        {
            if (s == null)
            {
                return(0);
            }

            if (!this.DataAccessAuthorized(s, "DELETE", false))
            {
                return(0);
            }

            List <AnatomyTag> pms = new List <AnatomyTag>();

            if (purge)
            {
                using (var context = new TreeMonDbContext(this._connectionKey))
                {
                    return(context.Delete <AnatomyTag>(s));
                }
            }

            //get the AnatomyTag from the table with all the data so when its updated it still contains the same data.
            s = this.GetAnatomyTagBy(s.UUID);
            if (s == null)
            {
                return(0);
            }
            s.Deleted = true;
            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                return(context.Update <AnatomyTag>(s));
            }
        }
示例#6
0
        /// <summary>
        /// 1. remove record in usersinaccounts AccountMember where AccountUUID and user id match
        /// 2. check user record and make sure the account id doesn't match. If it does erase the field.
        /// TBD may need other proccessing
        /// </summary>
        /// <param name="accountUUID"></param>
        /// <param name="userUUID"></param>
        /// <returns></returns>
        public ServiceResult RemoveUserFromAccount(string accountUUID, string userUUID)
        {
            if (string.IsNullOrWhiteSpace(accountUUID))
            {
                return(ServiceResponse.Error("Invalid account id"));
            }

            if (string.IsNullOrWhiteSpace(userUUID))
            {
                return(ServiceResponse.Error("Invalid user id"));
            }

            if (!IsUserInAccount(accountUUID, userUUID))
            {
                return(ServiceResponse.OK());
            }

            //if (!this.DataAccessAuthorized(a, "PATCH", false)) return ServiceResponse.Error("You are not authorized this action.");

            User u = null;

            try
            {
                using (var context = new TreeMonDbContext(this._connectionKey))
                {
                    u = context.GetAll <User>().FirstOrDefault(w => w.UUID == userUUID);
                    if (u == null)
                    {
                        return(ServiceResponse.Error("Invalid user id"));
                    }

                    DynamicParameters parameters = new DynamicParameters();
                    parameters.Add("@AccountUUID", accountUUID);
                    parameters.Add("@MEMBERID", userUUID);

                    int res = context.Delete <AccountMember>("WHERE AccountUUID=@AccountUUID AND  MemberUUID=@MEMBERID", parameters);
                    //Remove the reference in the xref table
                    //SQLITE SYNTAX
                    //    object[] parameters = new object[] { accountUUID, userUUID };
                    // int res =  context.Delete<AccountMember>("WHERE AccountUUID=? AND  MemberUUID=?", parameters);

                    if (u.AccountUUID == accountUUID)
                    {
                        u.AccountUUID = "";
                        if (context.Update <User>(u) == 0)
                        {
                            return(ServiceResponse.Error("Failed to remove user " + u.Name));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.InsertError(ex.Message, "AccountManager", "RemoveUserFromAccount:accountUUID:" + accountUUID + " userUUID" + userUUID);
                Debug.Assert(false, ex.Message);
                return(ServiceResponse.Error(ex.Message));
            }
            return(ServiceResponse.OK(string.Format("User {0} removed from account.", u.Name)));
        }
示例#7
0
        public ServiceResult Update(dynamic p)
        {
            if (!this.DataAccessAuthorized(p, "PATCH", false))
            {
                return(ServiceResponse.Error("You are not authorized this action."));
            }

            ServiceResult res           = ServiceResponse.OK();
            int           recordUpdated = 0;

            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                string type = p.UUIDType.ToString().ToUpper();
                switch (type)
                {
                case "PLANT":
                    recordUpdated = context.Update <TreeMon.Models.Plant.Plant>(p);
                    break;

                case "BALLAST":
                    recordUpdated = context.Update <Ballast>(p);
                    break;

                case "BULB":
                    recordUpdated = context.Update <Bulb>(p);
                    break;

                case "CUSTOM":
                    recordUpdated = context.Update <Custom>(p);
                    break;

                case "FAN":
                    recordUpdated = context.Update <Fan>(p);
                    break;

                case "FILTER":
                    recordUpdated = context.Update <Filter>(p);
                    break;

                case "PUMP":
                    recordUpdated = context.Update <Pump>(p);
                    break;

                case "VEHICLE":
                    recordUpdated = context.Update <Vehicle>(p);
                    break;
                }
            }
            if (recordUpdated == 0)
            {
                return(ServiceResponse.Error(p.Name + " failed to update. "));
            }

            return(res);
        }
示例#8
0
        public void TreeMonDbContext_Context_Update()
        {
            TreeMonDbContext context = new TreeMonDbContext("MSSQL_TEST");

            _user      = context.GetAll <User>().FirstOrDefault();
            _user.Name = "UPDATE_TEST";
            Assert.AreEqual(context.Update <User>(_user), 1);
            Assert.AreEqual(context.Get <User>(_user.Id).Name, "UPDATE_TEST");
        }
示例#9
0
        public ServiceResult Delete(INode n, bool purge = false)
        {
            if (n == null)
            {
                return(ServiceResponse.Error("No record sent."));
            }

            if (!this.DataAccessAuthorized(n, "DELETE", false))
            {
                return(ServiceResponse.Error("You are not authorized this action."));
            }

            var p = (Vendor)n;

            try
            {
                using (var context = new TreeMonDbContext(this._connectionKey))
                {
                    if (purge)
                    {
                        DynamicParameters parameters = new DynamicParameters();
                        parameters.Add("@VendorUUID", p.UUID);
                        if (context.Delete <Vendor>("WHERE UUID=@VendorUUID", parameters) > 0)
                        {
                            DynamicParameters credParams = new DynamicParameters();
                            credParams.Add("@UUID", p.UUID);
                            credParams.Add("@TYPE", "Vendor");
                            context.Delete <Credential>("WHERE RecipientUUID=@UUID AND RecipientType=@TYPE", credParams);

                            return(ServiceResponse.OK());
                        }
                    }
                    else
                    {
                        p.Deleted = true;
                        if (context.Update <Vendor>(p) > 0)
                        {
                            return(ServiceResponse.OK());
                        }
                    }
                }


                return(ServiceResponse.Error("No records deleted."));
                //SQLITE
                //this was the only way I could get it to delete a RolePermission without some stupid EF error.
                //object[] paramters = new object[] { rp.PermissionUUID , rp.RoleUUID ,rp.AccountUUID };
                //context.Delete<RolePermission>("WHERE PermissionUUID=? AND RoleUUID=? AND AccountUUID=?", paramters);
                //  context.Delete<RolePermission>(rp);
            }
            catch (Exception ex)
            {
                _logger.InsertError(ex.Message, "VendorManager", "DeleteVendor:" + p.UUID);
                Debug.Assert(false, ex.Message);
                return(ServiceResponse.Error("Exception occured while deleting this record."));
            }
        }
示例#10
0
 public bool Update(UserSession us)
 {
     if (us == null)
     {
         return(false);
     }
     using (var context = new TreeMonDbContext(_connectionKey))
     {
         return(context.Update <UserSession>(us) > 0 ? true : false);
     }
 }
示例#11
0
        public ServiceResult Delete(INode n, bool purge = false)
        {
            ServiceResult res = ServiceResponse.OK();

            if (n == null)
            {
                return(ServiceResponse.Error("No record sent."));
            }


            if (!this.DataAccessAuthorized(n, "DELETE", false))
            {
                return(ServiceResponse.Error("You are not authorized this action."));
            }

            var p = (Product)n;

            try
            {
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@PRODUCTUUID", p.UUID);
                using (var context = new TreeMonDbContext(this._connectionKey))
                {
                    if (purge)
                    {
                        if (context.Delete <Product>("WHERE UUID=@PRODUCTUUID", parameters) == 0)
                        {
                            return(ServiceResponse.Error(p.Name + " failed to delete. "));
                        }
                    }
                    else
                    {
                        p.Deleted = true;
                        if (context.Update <Product>(p) == 0)
                        {
                            return(ServiceResponse.Error(p.Name + " failed to delete. "));
                        }
                    }
                }
                //SQLITE
                //this was the only way I could get it to delete a RolePermission without some stupid EF error.
                //object[] paramters = new object[] { rp.PermissionUUID , rp.RoleUUID ,rp.AccountUUID };
                //context.Delete<RolePermission>("WHERE PermissionUUID=? AND RoleUUID=? AND AccountUUID=?", paramters);
                //  context.Delete<RolePermission>(rp);
            }
            catch (Exception ex)
            {
                _logger.InsertError(ex.Message, "ProductManager", "DeleteProduct");
                Debug.Assert(false, ex.Message);
                return(ServiceResponse.Error("Exception occured while deleting this record."));
            }

            return(res);
        }
示例#12
0
        public ServiceResult DeleteCartItem(string cartItemUUID)
        {
            InventoryManager inventoryManager = new InventoryManager(this._connectionKey, _sessionKey);
            ShoppingCartItem cartItem         = this.GetCartItem(cartItemUUID);

            if (cartItem == null)
            {
                return(ServiceResponse.Error("Item not found in cart."));
            }

            InventoryItem item = (InventoryItem)inventoryManager.GetBy(cartItem.ItemUUID);

            if (item == null)
            {
                return(ServiceResponse.Error("Product wasn't found."));
            }

            if (!item.Virtual)
            {
                item.Quantity += cartItem.Quantity; //add the item back to inventory.
            }

            User u = this.GetUser(_sessionKey);

            using (var transactionScope = new TransactionScope())
                using (var dbContext = new TreeMonDbContext(this._connectionKey))
                {
                    try
                    {
                        if (dbContext.Delete <ShoppingCartItem>(cartItem) <= 0)
                        {
                            _logger.InsertError("Failed to delete shopping cart item. " + cartItem.UUID, "StoreManager", MethodInfo.GetCurrentMethod().Name);
                            return(ServiceResponse.Error("Failed to delete shopping cart."));
                        }

                        if (dbContext.Update <InventoryItem>(item) <= 0)
                        {
                            _logger.InsertError("Failed to update inventory. " + item.UUID, "StoreManager", MethodInfo.GetCurrentMethod().Name);
                            return(ServiceResponse.Error("Failed to update inventory."));
                        }
                        transactionScope.Complete();
                        //  remainingStock = item.Quantity;
                    }
                    catch (Exception ex)
                    {
                        _logger.InsertError(ex.Message, "StoreManager", MethodInfo.GetCurrentMethod().Name);
                        return(ServiceResponse.Error("Failed to delete item."));
                    }
                }
            return(ServiceResponse.OK());
        }
示例#13
0
        /// <summary>
        /// Note: this is used in the admin section and only requires
        /// the product uuid and the new category assigned to the category property.
        /// </summary>
        /// <param name="products"></param>
        /// <returns></returns>
        public ServiceResult Update(List <Product> products)
        {
            ServiceResult res = ServiceResponse.OK("Products moved to new categories.");

            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                foreach (Product p in products)
                {
                    string newCat = p.CategoryUUID;
                    //Get the product from the database because when we update the framework will update all fields, and the products
                    //passed in may not be complete.
                    //
                    var dbP = context.GetAll <Product>().FirstOrDefault(pw => pw.UUID == p.UUID);
                    if (dbP == null)
                    {
                        if (res.Code == 200)
                        {
                            res = ServiceResponse.Error(p.UUID + " product not found.<br/>");
                        }
                        else
                        {
                            res.Message += p.UUID + " product not found.<br/>";
                        }

                        continue;
                    }

                    if (!this.DataAccessAuthorized(dbP, "PATCH", false))
                    {
                        return(ServiceResponse.Error("You are not authorized this action."));
                    }

                    dbP.CategoryUUID = newCat;

                    if (context.Update <Product>(dbP) == 0)
                    {
                        if (res.Code == 200)
                        {
                            res = ServiceResponse.Error(dbP.Name + " failed to update. " + dbP.UUID + "<br/>");
                        }
                        else
                        {
                            res.Message += dbP.Name + " failed to update. " + dbP.UUID + "<br/>";
                        }
                    }
                }
            }

            return(res);
        }
示例#14
0
        public ServiceResult Delete(INode n, bool purge = false)
        {
            if (n == null)
            {
                return(ServiceResponse.Error("Invalid Order data."));
            }


            if (!this.DataAccessAuthorized(n, "DELETE", false))
            {
                return(ServiceResponse.Error("You are not authorized this action."));
            }

            var s = (Order)n;

            List <Order> pms = new List <Order>();

            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                if (purge)
                {
                    if (context.Delete <Order>(s) > 0)
                    {
                        return(ServiceResponse.OK());
                    }
                    else
                    {
                        return(ServiceResponse.Error("Failed to delete the order."));
                    }
                }

                //get the Order from the table with all the data so when its updated it still contains the same data.
                s = (Order)this.GetBy(s.UUID);
                if (s == null)
                {
                    return(ServiceResponse.Error("Could not find order."));
                }

                s.Deleted = true;
                if (context.Update <Order>(s) > 0)
                {
                    return(ServiceResponse.OK());
                }
                else
                {
                    return(ServiceResponse.Error("Failed to delete the order."));
                }
            }
        }
示例#15
0
        public ServiceResult Update(PaymentGatewayLog p)
        {
            // if (!this.DataAccessAuthorized(p, "PATCH", false)) return ServiceResponse.Error("You are not authorized this action.");

            ServiceResult res = ServiceResponse.OK();

            using (var context = new TreeMonDbContext(_dbConnectionKey))
            {
                if (context.Update <PaymentGatewayLog>(p) == 0)
                {
                    return(ServiceResponse.Error(p.Gateway + " failed to update. "));
                }
            }
            return(res);
        }
示例#16
0
        ///This removes the user from all accounts
        public ServiceResult RemoveUserFromAllAccounts(string userUUID)
        {
            ServiceResult res = ServiceResponse.OK();

            User u = null;

            try
            {
                using (var context = new TreeMonDbContext(this._connectionKey))
                {
                    //Make sure correct userid is passed in.
                    u = context.GetAll <User>().FirstOrDefault(w => w.UUID == userUUID);

                    if (u == null)
                    {
                        return(ServiceResponse.Error("Invalid user id."));
                    }

                    if (!this.DataAccessAuthorized(u, "PATCH", false))
                    {
                        return(ServiceResponse.Error("You are not authorized this action."));
                    }

                    DynamicParameters parameters = new DynamicParameters();
                    parameters.Add("@MEMBERID", userUUID);
                    context.Delete <AccountMember>("WHERE MemberUUID=@MEMBERID", parameters);

                    //SQLITE
                    //Remove the reference in the xref table
                    //object[] parameters = new object[] { userUUID };
                    //context.Delete<AccountMember>("WHERE MemberUUID=?", parameters);

                    //now make sure the primary account in the user table is emptied.
                    u.AccountUUID = "";
                    if (context.Update <User>(u) == 0)
                    {
                        return(ServiceResponse.Error(u.Name + " failed to update. "));
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.InsertError(ex.Message, "AccountManager", "RemoveUserFromAllAccounts:userUUID:" + userUUID);
                Debug.Assert(false, ex.Message);
                return(ServiceResponse.Error("Exception occured while deleting this record."));
            }
            return(res);
        }
示例#17
0
        public ServiceResult Update(INode n)
        {
            if (n == null)
            {
                return(ServiceResponse.Error("Invalid EmailLog data."));
            }
            var s = (EmailLog)n;

            using (var context = new TreeMonDbContext(_dbConnectionKey))
            {
                if (context.Update <EmailLog>(s) > 0)
                {
                    return(ServiceResponse.OK());
                }
            }
            return(ServiceResponse.Error("System error, EmailLog was not updated."));
        }
示例#18
0
        public ServiceResult Delete(INode n, bool purge = false)
        {
            ServiceResult res = ServiceResponse.OK();

            if (n == null)
            {
                return(ServiceResponse.Error("No record sent."));
            }

            if (!this.DataAccessAuthorized(n, "DELETE", false))
            {
                return(ServiceResponse.Error("You are not authorized this action."));
            }

            var s = (Category)n;

            List <Category> pms = new List <Category>();

            if (purge)
            {
                using (var context = new TreeMonDbContext(this._connectionKey))
                {
                    if (context.Delete <Category>(s) == 0)
                    {
                        return(ServiceResponse.Error(s.Name + " failed to delete. "));
                    }
                }
            }

            //get the Category from the table with all the data so when its updated it still contains the same data.
            s = (Category)this.GetBy(s.UUID);
            if (s == null)
            {
                return(ServiceResponse.Error("Symptom not found"));
            }
            s.Deleted = true;
            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                if (context.Update <Category>(s) == 0)
                {
                    return(ServiceResponse.Error(s.Name + " failed to delete. "));
                }
            }
            return(res);
        }
示例#19
0
        /// <summary>
        /// Method to validate token against expiry and existence in database.
        /// </summary>
        /// <param name="tokenId"></param>
        /// <returns></returns>
        public bool IsValidSession(string authToken)
        {
            if (string.IsNullOrEmpty(authToken))
            {
                return(false);
            }

            UserSession us = null;

            using (var context = new TreeMonDbContext(_connectionKey))
            {
                try
                {
                    us = context.GetAll <UserSession>().OrderByDescending(ob => ob.Issued).FirstOrDefault(w => w.AuthToken == authToken);
                }
                catch (Exception ex)
                {
                    _logger.InsertError(ex.Message, "SessionManager", "IsValidSession");
                    Debug.Assert(false, ex.Message);
                    return(false);
                }

                if (us == null)
                {
                    return(false);
                }

                double secondsLeft = us.Expires.Subtract(DateTime.UtcNow).TotalSeconds;

                if (us.IsPersistent == false && secondsLeft <= 0)
                {
                    context.Delete <UserSession>(us);
                    return(false);
                }

                if (us.SessionLength != SessionLength)
                {
                    us.SessionLength = this.SessionLength;
                }
                //This adds time to the session so it won't cut them off while they're working (non idle user).
                us.Expires = DateTime.UtcNow.AddMinutes(us.SessionLength);
                context.Update <UserSession>(us);
            }
            return(true);
        }
示例#20
0
        public ServiceResult Update(INode n)
        {
            if (!this.DataAccessAuthorized(n, "PATCH", false))
            {
                return(ServiceResponse.Error("You are not authorized this action."));
            }
            var           p   = (PriceRule)n;
            ServiceResult res = ServiceResponse.OK();

            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                if (context.Update <PriceRule>(p) == 0)
                {
                    return(ServiceResponse.Error(p.Name + " failed to update. "));
                }
            }
            return(res);
        }
示例#21
0
        public ServiceResult Update(INode n)
        {
            if (n == null)
            {
                return(ServiceResponse.Error("Invalid UnitOfMeasure data."));
            }

            var s = (UnitOfMeasure)n;

            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                if (context.Update <UnitOfMeasure>(s) > 0)
                {
                    return(ServiceResponse.OK());
                }
            }
            return(ServiceResponse.Error("System error, UnitOfMeasure was not updated."));
        }
示例#22
0
 public ServiceResult UpdateCart(ShoppingCart cart)
 {
     using (var context = new TreeMonDbContext(this._connectionKey))
     {
         try
         {
             if (context.Update <ShoppingCart>(cart) > 0)
             {
                 return(ServiceResponse.OK());
             }
         }
         catch (Exception ex)
         {
             _logger.InsertError(ex.Message, "StoreManager", MethodInfo.GetCurrentMethod().Name);
         }
     }
     return(ServiceResponse.Error("Failed to update the cart."));
 }
示例#23
0
        public ServiceResult Delete(INode n, bool purge = false)
        {
            ServiceResult res = ServiceResponse.OK();

            if (n == null)
            {
                return(ServiceResponse.Error("No record sent."));
            }

            var s = (EmailLog)n;

            List <EmailLog> pms = new List <EmailLog>();

            if (purge)
            {
                using (var context = new TreeMonDbContext(_dbConnectionKey))
                {
                    if (context.Delete <EmailLog>(s) == 0)
                    {
                        return(ServiceResponse.Error(s.Name + " failed to delete. "));
                    }
                }
            }

            //get the EmailLog from the table with all the data so when its updated it still contains the same data.
            s = (EmailLog)this.GetBy(s.UUID);
            if (s == null)
            {
                return(ServiceResponse.Error("Email log not found"));
            }

            s.Deleted = true;
            using (var context = new TreeMonDbContext(_dbConnectionKey))
            {
                if (context.Update <EmailLog>(s) == 0)
                {
                    return(ServiceResponse.Error(s.Name + " failed to delete. "));
                }
            }
            return(res);
        }
示例#24
0
        public ServiceResult Update(SymptomLog s)
        {
            if (s == null)
            {
                return(ServiceResponse.Error("Invalid SymptomLog data."));
            }

            if (!this.DataAccessAuthorized(s, "PATCH", false))
            {
                return(ServiceResponse.Error("You are not authorized this action."));
            }

            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                if (context.Update <SymptomLog>(s) > 0)
                {
                    return(ServiceResponse.OK());
                }
            }
            return(ServiceResponse.Error("System error, SymptomLog was not updated."));
        }
示例#25
0
        /// <summary>
        /// This sets the AccountUUID in the Users table. This is the default or active account
        /// the user is accociated with and all data will be loaded according to this field.
        /// </summary>
        /// <param name="accountUUID"></param>
        /// <param name="userUUID"></param>
        /// <returns></returns>
        public ServiceResult SetActiveAccount(string accountUUID, string userUUID, User requestingUser)
        {
            if (string.IsNullOrWhiteSpace(userUUID))
            {
                return(ServiceResponse.Error("Invalid user id"));
            }

            if (!IsUserInAccount(accountUUID, userUUID))
            {
                return(ServiceResponse.Error("User must be added to the account before setting it as the active account."));
            }

            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                User u = context.GetAll <User>().FirstOrDefault(w => w.UUID == userUUID);
                if (u == null)
                {
                    return(ServiceResponse.Error("Invalid user id."));
                }

                if (u.AccountUUID == accountUUID)
                {
                    return(ServiceResponse.OK("This account is already the default for this user."));
                }

                if (!this.DataAccessAuthorized(u, "PATCH", false))
                {
                    return(ServiceResponse.Error("You are not authorized this action."));
                }

                u.AccountUUID = accountUUID;

                if (context.Update <User>(u) > 0)
                {
                    return(ServiceResponse.OK("This account is now the default for this user."));
                }
            }
            return(ServiceResponse.Error("Error occurred while updating the user."));
        }
示例#26
0
        public ServiceResult Delete(INode s, bool purge = false)
        {
            ServiceResult res = ServiceResponse.OK();

            if (s == null)
            {
                return(ServiceResponse.Error("No record sent."));
            }

            if (!this.DataAccessAuthorized(s, "DELETE", false))
            {
                return(ServiceResponse.Error("You are not authorized this action."));
            }

            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                if (purge)
                {
                    if (context.Delete <Strain>((Strain)s) == 0)
                    {
                        return(ServiceResponse.Error(s.Name + " failed to delete. "));
                    }
                }

                //get the strain from the table with all the data so when its updated it still contains the same data.
                s = this.GetBy(s.UUID);
                if (s == null)
                {
                    ServiceResponse.Error("Strain not found.");
                }
                s.Deleted = true;
                if (context.Update <Strain>((Strain)s) == 0)
                {
                    return(ServiceResponse.Error(s.Name + " failed to delete. "));
                }
            }
            return(res);
        }
示例#27
0
        public ServiceResult Update(INode n)
        {
            if (n == null)
            {
                return(ServiceResponse.Error("Invalid Dose data."));
            }

            if (!this.DataAccessAuthorized(n, "PATCH", false))
            {
                return(ServiceResponse.Error("You are not authorized this action."));
            }

            var s = (DoseLog)n;

            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                if (context.Update <DoseLog>(s) > 0)
                {
                    return(ServiceResponse.OK());
                }
            }
            return(ServiceResponse.Error("System error, Dose was not updated."));
        }
示例#28
0
        public ServiceResult Delete(INode n, bool purge = false)
        {
            if (n == null)
            {
                return(ServiceResponse.Error("No record sent."));
            }

            var s = (UnitOfMeasure)n;

            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                if (purge)
                {
                    if (context.Delete <UnitOfMeasure>(s) > 0)
                    {
                        return(ServiceResponse.OK());
                    }

                    return(ServiceResponse.Error("Failed to purge record."));
                }

                //get the UnitOfMeasure from the table with all the data so when its updated it still contains the same data.
                s = (UnitOfMeasure)this.GetBy(s.UUID);
                if (s == null)
                {
                    return(ServiceResponse.Error("Measure not found."));
                }

                s.Deleted = true;
                if (context.Update <UnitOfMeasure>(s) > 0)
                {
                    return(ServiceResponse.OK());
                }

                return(ServiceResponse.Error("Failed to delete record."));
            }
        }
示例#29
0
        public ServiceResult Update(INode n)
        {
            if (n == null)
            {
                return(ServiceResponse.Error("Invalid Order data."));
            }

            var s = (Order)n;

            if (!this.DataAccessAuthorized(s, "UPDATE", false))
            {
                return(ServiceResponse.Error("You are not authorized this action."));
            }


            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                if (context.Update <Order>(s) > 0)
                {
                    return(ServiceResponse.OK());
                }
            }
            return(ServiceResponse.Error("System error, Order was not updated."));
        }
示例#30
0
        public ServiceResult AddCartItem(string cartUUID, string cartItemUUID, float quantity)
        {
            InventoryManager inventoryManager = new InventoryManager(this._connectionKey, _sessionKey);
            ShoppingCartItem cartItem         = this.GetCartItem(cartItemUUID);

            if (cartItem == null)
            {
                return(ServiceResponse.Error("Item not found in cart."));
            }

            InventoryItem item = (InventoryItem)inventoryManager.GetBy(cartItem.ItemUUID);

            if (item == null)
            {
                return(ServiceResponse.Error("Product wasn't found."));
            }



            int   itemsInCart    = 0;
            float remainingStock = 0;

            if (!item.Virtual)
            {
                if (item.Quantity <= 0)
                {
                    return(ServiceResponse.Error("The product " + item.Name + " is sold out."));
                }
                //If the amount requested is greater than whats in stock then set the ammount ordered to the amount on hand.
                if (item.Quantity < quantity)
                {
                    quantity = item.Quantity;
                }

                cartItem.Quantity += quantity; //add to cart quantity
                item.Quantity     -= quantity; //remove from inventory
            }

            User   u          = this.GetUser(_sessionKey);
            string trackingID = u == null ? _sessionKey : u.UUID;

            using (var transactionScope = new TransactionScope())
                using (var dbContext = new TreeMonDbContext(this._connectionKey))
                {
                    try
                    {
                        if (cartItem.Quantity <= 0 && dbContext.Delete <ShoppingCartItem>(cartItem) <= 0)
                        {
                            _logger.InsertError("Failed to delete shopping cart item. " + cartItem.UUID, "StoreManager", MethodInfo.GetCurrentMethod().Name);
                            return(ServiceResponse.Error("Failed to update shopping cart."));
                        }
                        else
                        {
                            int changeCount = dbContext.Update <ShoppingCartItem>(cartItem);
                            // if (dbContext.Update<ShoppingCartItem>(cartItem) <= 0)
                            //{
                            //    _logger.InsertError("Failed to update shopping cart. " + cartItem.UUID, "StoreManager", MethodInfo.GetCurrentMethod().Name);
                            //    return ServiceResponse.Error("Failed to update shopping cart.");
                            //}
                        }

                        if (dbContext.Update <InventoryItem>(item) <= 0)
                        {
                            _logger.InsertError("Failed to update inventory. " + item.UUID, "StoreManager", MethodInfo.GetCurrentMethod().Name);
                            return(ServiceResponse.Error("Failed to update inventory."));
                        }

                        transactionScope.Complete();
                        remainingStock = item.Quantity;
                    }
                    catch (Exception ex) {
                        _logger.InsertError(ex.Message, "StoreManager", MethodInfo.GetCurrentMethod().Name);
                    }
                    itemsInCart = dbContext.GetAll <ShoppingCartItem>().Where(w => w.UserUUID == trackingID).Count();
                }
            return(ServiceResponse.OK("", "{ \"CartUUID\" :\"" + trackingID + "\",    \"CartItemUUID\" :\"" + cartItem.UUID + "\", \"RemainingStock\" : \"" + remainingStock + "\", \"IsVirtual\" : \"" + item.Virtual + "\" , \"ItemsInCart\" : \"" + itemsInCart + "\" }"));
        }