/// <summary> /// Create a device /// </summary> /// <param name="deviceTwinObj">DeviceTwinModel</param> /// <returns>DeviceModel</returns> public async Task <DeviceModel> CreateDevice(DeviceTwinModel deviceTwinObj) { //If device doesn't exist, throw exception DeviceDAO deviceEntity = _mapper.Map <DeviceDAO>(deviceTwinObj); deviceEntity.Id = await _repoDevices.CreateItemAsync(deviceEntity); if (_repoDevices.IsDocumentKeyNull(deviceEntity)) { throw new Exception($"An error occured when creating a new device: {deviceTwinObj.DeviceId}"); } return(_mapper.Map <DeviceModel>(deviceEntity)); }
/// <summary> /// Create a notification /// </summary> /// <param name="notification">NotificationCreationModel</param> /// <returns>NotificationModel</returns> public async Task <NotificationModel> CreateNotification(NotificationCreationModel notification) { var date = DateTime.UtcNow; NotificationDAO notificationDao = new NotificationDAO() { NotificationText = notification.NotificationText, CreationDate = date, UpdateDate = date, ResponseId = notification.ResponseId.ToString(), Status = notification.Status, Tags = notification.Tags, Title = notification.Title, User = notification.User }; notificationDao.Id = await _repoNotifications.CreateItemAsync(notificationDao); if (_repoNotifications.IsDocumentKeyNull(notificationDao)) { throw new Exception($"An error occured when creating a new notification"); } NotificationModel output = _mapper.Map <NotificationModel>(notificationDao); return(output); }
/// <summary> /// Create a new response /// </summary> /// <param name="responseObj">ResponseCreationModel</param> /// <returns>ResponseModel</returns> public async Task <ResponseModel> CreateResponse(ResponseCreationModel responseObj) { //Instantiate the actions InstantiateResponseActions(responseObj.ActionPlan.OpenActions); InstantiateResponseActions(responseObj.ActionPlan.CloseActions); ResponseDAO response = new ResponseDAO() { ActionPlan = _mapper.Map <ResponseActionPlanDAOObject>(responseObj.ActionPlan), ResponderUserId = responseObj.ResponderUserId, ResponseState = RESPONSE_STATE_ACTIVE, PrimaryEventClusterId = responseObj.PrimaryEventClusterId, Geolocation = _mapper.Map <GeolocationDAOObject>(responseObj.Geolocation) }; response.Id = await _repoResponses.CreateItemAsync(response); if (_repoResponses.IsDocumentKeyNull(response)) { throw new Exception($"An error occured when creating a new response"); } ResponseModel output = _mapper.Map <ResponseModel>(response); return(output); }
/// <summary> /// Create an Event Cluster /// </summary> /// <param name="eventObj">EventClusterCreationModel</param> /// <returns>EventClusterModel</returns> public async Task <EventClusterModel> CreateEventCluster(EventClusterCreationModel eventObj) { //If device doesn't exist, throw exception DeviceDAO deviceEntity = await _repoDevices.GetItemAsync(eventObj.DeviceId); if (deviceEntity == null) { throw new Exception($"No device found that matches DeviceId: {eventObj.DeviceId}"); } EventClusterDAO eventCluster = new EventClusterDAO() { Id = eventObj.EventClusterId.ToString(), Device = _mapper.Map <EventClusterDeviceDAOObject>(deviceEntity), EventType = eventObj.EventType.ToLower(), EventCount = 1, Events = new EventDAOObject[] { _mapper.Map <EventDAOObject>(eventObj) }, StartDate = eventObj.Date }; eventCluster.Id = await _repoEventClusters.CreateItemAsync(eventCluster); if (_repoEventClusters.IsDocumentKeyNull(eventCluster)) { throw new Exception($"An error occured when creating a new cluster id for DeviceId: {eventObj.DeviceId}"); } return(_mapper.Map <EventClusterModel>(eventCluster)); }
/// <summary> /// Create an action plan /// </summary> /// <param name="actionPlanObj">ActionPlanCreationModel</param> /// <returns>ActionPlanModel</returns> public async Task <ActionPlanModel> CreationActionPlan(ActionPlanCreationModel actionPlanObj) { ActionPlanDAO plan = _mapper.Map <ActionPlanDAO>(actionPlanObj); plan.Id = await _repoActionPlans.CreateItemAsync(plan); if (_repoActionPlans.IsDocumentKeyNull(plan)) { throw new Exception($"An error occured when creating a new action plan"); } return(_mapper.Map <ActionPlanModel>(plan)); }