Esempio n. 1
0
        public List <User> GetAccountMembers(string accountUUID, bool clearSensitiveData = true)
        {
            List <User> accountMembers;

            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                accountMembers = context.GetAll <AccountMember>().Where(w => w.AccountUUID == accountUUID)
                                 .Join(
                    context.GetAll <User>()
                    .Where(w => w.Deleted == false),
                    acct => acct.MemberUUID,
                    users => users.UUID,
                    (acct, users) => new { acct, users }
                    )
                                 .Select(s => s.users)
                                 .ToList();
            }
            if (accountMembers == null)
            {
                return(new List <User>());
            }

            if (clearSensitiveData)
            {
                accountMembers = new UserManager(this._connectionKey, SessionKey).ClearSensitiveData(accountMembers);
            }

            //if (!this.DataAccessAuthorized(u, "GET", false)) return ServiceResponse.Error("You are not authorized this action.");
            return(accountMembers);
        }
Esempio n. 2
0
        public void Api_UnitOfMeasureController_UpdateUnitOfMeasure()
        {
            TreeMonDbContext context = new TreeMonDbContext(connectionKey);
            UnitOfMeasure    mdl     = new UnitOfMeasure();

            mdl.AccountUUID = SystemFlag.Default.Account;
            mdl.Name        = Guid.NewGuid().ToString("N");
            mdl.UUID        = Guid.NewGuid().ToString("N");
            mdl.DateCreated = DateTime.UtcNow;

            Assert.IsTrue(context.Insert <UnitOfMeasure>(mdl));

            mdl = context.GetAll <UnitOfMeasure>().Where(w => w.Name == mdl.Name).FirstOrDefault();
            UnitOfMeasure pv = new UnitOfMeasure();

            pv.Id          = mdl.Id;
            pv.UUID        = mdl.UUID;
            pv.AccountUUID = mdl.AccountUUID;
            pv.Name        = mdl.Name;
            //~~~ Updatable fields ~~~

            string postData = JsonConvert.SerializeObject(pv);

            Task.Run(async() =>
            {
                ServiceResult res = await TestHelper.SentHttpRequest("POST", "api/UnitsOfMeasure/Update", postData, _ownerAuthToken);
                Assert.IsNotNull(res);
                Assert.AreEqual(res.Code, 200);

                UnitOfMeasure dbUnitOfMeasure = context.GetAll <UnitOfMeasure>().Where(w => w.Name == mdl.Name).FirstOrDefault();
                Assert.IsNotNull(dbUnitOfMeasure);
            }).GetAwaiter().GetResult();
        }
Esempio n. 3
0
        public List <InventoryItem> GetItems(string accountUUID, bool deleted = false)
        {
            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                //if (!this.DataAccessAuthorized(dbP, "GET", false)) return ServiceResponse.Error("You are not authorized this action.");

                List <InventoryItem> items = context.GetAll <InventoryItem>()
                                             .Where(sw => (sw.AccountUUID == accountUUID) && sw.Deleted == deleted).OrderBy(ob => ob.Name).ToList();

                items.ForEach(x =>
                {
                    x.WeightUOM = context.GetAll <UnitOfMeasure>().FirstOrDefault(w => w.UUID == x.UOMUUID)?.Name;
                });

                //todo reimplement this after making sure the unit of measure id is set.
                //return context.GetAll<InventoryItem>()
                //                 .Where(sw => (sw.AccountUUID == accountUUID) && sw.Deleted == deleted)
                //                 .Join(context.GetAll<UnitOfMeasure>(),
                //                 ii => ii?.UOMUUID,
                //                 uom => uom?.UUID,
                //                 (ii, uom) =>{
                //                     ii.WeightUOM = uom.Name;
                //                     return ii;
                //                 })
                //                 .OrderBy(ob => ob.Name).ToList();

                return(items);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Gets all users not in the account
        /// </summary>
        /// <param name="accountUUID"></param>
        /// <param name="clearSensitiveData"></param>
        /// <returns></returns>
        public List <User> GetAccountNonMembers(string accountUUID, bool clearSensitiveData = true)
        {
            List <User> nonMembers;

            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                //this query is from a bug, just kludge it in for now. backlog fix this damn mess.
                nonMembers = context.GetAll <User>().Where(w => w.Deleted == false && string.IsNullOrWhiteSpace(w.AccountUUID) == true).ToList();

                nonMembers.AddRange(context.GetAll <AccountMember>().Where(w => w.AccountUUID != accountUUID)
                                    .Join(
                                        context.GetAll <User>()
                                        .Where(w => w.Deleted == false),
                                        acct => acct.MemberUUID,
                                        users => users.UUID,
                                        (acct, users) => new { acct, users }
                                        )
                                    .Select(s => s.users)
                                    .ToList());
            }
            List <User> members = GetAccountMembers(accountUUID, clearSensitiveData);

            if (members != null)
            {
                nonMembers = nonMembers.Except(members).ToList();
            }

            //if (!this.DataAccessAuthorized(u, "GET", false)) return ServiceResponse.Error("You are not authorized this action.");
            return(nonMembers);
        }
