Ejemplo 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));
        }