public bool Delete(Guid entityId) { try { var relRepository = new DbRelationRepository(CurrentContext); var relations = relRepository.Read(); var entityRelations = relations.Where(x => x.TargetEntityId == entityId || x.OriginEntityId == entityId); using (DbConnection con = DbContext.Current.CreateConnection()) { try { con.BeginTransaction(); foreach (var relation in entityRelations) { relRepository.Delete(relation.Id); } var entity = Read(entityId); NpgsqlCommand command = con.CreateCommand("DELETE FROM entities WHERE id=@id; DROP TABLE rec_" + entity.Name); var parameterId = command.CreateParameter() as NpgsqlParameter; parameterId.ParameterName = "id"; parameterId.Value = entityId; parameterId.NpgsqlDbType = NpgsqlDbType.Uuid; command.Parameters.Add(parameterId); var result = command.ExecuteNonQuery() > 0; con.CommitTransaction(); return(result); } catch { con.RollbackTransaction(); throw; } } } finally { Cache.Clear(); } }
public bool Create(DbEntity entity, Dictionary <string, Guid> sysldDictionary = null, bool createOnlyIdField = true) { try { using (DbConnection con = DbContext.Current.CreateConnection()) { con.BeginTransaction(); try { List <DbParameter> parameters = new List <DbParameter>(); JsonSerializerSettings settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto }; DbParameter parameterId = new DbParameter(); parameterId.Name = "id"; parameterId.Value = entity.Id; parameterId.Type = NpgsqlDbType.Uuid; parameters.Add(parameterId); DbParameter parameterJson = new DbParameter(); parameterJson.Name = "json"; parameterJson.Value = JsonConvert.SerializeObject(entity, settings); parameterJson.Type = NpgsqlDbType.Json; parameters.Add(parameterJson); string tableName = RECORD_COLLECTION_PREFIX + entity.Name; DbRepository.CreateTable(tableName); foreach (var field in entity.Fields) { bool isPrimaryKey = field.Name.ToLowerInvariant() == "id"; FieldType fieldType = field.GetFieldType(); DbRepository.CreateColumn(tableName, field); } bool result = DbRepository.InsertRecord("entities", parameters); if (!result) { throw new Exception("Entity record was not added!"); } if (entity.Id != SystemIds.UserEntityId && createOnlyIdField == false) { DbEntity userEntity = Read(SystemIds.UserEntityId); DbRelationRepository relRep = new DbRelationRepository(CurrentContext); string createdByRelationName = $"user_{entity.Name}_created_by"; string modifiedByRelationName = $"user_{entity.Name}_modified_by"; Guid createdByRelationId = Guid.NewGuid(); if (sysldDictionary != null && sysldDictionary.ContainsKey(createdByRelationName)) { createdByRelationId = sysldDictionary[createdByRelationName]; } Guid modifiedByRelationId = Guid.NewGuid(); if (sysldDictionary != null && sysldDictionary.ContainsKey(modifiedByRelationName)) { modifiedByRelationId = sysldDictionary[modifiedByRelationName]; } List <DbEntityRelation> relationList = relRep.Read(); DbEntityRelation tempRel = relationList.FirstOrDefault(r => r.Name == createdByRelationName); if (tempRel != null) { createdByRelationId = tempRel.Id; relRep.Delete(createdByRelationId); } tempRel = relationList.FirstOrDefault(r => r.Name == modifiedByRelationName); if (tempRel != null) { modifiedByRelationId = tempRel.Id; relRep.Delete(modifiedByRelationId); } DbEntityRelation createdByRelation = new DbEntityRelation(); createdByRelation.Id = createdByRelationId; createdByRelation.Name = createdByRelationName; createdByRelation.Label = $"user<-[1]:[m]->{entity.Name}.created_by"; createdByRelation.RelationType = EntityRelationType.OneToMany; createdByRelation.OriginEntityId = SystemIds.UserEntityId; createdByRelation.OriginFieldId = userEntity.Fields.Single(f => f.Name == "id").Id; createdByRelation.TargetEntityId = entity.Id; createdByRelation.TargetFieldId = entity.Fields.Single(f => f.Name == "created_by").Id; { bool res = relRep.Create(createdByRelation); if (!res) { throw new Exception("Creation of relation between User and Area entities failed!"); } } DbEntityRelation modifiedByRelation = new DbEntityRelation(); modifiedByRelation.Id = modifiedByRelationId; modifiedByRelation.Name = modifiedByRelationName; modifiedByRelation.Label = $"user<-[1]:[m]->{entity.Name}.last_modified_by"; modifiedByRelation.RelationType = EntityRelationType.OneToMany; modifiedByRelation.OriginEntityId = SystemIds.UserEntityId; modifiedByRelation.OriginFieldId = userEntity.Fields.Single(f => f.Name == "id").Id; modifiedByRelation.TargetEntityId = entity.Id; modifiedByRelation.TargetFieldId = entity.Fields.Single(f => f.Name == "last_modified_by").Id; { bool res = relRep.Create(modifiedByRelation); if (!res) { throw new Exception($"Creation of relation between User and {entity.Name} entities failed!"); } } } con.CommitTransaction(); return(result); } catch (Exception) { con.RollbackTransaction(); } } return(false); } finally { Cache.Clear(); } }