using System; using System.Linq; using System.Linq.Expressions; using Luticate2.Utils.DataAccess; using Luticate2.Utils.Dbo.Filter; using Luticate2.Utils.Dbo.Result; using Microsoft.EntityFrameworkCore; using Npgsql; using TestUtils.DataAccess.Models; using TestUtils.Dbo.PkGuid; namespace TestUtils.DataAccess { public class LuUtilsPkGuidDataAccess : LuEfCrudDataAccess { public LuUtilsPkGuidDataAccess(IServiceProvider serviceProvider) : base(serviceProvider) { } protected override DbSet GetTable(LuUtilsDbContext db) { return db.pk_guid; } protected override pk_guid GetModelFromTCreate(PkGuidAddDbo obj) { return GetModelFromTUpdate(obj, new pk_guid()); } protected override LuResult HandleError(Exception e) { if (e is DbUpdateException) { var pge = e.InnerException as PostgresException; if (pge != null) { if (pge.ConstraintName == "pk_guid_some_text_key") { return LuResult.Error(LuStatus.InputError, e, "someText already exists"); } if (pge.ConstraintName == "pkguid_some_text_check_insert") { return LuResult.Error(LuStatus.InputError, e, "someText can not end with '_edited'"); } if (pge.ConstraintName == "pkguid_some_text_check_update") { return LuResult.Error(LuStatus.InputError, e, "someText must end with '_edited'"); } } } return null; } protected override void EditModelFromTUpdate(PkGuidAddDbo obj, pk_guid model) { model.some_int = obj.SomeInt; model.some_text = obj.SomeText; } protected override PkGuidDbo GetDboFromModel(pk_guid model) { return model.ToDbo(); } protected override Expression> GetFilterExpression(LuFilterDbo filter) { if (filter == null) { return x => true; } var someText = filter.GetFilterString("someText", null); var someInt = filter.GetFilterInt("someInt", null); return model => LuUtilsDbContext.lu_texts_match(filter.Query, model.some_text + " " + model.some_int.ToString()) && (someText == null || LuUtilsDbContext.lu_texts_match(someText, model.some_text)) && (someInt == null || model.some_int == someInt); } protected override LuResult _Add(pk_guid model, PkGuidAddDbo dbo, LuUtilsDbContext db, IQueryable table) { if (dbo.SomeInt == 2424) { throw new Exception("Test unexpected db error"); } if (dbo.SomeInt == 4242) { return LuResult.Error(LuStatus.DbError, "Some expected error", ""); } return LuResult.Ok(true); } protected override LuResult _EditSingleById(pk_guid model, PkGuidAddDbo update, LuUtilsDbContext db, IQueryable table) { return LuResult.Ok(true); } } }