Esempio n. 5
0
        public UserSession GetSession(string authToken, bool validate = true)
        {
            try {
                using (var context = new TreeMonDbContext(_connectionKey))
                {
                    if (!validate)
                    {
                        return(context.GetAll <UserSession>().OrderByDescending(ob => ob.Issued).FirstOrDefault(w => w.AuthToken == authToken));
                    }

                    if (!IsValidSession(authToken))
                    {
                        return(null);
                    }

                    return(context.GetAll <UserSession>().OrderByDescending(ob => ob.Issued).FirstOrDefault(w => w.AuthToken == authToken));
                }
            }
            catch (Exception ex)
            {
                _logger.InsertError(ex.Message, "SessionManager", "GetSession");
                Debug.Assert(false, ex.Message);
            }
            return(null);
        }
Esempio n. 6
0
        public void Api_RoleController_DeletePermissionsFromRole()
        {
            TreeMonDbContext      context         = new TreeMonDbContext(connectionKey);
            List <RolePermission> rolePermissions = context.GetAll <RolePermission>().Where(w => w.AccountUUID == SystemFlag.Default.Account).ToList();

            if (rolePermissions == null)
            {
                Api_RoleController_AddPermissionsToRole();

                rolePermissions = context.GetAll <RolePermission>().Where(w => w.AccountUUID == SystemFlag.Default.Account).ToList();
            }

            Assert.IsNotNull(rolePermissions);

            string rolUUID = rolePermissions.FirstOrDefault().UUID;

            string postData = JsonConvert.SerializeObject(rolePermissions);

            Task.Run(async() =>
            {
                ServiceResult res = await TestHelper.SentHttpRequest("POST", "api/Roles/" + rolUUID + "/Permissions/Delete", postData, _ownerAuthToken);
                Assert.IsNotNull(res);
                Assert.AreEqual(res.Code, 200);

                RolePermission dbRolePermission = context.GetAll <RolePermission>().FirstOrDefault(w => w.RoleUUID == rolUUID);
                Assert.IsNull(dbRolePermission);
            }).GetAwaiter().GetResult();
        }
Esempio n. 7
0
        public void Api_MODELNAMEController_UpdateMODELNAME()
        {
            TreeMonDbContext context = new TreeMonDbContext(connectionKey);
            MODELNAME        mdl     = new MODELNAME();

            mdl.AccountId = SystemFlag.Default.Account;//todo create SystemFlag.Test.Account
            mdl.Name      = Guid.NewGuid().ToString("N");
            mdl.UUID      = Guid.NewGuid().ToString("N");

            Assert.IsTrue(context.Insert <MODELNAME>(mdl));

            mdl = context.GetAll <MODELNAME>().Where(w => w.Name == mdl.Name).FirstOrDefault();
            MODELNAMEView pv = new MODELNAMEView();

            pv.Id        = mdl.Id;
            pv.UUID      = mdl.UUID;
            pv.AccountId = mdl.AccountId;
            pv.Name      = mdl.Name;
            //~~~ Updatable fields ~~~

            string postData = JsonConvert.SerializeObject(pv);

            Task.Run(async() =>
            {
                ServiceResult res = await TestHelper.SentHttpRequest("POST", "api/MODELNAMEs/Update", postData, _ownerAuthToken);
                Assert.IsNotNull(res);
                Assert.AreEqual(res.Code, 200);

                MODELNAME dbMODELNAME = context.GetAll <MODELNAME>().Where(w => w.Name == mdl.Name).FirstOrDefault();
                Assert.IsNotNull(dbMODELNAME);
            }).GetAwaiter().GetResult();
        }
