123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- 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<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)
- {
- 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<bool> _Add(pk_guid model, PkGuidAddDbo dbo, LuUtilsDbContext db, IQueryable<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, IQueryable<pk_guid> table)
- {
- return LuResult<bool>.Ok(true);
- }
- }
- }
|