12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- 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)
- {
- return model => LuUtilsDbContext.lu_texts_match(filter.Query, model.some_text + " " + model.some_int.ToString());
- }
-
- protected override LuResult<pk_guid> _Add(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<pk_guid>.Error(LuStatus.DbError, "Some expected error", "");
- }
- return LuResult<pk_guid>.Ok(GetModelFromTCreate(dbo));
- }
- }
- }
|