Esempio n. 8
0
        public ServiceResult Delete(INode n, bool purge = false)
        {
            if (n == null)
            {
                return(ServiceResponse.Error("No account record sent."));
            }

            if (n.UUID == this._requestingUser.AccountUUID) //todo check if any user has this as default account
            {
                return(ServiceResponse.Error("Cannot delete a default account. You must select another account as default before deleting this one."));
            }

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

            var account = (Account)n;

            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                Account a = context.GetAll <Account>().FirstOrDefault(w => w.UUID == account.UUID);

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

                if (!purge)
                {
                    Account dbAcct = context.GetAll <Account>().FirstOrDefault(w => w.UUID == a.UUID);

                    if (dbAcct == null)
                    {
                        return(ServiceResponse.Error("Account not found."));
                    }

                    dbAcct.Deleted = true;

                    return(Update(dbAcct));
                }

                try
                {
                    if (context.Delete <Account>(a) > 0)
                    {
                        return(ServiceResponse.OK());
                    }

                    return(ServiceResponse.Error("No records deleted."));
                }
                catch (Exception ex)
                {
                    _logger.InsertError(ex.Message, "AccountManager", "DeleteAccount:" + account.UUID);
                    Debug.Assert(false, ex.Message);
                    return(ServiceResponse.Error(ex.Message));
                }
            }
        }
Esempio n. 9
0
 public List <dynamic> GetItemsInCart(string shoppingCartUUID)
 {
     if (string.IsNullOrWhiteSpace(shoppingCartUUID))
     {
         return(new List <dynamic>());
     }
     try
     {
         using (var context = new TreeMonDbContext(this._connectionKey))
         {
             var res = context.GetAll <ShoppingCartItem>().Where(w => w.ShoppingCartUUID == shoppingCartUUID)
                       .Join(context.GetAll <InventoryItem>(),
                             cartItem => cartItem.ItemUUID, //cartItem.ItemType
                             invItem => invItem.UUID,       //invItem.ReferenceType
                             (cartItem, invItem) => new { cartItem, invItem })
                       .Select(s => new
             {
                 Name         = s.invItem.Name,
                 Weight       = s.invItem.Weight,
                 WeightUOM    = context.GetAll <UnitOfMeasure>().FirstOrDefault(w => w.UUID == s.invItem.UOMUUID)?.Name,                            // s.invItem.Name,
                 Virtual      = s.invItem.Virtual,
                 Image        = s.invItem.Image,
                 CartItemUUID = s.cartItem.UUID,
                 Quantity     = s.cartItem.Quantity,
                 Price        = s.cartItem.Price,
                 TotalPrice   = s.cartItem.TotalPrice,
                 UserUUID     = s.cartItem.UserUUID,
                 ItemUUID     = s.cartItem.ItemUUID,
                 ItemType     = s.cartItem.ItemType
             }).Cast <dynamic>().ToList();
             //.Join(context.GetAll<UnitOfMeasure>(),
             //     ii => ii.invItem.UOMUUID,
             //     uom => uom.UUID,
             //    (uom, ii) => new { uom, ii })
             //.Select(s => new {
             //    Name = s.uom.invItem.Name,
             //    Weight = s.uom.invItem.Weight,
             //    WeightUOM = s.ii.Name,
             //    Virtual = s.uom.invItem.Virtual,
             //    Image = s.uom.invItem.Image,
             //    CartItemUUID = s.uom.cartItem.UUID,
             //    Quantity = s.uom.cartItem.Quantity,
             //    Price = s.uom.cartItem.Price,
             //    TotalPrice = s.uom.cartItem.TotalPrice,
             //    UserUUID = s.uom.cartItem.UserUUID,
             //    ItemUUID = s.uom.cartItem.ItemUUID,
             //    ItemType = s.uom.cartItem.ItemType
             //}).Cast<dynamic>().ToList();
             return(res);
         }
     }
     catch (Exception ex)
     {
         this._logger.InsertError(ex.Message, "StoreManager", "GetItemsInCart:" + shoppingCartUUID);
     }
     return(new List <dynamic>());
 }
