예제 #1
0
        //public CPR_Incident AddPrimaryAbuseTypeToIncident(int incidentId, int abuseTypeId)
        public CPR_Incident AddPrimaryAbuseTypeToIncident(int incidentId, int abuseTypeId)
        {
            var dbContext = new SDIIS_DatabaseEntities();

            try
            {
                var incidentToEdit = dbContext.CPR_Incidents.FirstOrDefault(x => x.Incident_Id.Equals(incidentId));
                if (incidentToEdit == null)
                {
                    return(null);
                }

                //abuseTypeId =incidentToEdit.Incident_Abuse_Types.ToList().RemoveAll(p => p.Is_Primary_Abuse_Type == true);
                if (abuseTypeId != 0)
                {
                    var newAbuseType = new Incident_Abuse_Type()
                    {
                        Incident_Id = incidentId, Abuse_Type_Id = abuseTypeId, Is_Primary_Abuse_Type = true
                    };

                    incidentToEdit.Incident_Abuse_Types.Add(newAbuseType);
                }
                dbContext.SaveChanges();

                return(incidentToEdit);
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
            {
                Exception raise = dbEx;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        string message = string.Format("{0}:{1}",
                                                       validationErrors.Entry.Entity.ToString(),
                                                       validationError.ErrorMessage);
                        // raise a new exception nesting
                        // the current instance as InnerException
                        raise = new InvalidOperationException(message, raise);
                    }
                }
                throw raise;
            }
        }
예제 #2
0
        public Incident_Abuse_Type CreateIncidentAbuseType(int incidentId, int abuseTypeId, bool isPrimaryAbuse)
        {
            var dbContext = new SDIIS_DatabaseEntities();

            var incidentAbuseType = new Incident_Abuse_Type()
            {
                Incident_Id = incidentId, Abuse_Type_Id = abuseTypeId, Is_Primary_Abuse_Type = isPrimaryAbuse
            };

            try
            {
                var newIncidentAbuseType = dbContext.Incident_Abuse_Types.Add(incidentAbuseType);

                dbContext.SaveChanges();

                return(newIncidentAbuseType);
            }
            catch (Exception)
            {
                return(null);
            }
        }
예제 #3
0
        public CPR_Incident AddSecondaryAbuseTypesToIncident(int incidentId, List <int> abuseTypeIds)
        {
            var dbContext = new SDIIS_DatabaseEntities();

            try
            {
                var incidentToEdit = dbContext.CPR_Incidents.FirstOrDefault(x => x.Incident_Id.Equals(incidentId));
                if (incidentToEdit == null)
                {
                    return(null);
                }

                var abuseTypesToRemove = incidentToEdit.Incident_Abuse_Types.Where(x => !x.Is_Primary_Abuse_Type).ToList();

                dbContext.Incident_Abuse_Types.RemoveRange(abuseTypesToRemove);
                dbContext.SaveChanges();

                foreach (var abuseTypeId in abuseTypeIds)
                {
                    var newAbuseType = new Incident_Abuse_Type()
                    {
                        Incident_Id = incidentId, Abuse_Type_Id = abuseTypeId, Is_Primary_Abuse_Type = false
                    };

                    dbContext.Incident_Abuse_Types.Add(newAbuseType);
                }

                dbContext.SaveChanges();

                return(incidentToEdit);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }