public Task <BaseIncident> Add(BaseIncident incident) { if (incident.CommonId != 0) { throw new SimsIncidentExistsException("This incident has already been added."); } // IF we have an officer, and status is not open... if (!String.IsNullOrEmpty(incident.LeadOfficer) && incident.StatusId != (int)SimsIncidentStatusTypes.Open) { incident = incident.WithStatus((int)SimsIncidentStatusTypes.Open); } // if we don't have an officer status must be unsassigned if (String.IsNullOrEmpty(incident.LeadOfficer)) { incident = incident.WithStatus((int)SimsIncidentStatusTypes.Unassigned); } return(dbHost.Incidents.Add(incident)); }
/// <summary> /// This is the basic update of direct incident fields /// Collections are handled in their own objectsd /// </summary> /// <param name="incident"></param> /// <returns></returns> public async Task <BaseIncident> Update(BaseIncident incident) { var dbItem = this.ctx.Incidents.Find(incident.CommonId); if (dbItem == null) { throw new NullReferenceException("No incident was found"); } if (dbItem.IncidentStatusId == (int)IncidentStatusTypes.Closed) { throw new ArgumentOutOfRangeException("Cannot update a closed incident!"); } // Logical changes. // Mark some differences since last update // Have we changed to Status.unassigned? if so ensure we remove the lead officer. if (dbItem.IncidentStatusId == (int)IncidentStatusTypes.Open && incident.StatusId == (int)IncidentStatusTypes.Unassigned) { incident = incident.WithLeadOfficer(""); } // Have we assigned a new lead officer? // so status must be set to open if (String.IsNullOrEmpty(dbItem.LeadOfficer) && !String.IsNullOrEmpty(incident.LeadOfficer) && incident.StatusId != (int)IncidentStatusTypes.Closed) { incident = incident.WithStatus((int)IncidentStatusTypes.Open); } //Transfer our updates into the existing incident //incident.ToUpdateDb(dbItem); var receivedOn = dbItem.ReceivedOn; mapper.Map(incident, dbItem); // This can be cleared out by faulty data from the client. // It should not be replaced once set. //if (receivedOn.HasValue) // dbItem.ReceivedOn = receivedOn; // Are we closed? // Then ensure we update the closed date. if (dbItem.IncidentStatusId == (int)IncidentStatusTypes.Closed) { dbItem.IncidentClosed = DateTime.UtcNow; } var updatedDbItem = this.ctx.Incidents.Update(dbItem); await this.ctx.SaveChangesAsync(); return(mapper.Map <IncidentDb, BaseIncident>(updatedDbItem.Entity)); }