/// <summary> /// Adds entity if Id is 0. Otherwise Updates it /// </summary> /// <exception cref="EntityException">Contains EF Exception and a standart HTTP-responce for client side</exception> public Guid AddOrUpdate <T>(T entity) where T : class, IEntity { if (entity is null) { throw new ArgumentNullException(nameof(entity)); } try { if (entity.Id == Guid.Empty) { if (entity is Human) { (entity as Human).Passhash = StringHash.GetStringSha256Hash((entity as Human).Passhash); } db.Add(EntitySecurityAdapter.Encrypt(entity)); } else { db.Update(EntitySecurityAdapter.Encrypt(entity)); //todo: bugfix user can not change his password or account is lost } db.SaveChanges(); return(entity.Id); } catch (Exception E) { throw new EntityException(E, entity); } }
/// <summary> /// Adds range of objects /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entities"></param> public void AddRange <T>(T[] entities) where T : class, IEntity { try { Human[] temp = entities as Human[]; if (temp is not null) { foreach (Human item in temp) { item.Passhash = StringHash.GetStringSha256Hash(item.Passhash); } db.AddRange(temp.Select(x => EntitySecurityAdapter.Encrypt(x))); } else { db.AddRange(entities.Select(x => EntitySecurityAdapter.Encrypt(x))); } db.SaveChanges(); } catch (Exception E) { throw new EntityException("Failed to InsertBulk.", E, null); } }
/// <summary> /// Get table with predicate where <paramref name="type"/> is inherited from <see cref="IEntity"/> /// </summary> /// <param name="predicate">For filtering entities</param> public IEnumerable <object> GetTable(Type type, Func <dynamic, bool> predicate) => db.Set(type).Select(x => EntitySecurityAdapter.Decrypt(x)).Where(predicate); // todo: optimize