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)); }
public IHttpActionResult GetGraphs(int itemId, int widgetId) { widgetManager = new WidgetManager(); itemManager = new ItemManager(); //Get widgets for item IEnumerable <Widget> widgets = widgetManager.GetAllWidgetsWithAllDataForItem(itemId); //Get keyvalue for the widgetid. string keyValue = widgetManager.GetWidgetWithAllData(widgetId)?.WidgetDatas.FirstOrDefault()?.KeyValue; if (keyValue == null) { keyValue = widgetManager.GetWidgetWithAllData(widgetId)?.PropertyTags.FirstOrDefault()?.Name; } try { IEnumerable <WidgetData> widgetDatas = widgets.FirstOrDefault(widget => widget.WidgetDatas.Any(data => data.KeyValue == keyValue)).WidgetDatas; IEnumerable <WidgetDataDTO> widgetDataDtos = Mapper.Map(widgetDatas, new List <WidgetDataDTO>()); //Get item name widgetDataDtos.First().ItemName = itemManager.GetItem(itemId).Name; return(Ok(widgetDataDtos)); #pragma warning disable CS0168 // Variable is declared but never used } catch (Exception e) #pragma warning restore CS0168 // Variable is declared but never used { IEnumerable <WidgetData> widgetDatas = new List <WidgetData>(); IEnumerable <WidgetDataDTO> widgetDataDtos = Mapper.Map(widgetDatas, new List <WidgetDataDTO>()); return(Ok(widgetDataDtos)); } }