Esempio n. 10
0
        public List <PriceRule> GetPriceRules(string accountUUID, bool deleted = false)
        {
            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                //if (!this.DataAccessAuthorized(dbP, "GET", false)) return ServiceResponse.Error("You are not authorized this action.");
                //tod check if asset class is returned if so delete the line below.
                List <PriceRule> tmp = context.GetAll <PriceRule>().Where(sw => (sw.AccountUUID == accountUUID) && sw.Deleted == deleted).OrderBy(ob => ob.Name).ToList();

                return(context.GetAll <PriceRule>().Where(sw => (sw.AccountUUID == accountUUID) && sw.Deleted == deleted).OrderBy(ob => ob.Name).ToList());
            }
        }
        public FinanceAccount GetSiteLedger(bool mustBeUnused, string accountUUID)
        {
            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                if (mustBeUnused)
                {
                    return(context.GetAll <FinanceAccount>().FirstOrDefault(l => l.SourceUUID == -1 && l.AccountUUID == accountUUID));
                }

                return(context.GetAll <FinanceAccount>().FirstOrDefault(l => l.AccountUUID == accountUUID));
            }
        }
Esempio n. 12
0
        public ServiceResult AddUserToAccount(string accountUUID, string userUUID, User requestingUser)
        {
            if (string.IsNullOrWhiteSpace(accountUUID))
            {
                return(ServiceResponse.Error("Invalid account id"));
            }

            if (string.IsNullOrWhiteSpace(userUUID))
            {
                return(ServiceResponse.Error("Invalid user id"));
            }
            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                Account a = context.GetAll <Account>().FirstOrDefault(w => w.UUID == accountUUID);
                if (a == null)
                {
                    return(ServiceResponse.Error("Account not found."));
                }

                if (!this.DataAccessAuthorized(a, "DELETE", false))
                {
                    return(ServiceResponse.Error("You are not authorized this action."));
                }
                // if (!_roleManager.DataAccessAuthorized(a, requestingUser, "post", false))
                //   return ServiceResponse.Error("Access denied for account " + a.Name );

                User u = context.GetAll <User>().FirstOrDefault(w => w.UUID == userUUID);
                if (u == null)
                {
                    return(ServiceResponse.Error("User not found."));
                }

                if (!_roleManager.DataAccessAuthorized(u, requestingUser, "post", false))
                {
                    return(ServiceResponse.Error("Access denied for user " + u.Name));
                }


                if (IsUserInAccount(accountUUID, userUUID))
                {
                    return(ServiceResponse.OK("User is already a member of the account."));
                }

                if (context.Insert <AccountMember>(new AccountMember()
                {
                    AccountUUID = accountUUID, MemberUUID = userUUID, MemberType = "User"
                }))
                {
                    return(ServiceResponse.OK(string.Format("User {0} added account.", u.Name)));
                }
            }
            return(ServiceResponse.Error("Server error, member was not added to the account."));
        }
Esempio n. 13
0
        public List <SymptomLog> GetSymptomsByDose(string doseUUID, string parentUUID, string accountUUID, bool deleted = false)
        {
            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                if (string.IsNullOrWhiteSpace(parentUUID) || parentUUID == "0")
                {
                    return(context.GetAll <SymptomLog>().Where(sw => sw.DoseUUID == doseUUID && (sw.UUParentID == "" || sw.UUParentID == null) && sw.AccountUUID == accountUUID && sw.Deleted == deleted).OrderBy(ob => ob.Name).ToList());
                }

                return(context.GetAll <SymptomLog>().Where(sw => sw.DoseUUID == doseUUID && sw.UUParentID == parentUUID && sw.AccountUUID == accountUUID && sw.Deleted == deleted).OrderBy(ob => ob.Name).ToList());
            }
            //if (!this.DataAccessAuthorized(s, "POST", false)) return ServiceResponse.Error("You are not authorized this action.");
        }
Esempio n. 14
0
        public INode GetBy(string UUID, bool clearSensitiveData = true)
        {
            // if (!this.DataAccessAuthorized(u, "GET", false)) return ServiceResponse.Error("You are not authorized this action.");
            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                if (clearSensitiveData)
                {
                    return(ClearSensitiveData(context.GetAll <User>().Where(uw => uw.UUID == UUID).FirstOrDefault()));
                }

                return(context.GetAll <User>().FirstOrDefault(uw => uw.UUID == UUID));
            }
        }
Esempio n. 15
0
        public List <Vendor> GetUserVendors(string accountUUID, string userUUID)
        {
            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                //if (!this.DataAccessAuthorized(p, "GET", false))return ServiceResponse.Error("You are not authorized this action.");
                if (string.IsNullOrWhiteSpace(accountUUID))
                {
                    return(context.GetAll <Vendor>().ToList());
                }

                return(context.GetAll <Vendor>().Where(pw => pw.AccountUUID == accountUUID && pw.CreatedBy == userUUID).ToList());
            }
        }
