Exemplo n.º 1
0
        public IHttpActionResult GetWidgets()
        {
            widgetManager = new WidgetManager();
            itemManager   = new ItemManager();

            Dashboard dashboard = widgetManager.GetDashboardWithAllDataForUserId(User.Identity.GetUserId());

            if (dashboard == null)
            {
                return(NotFound());
            }

            //Get all widgets for user
            List <UserWidget> userWidgets = widgetManager.GetWidgetsForDashboard(dashboard.DashboardId).ToList();

            if (userWidgets == null || userWidgets.Count() == 0)
            {
                return(StatusCode(HttpStatusCode.NoContent));
            }

            //Convert all widget to userWidgets (DTO's).
            List <UserWidgetViewModel> userWidgetDtos = Mapper.Map(userWidgets, new List <UserWidgetViewModel>());

            foreach (UserWidgetViewModel userWidgetVM in userWidgetDtos)
            {
                foreach (int itemId in userWidgetVM.ItemIds)
                {
                    //Get the topic of the graph of the userwidget.
                    string keyValue = widgetManager.GetWidgetWithAllData(userWidgetVM.WidgetId)?.WidgetDatas.FirstOrDefault()?.KeyValue;

                    //Get all widgets for each item in userWidgets.
                    IEnumerable <Widget> widgetsForItem = widgetManager.GetAllWidgetsWithAllDataForItem(itemId);

                    //Check if these widgets have graph data (WidgetData) on this topic, if they do add this data to the userWidget.
                    IEnumerable <WidgetData> widgetDatas = widgetsForItem.FirstOrDefault(w => w.WidgetDatas.Any(wd => wd.KeyValue == keyValue)).WidgetDatas;

                    //Convert the graphdata to a DTO.
                    List <WidgetDataDTO> widgetDataDtos = Mapper.Map(widgetDatas, new List <WidgetDataDTO>());

                    //Link the graphdata to the corresponding item.
                    widgetDataDtos.First().ItemName = itemManager.GetItem(itemId).Name;

                    if (userWidgetVM.WidgetDataDtos == null)
                    {
                        userWidgetVM.WidgetDataDtos = widgetDataDtos;
                    }
                    else
                    {
                        userWidgetVM.WidgetDataDtos.AddRange(widgetDataDtos);
                    }
                }
            }

            return(Ok(userWidgetDtos));
        }
Exemplo n.º 2
0
        /// <summary>
        ///Reads all widgets for a user dashboard and returns them
        /// </summary>
        public IHttpActionResult GetUserWidgets()
        {
            widgetManager = new WidgetManager();
            Dashboard dash = widgetManager.GetDashboard(User.Identity.GetUserId());

            try {
                List <UserWidget> widgets = widgetManager.GetWidgetsForDashboard(dash.DashboardId).ToList();

                if (widgets == null || widgets.Count() == 0)
                {
                    return(StatusCode(HttpStatusCode.NoContent));
                }

                return(Ok(Mapper.Map(widgets, new List <UserWidgetDTO>())));
            } catch (Exception e) {
                return(BadRequest(e.Message));
            }
        }