Beispiel #1
0
        public string AddDashPrivileges(string userID, List <string> dashPrivileges) //todo how to handle errors and pass up messags?
        {
            List <string> errorList = new List <string>();

            try
            {
                foreach (var dashID in dashPrivileges)
                {
                    Exception error = accessor.AddDashPrivilege(dashID, userID);
                    if (error != null)
                    {
                        errorList.Add(error.Message);
                    }
                }

                if (errorList.Count == 0)
                {
                    return("");
                }
                else
                {
                    return(JsonConvert.SerializeObject(errorList));
                }
            }
            catch (Exception e)
            {
                return(e.Message);
            }
        }
        public string AddDashboardWithPriv(Dashboard dash, ModelStateDictionary ModelState,
                                           List <string> usersWhoCanEdit)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (dash.DashboardID == 0)
                    {
                        var insertedDashID = accessor.InsertDashboard(dash); // if any error from insert, add to error list

                        foreach (var userID in usersWhoCanEdit)
                        {
                            accessor.AddDashPrivilege(insertedDashID, userID);
                        }
                    }
                    else
                    {
                        accessor.RemoveAllPrivilegesOnDashboard(dash.DashboardID.ToString()); // gives us a clean slate to add new privileges

                        foreach (var userID in usersWhoCanEdit)
                        {
                            accessor.AddDashPrivilege(dash.DashboardID.ToString(), userID); // adds privileges
                        }

                        accessor.UpdateDashboard(dash); // if any error from insert, add to error list
                    }
                }
            }
            catch (Exception e)
            {
                ModelState.AddModelError(string.Empty, e.Message);
            }


            if (ModelState.ErrorCount == 0)
            {
                return("/DashManager"); // no errors, redirect user
            }
            else
            {                                                  // if errors, convert them to a list to display
                var messages = ToolEngine.GetErrorMessages(ModelState);
                return(JsonConvert.SerializeObject(messages)); // returns string list of errors for page to display, errors based on model specifications
            }
        }