Esempio n. 16
0
        public List <Strain> GetStrains(string accountUUID, bool deleted = false, bool includeSystemAccount = false)
        {
            //if (!this.DataAccessAuthorized(s, "GET", false)) return ServiceResponse.Error("You are not authorized this action.");

            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                if (includeSystemAccount)
                {
                    return(context.GetAll <Strain>().Where(sw => (sw.AccountUUID == accountUUID || sw.AccountUUID == SystemFlag.Default.Account) && sw.Deleted == deleted).GroupBy(x => x.Name).Select(group => group.First()).OrderBy(ob => ob.Name).ToList());
                }
                return(context.GetAll <Strain>().Where(sw => (sw.AccountUUID == accountUUID) && sw.Deleted == deleted).OrderBy(ob => ob.Name).ToList());
            }
        }
Esempio n. 17
0
        //public INode GetBySyncKey(string syncKey)
        //{
        //    if (string.IsNullOrWhiteSpace(syncKey))
        //        return null;
        //    using (var context = new TreeMonDbContext(this._connectionKey))
        //    {
        //        //try to get the record by account.
        //        INode a = context.GetAll<Account>().FirstOrDefault(aw => aw.SyncKey == syncKey && aw.AccountUUID == _requestingUser.AccountUUID);

        //        //if not get the record matching the default account
        //        if(a == null)
        //            a = context.GetAll<Account>().FirstOrDefault(aw => aw.SyncKey == syncKey &&  aw.AccountUUID == SystemFlag.Default.Account );

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

        /// <summary>
        /// Gets all accounts the user is a member of.
        ///     userUUID  xref to  UsersInAccount => Accounts
        /// </summary>
        /// <param name="userUUID"></param>
        /// <returns></returns>
        public List <Account> GetUsersAccounts(string userUUID)
        {
            //AccountMember = UsersInAccount table.
            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                IEnumerable <Account> userAccounts =
                    from am in context.GetAll <AccountMember>().Where(amw => amw.MemberUUID == userUUID)/// && rrw.UUParentIDType == "User")// && rrw.AccountUUID == accountUUID)
                    join accounts in context.GetAll <Account>().Where(uw => uw.Deleted == false) on am.AccountUUID equals accounts.UUID
                    select accounts;

                //if (!this.DataAccessAuthorized(u, "GET", false)) return ServiceResponse.Error("You are not authorized this action.");
                return(userAccounts.ToList());
            }
        }
Esempio n. 18
0
 public List <Product> GetAll(string category = "")
 {
     //if (!this.DataAccessAuthorized(dbP, "GET", false)) return ServiceResponse.Error("You are not authorized this action.");
     using (var context = new TreeMonDbContext(this._connectionKey))
     {
         if (string.IsNullOrWhiteSpace(category))
         {
             return(context.GetAll <Product>()
                    .OrderBy(o => o.Name).ToList());
         }
         return(context.GetAll <Product>().Where(pw => pw.CategoryUUID == category)
                .OrderBy(po => po.Name).ToList());
     }
 }
Esempio n. 19
0
        public List <Currency> GetAccountCurrency(string accountUUID)
        {
            //if (!this.DataAccessAuthorized(dbP, "GET", false)) return ServiceResponse.Error("You are not authorized this action.");

            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                if (string.IsNullOrWhiteSpace(accountUUID))
                {
                    return(context.GetAll <Currency>().ToList());
                }

                return(context.GetAll <Currency>().Where(pw => pw.AccountUUID == accountUUID).ToList());
            }
        }
Esempio n. 20
0
 public List <Category> GetCategories(string accountUUID, bool deleted = false, bool includeSystemAccount = false)
 {
     using (var context = new TreeMonDbContext(this._connectionKey))
     {
         if (includeSystemAccount)
         {
             return(context.GetAll <Category>()
                    .Where(sw => (sw.AccountUUID == accountUUID) && sw.Deleted == deleted).ToList());
             //  .GroupBy(x => x.Name).Select(group => group.First()).OrderBy(ob => ob.Name).ToList();//this was to remove duplicates when getting system.default.acccount
         }
         return(context.GetAll <Category>().Where(sw => (sw.AccountUUID == accountUUID) && sw.Deleted == deleted).OrderBy(ob => ob.Name).ToList());
     }
     //if (!this.DataAccessAuthorized(s, "GET", false)) return ServiceResponse.Error("You are not authorized this action.");
 }
