12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- 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<pk_guid, PkGuidAddDbo, PkGuidDbo, PkGuidAddDbo, LuUtilsDbContext, string>
- {
- public LuUtilsPkGuidDataAccess(IServiceProvider serviceProvider) : base(serviceProvider)
- {
- }
-
- protected override DbSet<pk_guid> GetTable(LuUtilsDbContext db)
- {
- return db.pk_guid;
- }
-
- protected override pk_guid GetModelFromTCreate(PkGuidAddDbo obj)
- {
- return GetModelFromTUpdate(obj, new pk_guid());
- }
-
- protected override LuResult<T> HandleError<T>(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<T>.Error(LuStatus.InputError, e, "someText already exists");
- }
- if (pge.ConstraintName == "pkguid_some_text_check_insert")
- {
- return LuResult<T>.Error(LuStatus.InputError, e, "someText can not end with '_edited'");
- }
- if (pge.ConstraintName == "pkguid_some_text_check_update")
- {
- return LuResult<T>.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<Func<pk_guid, bool>> GetFilterExpression(LuFilterDbo filter)
- {
- 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<bool> _Add(pk_guid model, PkGuidAddDbo dbo, LuUtilsDbContext db, DbSet<pk_guid> table)
- {
- if (dbo.SomeInt == 2424)
- {
- throw new Exception("Test unexpected db error");
- }
- if (dbo.SomeInt == 4242)
- {
- return LuResult<bool>.Error(LuStatus.DbError, "Some expected error", "");
- }
- return LuResult<bool>.Ok(true);
- }
-
- protected override LuResult<bool> _EditSingleById(pk_guid model, PkGuidAddDbo update, LuUtilsDbContext db, DbSet<pk_guid> table)
- {
- return LuResult<bool>.Ok(true);
- }
- }
- }
|