using Gremlin_BlazorServer.Data.DBClasses; using Gremlin_BlazorServer.Data.EntityClasses; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; namespace Gremlin_BlazorServer.Services; public class GenericController { public IList? GetAll() where TResult : class, IMetadata { try { using (GremlinDb gremlinDb = new()) { return gremlinDb.Set().ToList(); } } catch (Exception exception) { Console.WriteLine(exception.InnerException); return null; } } public IList? GetAll(string include) where TResult : class, IMetadata { try { using (GremlinDb gremlinDb = new()) { return gremlinDb.Set().Include(include).ToList(); } } catch (Exception exception) { Console.WriteLine(exception.InnerException); return null; } } public IList? GetAll(Predicate search) where TResult : class, IMetadata { ArgumentNullException.ThrowIfNull(search); try { using (GremlinDb gremlinDb = new()) { return gremlinDb.Set().AsEnumerable().Where(t => search(t)).ToList(); } } catch (Exception exception) { Console.WriteLine(exception.InnerException); return null; } } public async Task?> GetAllAsync() where TResult : class, IMetadata { try { await using (GremlinDb gremlinDb = new()) { return await gremlinDb.Set().ToListAsync(); } } catch (DbUpdateConcurrencyException exception) { await HandleDbUpdateConcurrencyException(exception); Console.WriteLine(exception.InnerException); return null; } } public async Task?> GetAllAsync(string include) where TResult : class, IMetadata { try { await using (GremlinDb gremlinDb = new()) { return await gremlinDb.Set().Include(include).ToListAsync(); } } catch (DbUpdateConcurrencyException exception) { await HandleDbUpdateConcurrencyException(exception); Console.WriteLine(exception.InnerException); return null; } } public async Task?> GetAllAsync(string include1, string include2) where TResult : class, IMetadata { try { await using (GremlinDb gremlinDb = new()) { return await gremlinDb.Set().Include(include1).Include(include2).ToListAsync(); } } catch (DbUpdateConcurrencyException exception) { await HandleDbUpdateConcurrencyException(exception); Console.WriteLine(exception.InnerException); return null; } } public async Task?> GetAllAsync(Predicate search) where TResult : class, IMetadata { ArgumentNullException.ThrowIfNull(search); try { await using (GremlinDb gremlinDb = new()) { return await Task.Run(() => gremlinDb.Set().AsEnumerable().Where(t => search(t)).ToList()); } } catch (DbUpdateConcurrencyException exception) { await HandleDbUpdateConcurrencyException(exception); Console.WriteLine(exception.InnerException); return null; } } public async Task?> GetAllAsync(Predicate search, string include) where TResult : class, IMetadata { ArgumentNullException.ThrowIfNull(search); try { await using (GremlinDb gremlinDb = new()) { return await Task.Run(() => gremlinDb.Set().Include(include).AsEnumerable().Where(t => search(t)).ToList()); } } catch (DbUpdateConcurrencyException exception) { await HandleDbUpdateConcurrencyException(exception); Console.WriteLine(exception.InnerException); return null; } } public IList? GetAll(Predicate search, string include) where TResult : class, IMetadata { ArgumentNullException.ThrowIfNull(search); try { using (GremlinDb gremlinDb = new()) { return gremlinDb.Set().Include(include).AsEnumerable().Where(t => search(t)).ToList(); } } catch (Exception exception) { Console.WriteLine(exception.InnerException); return null; } } public TResult? Get(Predicate search) where TResult : class, IMetadata { ArgumentNullException.ThrowIfNull(search); try { using (GremlinDb gremlinDb = new()) { return gremlinDb.Set().AsEnumerable().FirstOrDefault(t => search(t)); } } catch (Exception exception) { Console.WriteLine(exception.InnerException); return null; } } public async Task GetAsync(Predicate search) where TResult : class, IMetadata { ArgumentNullException.ThrowIfNull(search); try { await using (GremlinDb gremlinDb = new()) { return await Task.Run(() => gremlinDb.Set().AsEnumerable().FirstOrDefault(t => search(t))); } } catch (DbUpdateConcurrencyException exception) { await HandleDbUpdateConcurrencyException(exception); Console.WriteLine(exception.InnerException); return null; } } public TResult? Get(Predicate search, string include) where TResult : class, IMetadata { ArgumentNullException.ThrowIfNull(search); try { using (GremlinDb gremlinDb = new()) { return gremlinDb.Set().AsNoTracking().Include(include).AsEnumerable().FirstOrDefault(t => search(t)); } } catch (Exception exception) { Console.WriteLine(exception.InnerException); return null; } } public async Task GetAsync(Predicate search, string include1) where TResult : class, IMetadata { ArgumentNullException.ThrowIfNull(search); try { await using (GremlinDb gremlinDb = new()) { return await Task.Run(() => gremlinDb.Set().Include(include1).AsEnumerable().FirstOrDefault(t => search(t))); } } catch (DbUpdateConcurrencyException exception) { await HandleDbUpdateConcurrencyException(exception); Console.WriteLine(exception.InnerException); return null; } } public async Task GetAsync(Predicate search, string include1, string include2) where TResult : class, IMetadata { ArgumentNullException.ThrowIfNull(search); try { await using (GremlinDb gremlinDb = new()) { return await Task.Run(() => gremlinDb.Set().Include(include1).Include(include2).AsEnumerable().FirstOrDefault(t => search(t))); } } catch (DbUpdateConcurrencyException exception) { await HandleDbUpdateConcurrencyException(exception); Console.WriteLine(exception.InnerException); return null; } } public TResult? GetLast() where TResult : class, IMetadata { try { using (GremlinDb gremlinDb = new()) { return gremlinDb.Set().AsEnumerable().Last(); } } catch (Exception exception) { Console.WriteLine(exception.InnerException); return null; } } public int Insert(T entity) where T : class, IMetadata { try { using (GremlinDb gremlinDb = new()) { gremlinDb.Set().Add(entity); return gremlinDb.SaveChanges(); } } catch (Exception exception) { Console.WriteLine(exception.InnerException); return 0; } } public int Insert(IEnumerable entities) where T : class, IMetadata { try { using (GremlinDb gremlinDb = new()) { gremlinDb.Set().AddRange(entities); return gremlinDb.SaveChanges(); } } catch (Exception exception) { Console.WriteLine(exception.InnerException); return 0; } } public async Task InsertAsync(T entity) where T : class, IMetadata { try { await using (GremlinDb gremlinDb = new()) { gremlinDb.Set().Add(entity); return await gremlinDb.SaveChangesAsync(); } } catch (DbUpdateConcurrencyException exception) { await HandleDbUpdateConcurrencyException(exception); Console.WriteLine(exception.InnerException); return 0; } } public async Task InsertAsync(IEnumerable entities) where T : class, IMetadata { try { await using (GremlinDb gremlinDb = new()) { gremlinDb.Set().AddRange(entities); return await gremlinDb.SaveChangesAsync(); } } catch (DbUpdateConcurrencyException exception) { await HandleDbUpdateConcurrencyException(exception); Console.WriteLine(exception.InnerException); return 0; } } public static bool IsExisting(Predicate search) where T : class, IMetadata { ArgumentNullException.ThrowIfNull(search); try { using (GremlinDb gremlinDb = new()) { return gremlinDb.Set().AsEnumerable().Any(t => search(t)); } } catch (Exception exception) { Console.WriteLine(exception.InnerException); return false; } } public int Update(T entity) where T : class, IMetadata { try { using (GremlinDb gremlinDb = new()) { gremlinDb.Set().Update(entity); return gremlinDb.SaveChanges(); } } catch (Exception exception) { Console.WriteLine(exception.InnerException); return 0; } } public async Task UpdateAsync(T entity) where T : class, IMetadata { await using (GremlinDb gremlinDb = new()) { try { gremlinDb.Set().Update(entity); return await gremlinDb.SaveChangesAsync(false); } catch (DbUpdateConcurrencyException exception) { await HandleDbUpdateConcurrencyException(exception); Console.WriteLine(exception.InnerException); return 0; } } } private async Task HandleDbUpdateConcurrencyException(DbUpdateConcurrencyException exception) where T : class, IMetadata { // Loop through the entities that caused the concurrency conflict foreach (EntityEntry? entry in exception.Entries) { if (entry.Entity is T) { T? clientValues = (T)entry.Entity; PropertyValues? databaseEntry = await entry.GetDatabaseValuesAsync(); if (databaseEntry is null) // The record has been deleted from the database // Notify the user or handle the error in some other way throw new("The record has been deleted from the database."); T? databaseValues = (T)databaseEntry.ToObject(); // Compare the database values with the client values if (databaseValues.DataVersionNumber != clientValues.DataVersionNumber) // The name has been changed by another user // Notify the user or handle the error in some other way throw new("The record has been modified by another user."); // The conflict is caused by a property other than the name // Notify the user or handle the error in some other way throw new("A concurrency conflict occurred."); } // Handle concurrency conflicts for other entity types, if necessary throw new NotSupportedException($"Concurrency conflicts for entities of type {entry.Entity.GetType().Name} are not supported."); } } public int Update(IEnumerable entities) where T : class, IMetadata { try { using (GremlinDb gremlinDb = new()) { gremlinDb.Set().UpdateRange(entities); return gremlinDb.SaveChanges(); } } catch (Exception exception) { Console.WriteLine(exception.InnerException); return 0; } } public async Task UpdateAsync(IEnumerable entities) where T : class, IMetadata { try { await using (GremlinDb gremlinDb = new()) { gremlinDb.Set().UpdateRange(entities); return await gremlinDb.SaveChangesAsync(); } } catch (Exception exception) { Console.WriteLine(exception.InnerException); return 0; } } public async Task RemoveAsync(T entity) where T : class, IMetadata { try { await using (GremlinDb gremlinDb = new()) { gremlinDb.Set().Remove(entity); return await gremlinDb.SaveChangesAsync(); } } catch (Exception exception) { Console.WriteLine(exception.InnerException); return 0; } } }