Esempio n. 21
0
        public List <Currency> GetCurrencies(string accountUUID, bool deleted = false, bool inlcudeSystemDefault = false)
        {
            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                //if (!this.DataAccessAuthorized(dbP, "GET", false)) return ServiceResponse.Error("You are not authorized this action.");
                //tod check if asset class is returned if so delete the line below.

                if (inlcudeSystemDefault)
                {
                    return(context.GetAll <Currency>().Where(sw => (sw.AccountUUID == accountUUID || sw.AccountUUID == SystemFlag.Default.Account) && sw.Deleted == deleted).OrderBy(ob => ob.Name).ToList());
                }

                return(context.GetAll <Currency>().Where(sw => (sw.AccountUUID == accountUUID) && sw.Deleted == deleted).OrderBy(ob => ob.Name).ToList());
            }
        }
Esempio n. 22
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="AccountUUID">This is technically accountUUID</param>
        /// <param name="clearSensitiveData"></param>
        /// <returns></returns>
        public List <User> GetUsers(string AccountUUID, bool clearSensitiveData = true)
        {
            // if (!this.DataAccessAuthorized(u, "GET", false)) return ServiceResponse.Error("You are not authorized this action.");
            List <User> usrs = new List <User>();

            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                if (clearSensitiveData)
                {
                    return(ClearSensitiveData(context.GetAll <User>().Where(w => w.AccountUUID == AccountUUID && w.Deleted == false).ToList()));
                }

                return(context.GetAll <User>().Where(w => w.AccountUUID == AccountUUID && w.Deleted == false).ToList());
            }
        }
Esempio n. 23
0
        public ServiceResult Insert(INode n, bool validateFirst = true)
        {
            if (!this.DataAccessAuthorized(n, "POST", false))
            {
                return(ServiceResponse.Error("You are not authorized this action."));
            }

            n.Initialize(this._requestingUser.UUID, this._requestingUser.AccountUUID, this._requestingUser.RoleWeight);

            var s = (DoseLog)n;

            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                if (validateFirst)
                {
                    DoseLog dbU = context.GetAll <DoseLog>().FirstOrDefault(wu => (wu.Name?.EqualsIgnoreCase(s.Name) ?? false) && wu.AccountUUID == s.AccountUUID);

                    if (dbU != null)
                    {
                        return(ServiceResponse.Error("Dose already exists."));
                    }
                }

                if (context.Insert <DoseLog>(s))
                {
                    return(ServiceResponse.OK("", s));
                }
            }
            return(ServiceResponse.Error("An error occurred inserting Dose " + s.Name));
        }
Esempio n. 24
0
 public List <UnitOfMeasure> GetUnitsOfMeasure(string accountUUID, string category, bool deleted = false)
 {
     using (var context = new TreeMonDbContext(this._connectionKey))
     {
         return(context.GetAll <UnitOfMeasure>().Where(sw => (sw.AccountUUID == accountUUID) && (sw.Category?.EqualsIgnoreCase(category) ?? false) && sw.Deleted == deleted).OrderBy(ob => ob.Name).ToList());
     }
 }
Esempio n. 25
0
        public ServiceResult Insert(INode n, bool validateFirst = true)
        {
            if (n == null)
            {
                return(ServiceResponse.Error("Value is empty."));
            }

            n.Initialize(this._requestingUser.UUID, this._requestingUser.AccountUUID, this._requestingUser.RoleWeight);

            var s = (UnitOfMeasure)n;

            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                if (validateFirst)
                {
                    UnitOfMeasure dbU = context.GetAll <UnitOfMeasure>().FirstOrDefault(wu => (wu.Name?.EqualsIgnoreCase(s.Name) ?? false) && wu.AccountUUID == s.AccountUUID);

                    if (dbU != null)
                    {
                        return(ServiceResponse.Error("UnitOfMeasure already exists."));
                    }
                }

                if (context.Insert <UnitOfMeasure>(s))
                {
                    return(ServiceResponse.OK("", s));
                }
            }
            return(ServiceResponse.Error("An error occurred inserting UnitOfMeasure " + s.Name));
        }
