using System; 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) { return model => LuUtilsDbContext.lu_texts_match(filter.Query, model.some_text + " " + model.some_int.ToString()); } protected override LuResult _Add(PkGuidAddDbo dbo, LuUtilsDbContext db, DbSet 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(GetModelFromTCreate(dbo)); } } }