Esempio n. 26
0
        public void Api_UnitOfMeasureController_AddUnitOfMeasure()
        {
            UnitOfMeasure mdl = new UnitOfMeasure();

            mdl.AccountUUID = SystemFlag.Default.Account;
            mdl.Name        = Guid.NewGuid().ToString("N");
            mdl.UUID        = Guid.NewGuid().ToString("N");
            mdl.DateCreated = DateTime.UtcNow;

            string postData = JsonConvert.SerializeObject(mdl);

            Task.Run(async() =>
            {
                ServiceResult res = await TestHelper.SentHttpRequest("POST", "api/UnitsOfMeasure/Add", postData, _ownerAuthToken);
                Assert.IsNotNull(res);
                Assert.AreEqual(res.Code, 200);

                UnitOfMeasure p = JsonConvert.DeserializeObject <UnitOfMeasure>(res.Result.ToString());
                Assert.IsNotNull(p);
                TreeMonDbContext context      = new TreeMonDbContext(connectionKey);
                UnitOfMeasure dbUnitOfMeasure = context.GetAll <UnitOfMeasure>().Where(w => w.UUID == p.UUID).FirstOrDefault();
                Assert.IsNotNull(dbUnitOfMeasure);
                Assert.AreEqual(mdl.Name, dbUnitOfMeasure.Name);
            }).GetAwaiter().GetResult();
        }
Esempio n. 27
0
        public ServiceResult Insert(Credential c, bool validateFirst = true)
        {
            if (!this.DataAccessAuthorized(c, "POST", false))
            {
                return(ServiceResponse.Error("You are not authorized this action."));
            }

            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                if (validateFirst)
                {
                    Credential dbU = context.GetAll <Credential>().FirstOrDefault(wu => wu.Name.EqualsIgnoreCase(c.Name) && wu.AccountUUID == c.AccountUUID);

                    if (dbU != null)
                    {
                        return(ServiceResponse.Error("Credential already exists."));
                    }
                }

                if (string.IsNullOrWhiteSpace(c.UUID))
                {
                    c.UUID = Guid.NewGuid().ToString("N");
                }

                c.UUIDType = "Credential";
                if (context.Insert <Credential>(c))
                {
                    return(ServiceResponse.OK("", c));
                }
            }
            return(ServiceResponse.Error("An error occurred inserting credential " + c.Name));
        }
Esempio n. 28
0
 public List <EmailLog> GetEmailLogs(string accountUUID, bool deleted = false)
 {
     using (var context = new TreeMonDbContext(_dbConnectionKey))
     {
         return(context.GetAll <EmailLog>().Where(sw => (sw.AccountUUID == accountUUID) && sw.Deleted == deleted).OrderBy(ob => ob.DateSent).ToList());
     }
 }
Esempio n. 29
0
        public ServiceResult Insert(INode n, bool validateFirst = true)
        {
            if (n == null)
            {
                return(ServiceResponse.Error("Invalid account data."));
            }

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

            n.Initialize(this._requestingUser.UUID, this._requestingUser.AccountUUID, this._requestingUser.RoleWeight);

            var a = (Account)n;

            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                if (validateFirst)
                {
                    Account dbU = context.GetAll <Account>().FirstOrDefault(wu => (wu.Name?.EqualsIgnoreCase(a.Name) ?? false) && wu.AccountUUID == a.AccountUUID);

                    if (dbU != null)
                    {
                        return(ServiceResponse.Error("Account already exists."));
                    }
                }

                if (context.Insert <Account>(a))
                {
                    return(ServiceResponse.OK("", a));
                }
            }
            return(ServiceResponse.Error("System error, account was not added."));
        }
Esempio n. 30
0
        public ServiceResult Insert(SymptomLog s, bool validateFirst = true)
        {
            using (var context = new TreeMonDbContext(this._connectionKey))
            {
                if (validateFirst)
                {
                    SymptomLog dbU = context.GetAll <SymptomLog>().FirstOrDefault(wu => wu.Name.EqualsIgnoreCase(s.Name) && wu.AccountUUID == s.AccountUUID);

                    if (dbU != null)
                    {
                        return(ServiceResponse.Error("SymptomLog already exists."));
                    }
                }
                s.UUID     = Guid.NewGuid().ToString("N");
                s.UUIDType = "SymptomLog";

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

                if (context.Insert <SymptomLog>(s))
                {
                    return(ServiceResponse.OK("", s));
                }
            }
            return(ServiceResponse.Error("An error occurred inserting SymptomLog " + s.Name));
        }